-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (56 loc) · 1.61 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
const path = require('path')
const config = require('config')
const Koa = require('koa')
const Router = require('koa-router')
const body = require('koa-body')
const responseHandler = require('./middlewares/response-handler')
const errorHandler = require('./middlewares/error-handler')
const logger = require('./logger')
const koaLogger = require('koa-logger')
const koaValidate = require('koa-validate')
const cors = require('kcors')
const serve = require('koa-static')
const json = require('koa-json')
const db = require('./db')
const app = new Koa()
app.context.db = db
app.context.logger = logger
koaValidate(app)
const router = new Router()
const normalizedPath = path.join(__dirname, 'routes')
require('fs')
.readdirSync(normalizedPath)
.forEach(file => {
require('./routes/' + file)(router)
})
const koaPort = config.get('api_port')
app
.use(async (ctx, next) => {
const start = Date.now()
await next()
const ms = Date.now() - start
ctx.set('X-Response-Time', `${ms}ms`)
})
.use(json())
.use(body())
.use(cors())
.use(responseHandler)
.use(errorHandler)
.use(koaLogger())
.use(router.routes())
.use(router.allowedMethods())
.use(serve('.'))
.on('error', error => {
logger.error(error)
})
.listen(koaPort, () => {
logger.info(`Api is running. Url: http://localhost:${koaPort}.`)
logger.info(`GraphQL Server is now running on http://localhost:${koaPort}/graphql`)
})
process
.on('uncaughtException', error => {
logger.error(`Caught global exception: ${error}`)
})
.on('unhandledRejection', reason => {
logger.error(`Unhandled rejection: ${reason}`)
})