-
Notifications
You must be signed in to change notification settings - Fork 5
/
app-server.js
executable file
·103 lines (81 loc) · 2.45 KB
/
app-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
'use strict'
var koa = require('koa')
var app = koa()
app.proxy = true
var http = require('http')
var https = require('https')
var fs = require('fs')
var co = require('co')
var session = require('koa-session')
var compose = require('koa-compose')
var body = require('koa-body')
// var enforceHttps = require('koa-sslify')
var config = require('./config')
var modules = require('./modules')
var routers = require('./routers')
var service = require('./service')
// 服务器启动时的初始状态检查
var statusCheck = require('./modules/tools/statusCheck.js')
// session config
var CONFIG = {
key: 'koa:sess', /** (string) cookie key (default is koa:sess) */
maxAge: 86400000, /** (number) maxAge in ms (default is 1 days) */
overwrite: true, /** (boolean) can overwrite or not (default true) */
httpOnly: true, /** (boolean) httpOnly or not (default true) */
signed: true, /** (boolean) signed or not (default true) */
}
// https option
var options = {
key: fs.readFileSync('keys/server.key'),
cert: fs.readFileSync('keys/server.crt'),
port: 8081
}
module.exports = function () {
Promise.all([
co(statusCheck)
]).then([
co(start)
]).catch(function (e) {
console.log('err:' + e.stack)
})
}
function * start () {
var ver = require('./package.json').version
var mw = compose([
/** force to redirect to https on all pages */
// enforceHttps(options),
/** control internal error */
service['500'],
/** send response time to browser */
modules.timer(),
/** send version of server to browser */
modules.version(ver),
/** init session storage */
session(CONFIG, app),
/** init local route */
routers.start(),
/** handle all 404 errors */
service['404']
])
app.use(body())
app.use(modules.urlFilter(mw))
// app.use(service['404'])
// app.listen(config.serverPort)
// http server
if (config.env === 'prd') {
http.createServer(app.callback()).listen(config.serverPort, function () {
// console.log('server http on ' + config.serverPort)
console.log(`server: http://${config.ip}:${config.serverPort}`)
})
}
if (config.env === 'dev') {
// https
https.createServer(options, app.callback()).listen(config.serverPorts, function () {
// console.log('server https on ' + config.serverPorts)
console.log(`server: https://${config.ip}:${config.serverPorts}`)
})
}
app.on('error', function (err, ctx) {
console.log('err:' + err.stack)
})
}