This repository has been archived by the owner on Aug 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
202 lines (176 loc) · 4.35 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
var fs = require('fs');
var http = require('http');
var https = require('https');
var express = require('express');
var config = require('./config/config.js')();
var app = express();
/**
* Normalize a port into a number, string, or false.
*/
var server;
var server_ssl;
var data_app = {};
data_app.root = __dirname;
data_app.server = server;
data_app.server_ssl = server_ssl;
data_app.root = __dirname;
data_app.config = config;
data_app.count = 0;
var execPreApp = function ()
{
next=execPreApp;
if (data_app.count >= data_app.config.pre_app.length)
{
data_app.count = 0;
execStartServer();
}
else
{
var pre_app = require(data_app.config.pre_app[data_app.count]);
data_app.count++;
pre_app.init(app,data_app,next);
}
}
var execDatabases = function ()
{
if(!data_app.database_config_pnt)
{
data_app.database_config_pnt={};
}
next = execDatabases;
if (data_app.count >= data_app.config.databases.length)
{
data_app.count=0;
data_app.database_config_pnt={};
execInAppPreRouting();
}
else
{
var database_config = data_app.config.databases[data_app.count];
if(!app.get('databases'))
{
app.set('databases',{});
}
data_app.database_config_pnt=database_config;
var database = require(database_config.init_file);
data_app.count++;
database.init(app,data_app,next);
}
}
var execInAppPreRouting = function ()
{
next=execInAppPreRouting;
if (data_app.count >= data_app.config.in_app.pre_routing.length)
{
data_app.count = 0;
execRouting();
}
else
{
var in_app_pre = require(data_app.config.in_app.pre_routing[data_app.count]);
data_app.count++;
in_app_pre.init(app,data_app,next);
}
}
var execRouting = function ()
{
let next = execInAppPostRouting;
let router = require('./config/router.js');
router.init(app,data_app,next);
}
var execInAppPostRouting = function ()
{
next=execInAppPostRouting;
if (data_app.count >= data_app.config.in_app.post_routing.length)
{
data_app.count = 0;
}
else
{
var in_app_post = require(data_app.config.in_app.post_routing[data_app.count]);
data_app.count++;
in_app_post.init(app,data_app,next);
}
}
var execStartServer = function ()
{
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var addr = this.address();
var bind = typeof addr === 'string'
? 'Pipe ' + addr
: 'Port ' + addr.port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = this.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
console.log('Listening on ' + bind);
}
//seems to be called once
function onClose() {
data_app.count = 0;
execPostApp();
}
if(data_app.config.http)
{
data_app.server = http.createServer(app);
data_app.server.listen(data_app.config.http.port);
data_app.server.on('error', onError);
data_app.server.on('listening', onListening);
data_app.server.on('close', onClose);
}
if(data_app.config.https)
{
let options={};
for(let key in data_app.config.https.options) {
if(key == 'key' || key == 'cert') {
options[key] = fs.readFileSync(data_app.config.https.options[key]);
}
}
data_app.server_ssl = https.createServer(options,app);
data_app.server_ssl.listen(data_app.config.https.port);
data_app.server_ssl.on('error', onError);
data_app.server_ssl.on('listening', onListening);
data_app.server_ssl.on('close', onClose);
}
execDatabases();
}
var execPostApp = function ()
{
next=execPostApp;
if (data_app.count >= data_app.config.post_app.length)
{
data_app.count = 0;
process.exit();
}
else
{
var post_app = require(data_app.config.post_app[data_app.count]);
data_app.count++;
post_app.init(app,data_app,next);
}
}
execPreApp();