-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
73 lines (55 loc) · 2.04 KB
/
index.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
const path = require('path')
const express = require('express')
const { Router } = express
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const fallback = require('express-history-api-fallback')
const makeDatabase = require('database')
const makeReport = require('reporter')
const { makeAuthorise } = require('api/auth')
const makeUserApi = require('user')
const makeStoryApi = require('story')
const makeCharacterApi = require('character')
const makePostApi = require('post')
const makeReportApi = require('crash-report')
const errorHandler = require('error-handler')
const logger = require('logger')
require('dotenv').config()
const { SECRET, DB_URL, DB_NAME, PORT, IMAGES_FOLDER, REPORT_URL } = process.env
const database = makeDatabase({ DB_URL, DB_NAME })
const report = makeReport({ REPORT_URL })
const authorise = makeAuthorise({ SECRET })
const app = express()
app.disable('x-powered-by')
app.use(cookieParser())
app.use(bodyParser.json())
app.use((req, res, next) => {
logger.debug(`${req.method} ${req.originalUrl} | ${req.get('User-Agent')}`)
next()
})
console.log(`Connecting to database at ${DB_URL}...`)
database()
.then(db => {
app.use('/api', makeStoryApi({ Router, authorise, db }))
app.use('/api', makeUserApi({ SECRET, IMAGES_FOLDER, Router, authorise, db }))
app.use('/api', makeCharacterApi({ Router, authorise, db }))
app.use('/api', makePostApi({ Router, authorise, db}))
app.use('/api/report', makeReportApi({ Router, report }))
const static_root = path.join(__dirname, '..', 'static')
app.use(express.static(static_root, { extensions: [ 'html' ]}))
app.use(fallback('index.html', { root: static_root }))
app.use(errorHandler)
app.listen(PORT, () => logger.info(`Server started listening on port ${PORT}.`))
})
.catch(err => {
console.error(err)
process.exit(1)
})
const exitHandler = () => database.close()
.then(() => {
console.log('Bye.')
process.exit()
})
process.on('exit', exitHandler)
process.on('SIGINT', exitHandler)
process.on('SIGTERM', exitHandler)