This repository has been archived by the owner on Mar 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
111 lines (92 loc) · 2.83 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* @fileoverview This is the server app script.
* @author [email protected] (Alvin Lin)
*/
const PROD_MODE = process.argv.includes('--prod')
const PORT = process.env.PORT || 5000
const GITHUB_PAGE = 'https://github.com/omgimanerd/nycurl'
const ERROR = 'An error occurred! Please try again later.\n'
// Dependencies.
const colors = require('colors')
const express = require('express')
const http = require('http')
const path = require('path')
const analyticsFile = path.join(__dirname, 'logs/analytics.log')
const errorFile = path.join(__dirname, 'logs/error.log')
const analytics = require('./server/analytics')
const api = require('./server/api')
const formatter = require('./server/formatter')
const loggers = require('./server/loggers')({
PROD_MODE: PROD_MODE,
analyticsFile: analyticsFile,
errorFile: errorFile
})
const logError = loggers.errorLogger.error
// Server initialization
const app = express()
app.set('port', PORT)
app.set('view engine', 'pug')
app.disable('etag')
app.use('/dist', express.static(__dirname + '/dist'))
app.use('/favicon.ico', express.static(__dirname + '/client/favicon.ico'))
app.use('/robots.txt', express.static(__dirname + '/robots.txt'))
// Dev output
app.use(loggers.devLogger)
// Write analytics-worthy requests to the analytics log file.
app.use(loggers.analyticsLogger)
// If the request is a curl request, we it as a param in the request object.
app.use((request, response, next) => {
request.isCurl = (request.headers['user-agent'] || '').includes('curl')
next()
})
app.get('/analytics', (request, response, next) => {
if (request.isCurl) {
next()
} else {
response.status(201).render('analytics')
}
})
app.post('/analytics', (request, response) => {
analytics.get(analyticsFile).then(data => {
response.status(201).send(data)
}).catch(error => {
logError(error)
response.status(500).send(error)
})
})
app.get('/:section?', (request, response, next) => {
if (!request.isCurl) {
response.status(301).redirect(GITHUB_PAGE)
return
}
const section = request.params.section || 'home'
if (section === 'help') {
response.send(formatter.formatHelp(false))
return
}
if (!api.isValidSection(section)) {
next()
return
}
api.fetchArticles(section).then(articles => {
response.send(formatter.formatArticles(articles, request.query))
}).catch(error => {
logError(error)
response.status(500).send(ERROR.red)
})
})
app.use((request, response) => {
response.status(400).send(formatter.formatHelp(true))
})
app.use((error, request, response, next) => {
logError(error)
response.status(500).send(ERROR.red)
})
// Starts the server.
http.Server(app).listen(PORT, () => {
if (PROD_MODE) {
console.log(`STARTING PRODUCTION SERVER ON PORT ${PORT}`.green)
} else {
console.log(`STARTING DEV SERVER ON PORT ${PORT}`.green)
}
})