This repository has been archived by the owner on May 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·103 lines (93 loc) · 2.14 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
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'
/***
* debuglog namespaces: awning, awning.REST, awning.rewrite, awning.static
***/
exports.dev = require('./lib/dev')
exports.build = require('./lib/build')
exports.utils = require('./lib/utils')
exports.Store = require('./lib/store')
exports.server = (config = {}) => {
const
paths = {
HttpServer: './lib/server',
middleware: './middleware'
}
,
fs = require('fs'), path = require('path'), util = require('util')
,
debug = util.debuglog('awning'),
debug_socket = util.debuglog('awning.socket')
,
HttpServer = require(paths.HttpServer)
,
noop = () => {}
;
const {
// Stuff we need for middleware.
root,
api,
auth,
globals,
bnsConfig
} = config
Object.assign(global.app = {}, {}, {
root,
api,
auth,
bnsConfig
}, globals)
const {
name,
uid,
port,
rewrite,
onError = noop,
onRequest = noop,
socketTimeout = 120000,
middleware = [
'logger',
'auth',
'REST',
'rewrite',
'beans'
]
} = config
debug('...startup')
const server = new HttpServer({
name,
uid,
port,
api,
rewrite,
middleware: middleware.map( middleware => require(`${paths.middleware}/${middleware}`))
})
server.timeout = socketTimeout
server.on('error', onError)
server.on('request', (req, res) => {
onRequest(req, res)
// Socket logging!
req.connection.on('close', had_error => {
debug_socket(`socket closed ${had_error ? 'with' : 'without'} error`)
}).on('connect', () => {
debug_socket('socket connected')
}).on('data', buffer => {
debug_socket('data being written')
}).on('drain', () => {
debug_socket('data drained')
}).on('end', () => {
debug_socket('FIN packet received from "other end"')
}).on('error', err => {
debug_socket(err)
}).on('timeout', () => {
debug_socket('socket timeout')
})
})
// handle Ctrl+c
process.on('SIGINT', () => {
server.close(() => {
console.log(`${server.name || 'server'} shut-down gracefully - `)
process.exit(1)
})
})
return server
}