-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ls
74 lines (60 loc) · 1.66 KB
/
app.ls
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
process.on 'unhandledRejection', (reason, p) ->
throw new Error(reason)
require('app-module-path').addPath(__dirname)
require! {
levn
getsecret
#bluebird
}
#fs = bluebird.promisifyAll(require('fs'))
roles_list = ['logging', 'viewdata']
if getsecret('roles')?
roles_list = levn.parse '[String]', getsecret('roles')
roles = {}
for role in roles_list
roles[role] = true
if roles.debug?
require('libs/globals').enable_globals()
else
require('libs/globals').disable_globals()
{
kapp
app
auth
} = require 'libs/server_common'
app.use (ctx, next) ->>
ip_addr = ctx.request.headers["x-forwarded-for"]
if ip_addr
list = ip_addr.split(",")
ip_addr = list[list.length-1]
else
ip_addr = ctx.request.ip
ctx.request.ip_address_fixed = ip_addr
await next()
if roles.https?
app.use(require('koa-sslify')({trustProtoHeader: true}))
require('routes/common_routes')
if roles.logging?
require('routes/logging_routes')
if roles.viewdata?
require('routes/viewdata_routes')
if roles.debug?
do ->
console.log 'go to chrome://inspect to debug'
# https://medium.com/@paul_irish/debugging-node-js-nightlies-with-chrome-devtools-7c4a1b95ae27
for k,v of require('libs/globals').get_globals()
global[k] = v
global.printcb = (err, x) ->
console.log err
if x?
console.log(x)
setInterval ->
# seems to be necessary so that async calls resolve in the inspector
, 100
kapp.use(app.routes())
kapp.use(app.allowedMethods())
if roles.viewdata?
kapp.use(require('koa-static')(__dirname + '/www'))
port = process.env.PORT ? 5000
kapp.listen(port)
console.log "listening to port #{port} visit http://localhost:#{port}"