forked from a632079/teng-koa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
68 lines (60 loc) · 1.44 KB
/
core.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
'use strict'
// Import Packages
const winston = require('winston')
const nconf = require('nconf')
const colors = require('colors/safe')
const Koa = require('koa') // Koa v2
// const mail = require('./src/mail')
// PreStart
const preStart = require('./src/prestart')
preStart.load()
// Use blubird promise
global.Promise = require('bluebird')
// Register Server
const app = new Koa()
// Load CronJob
const cron = require('./src/cron')
cron.load()
// Register Middlewares (Plugins)
async function registerMiddlewares () {
try {
const middlewares = require('./plugins')
for (let middleware of middlewares) {
app.use(middleware)
}
winston.verbose('All Plugins Load done.')
} catch (e) {
winston.error(e)
// mail.error(e)
process.exit(1)
}
}
// Load Route
async function registerRoutes (routes) {
try {
const router = await routes
app
.use(router.routes())
.use(router.allowedMethods())
winston.verbose('All Routes Load done.')
} catch (e) {
winston.error(e)
// mail.error(e)
process.exit(1)
}
}
// Start Server
async function start () {
try {
await registerMiddlewares()
const Routes = require('./src/route')
await registerRoutes(new Routes())
await app.listen(nconf.get('server:port'))
} catch (e) {
winston.error(e)
// mail.error(e)
process.exit(1)
}
}
start()
winston.info(colors.green('Server is started. Listening on Port:' + nconf.get('server:port')))