-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.js
63 lines (51 loc) · 1.34 KB
/
app.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
require('dotenv').config({silent: true})
const Koa = require('koa')
const json = require('koa-json')
const logger = require('koa-logger')
const bodyparser = require('koa-bodyparser')
const serve = require('koa-static')
const path = require('path')
const api = require('./server/router/api')
const historyApiFallback = require('koa2-history-api-fallback')
const koaRouter = require('koa-router')
const IO = require('koa-socket')
const { getRateLimit } = require('./server/controllers/api')
const app = new Koa()
const port = parseInt(process.env.KOA_PORT) || 443
const router = koaRouter()
app.use(bodyparser())
app.use(json())
app.use(logger())
app.use(async (ctx, next) => {
let start = new Date()
await next()
let ms = new Date() - start
console.log('%s %s - %s', ctx.method, ctx.url, ms)
io.broadcast('limit', {
...getRateLimit()
})
})
app.on('error', function (err, ctx) {
console.log('server error', err)
ctx.body = {
success: false
}
})
router.use('/api', api.routes())
app.use(router.routes())
app.use(historyApiFallback())
app.use(serve(path.resolve('dist')))
app.use(async (ctx, next) => {
console.log(ctx, next)
})
const io = new IO()
io.attach(app)
io.on('getLimit', () => {
io.broadcast('limit', {
...getRateLimit()
})
})
app.listen(port, () => {
console.log(`Koa is listening in ${port}`)
})
module.exports = app