-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
201 lines (167 loc) · 5.13 KB
/
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
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
"use strict";
var express = require("express"),
http = require('http'),
auth = require("./config/passport")(),
config = require("./config/application"),
bodyParser = require("body-parser"),
expressValidator = require('express-validator'),
auth = require("./config/passport")();
var anf = require("./anf"); // TODO: Move to globals!
var Server = (function () {
function Server(env, enforce_ssl) {
this.app = express();
this.router = express.Router();
this.env = env;
this.shuttingDown = false;
if(enforce_ssl === true) {
var enforceSSL = require("express-enforces-ssl");
this.app.enable("trust proxy");
this.app.use(enforceSSL());
}
}
Server.prototype.register_static_dir = function(dir) {
this.app.use(express.static(__dirname + dir));
};
Server.prototype.register_static_route = function(route, dir) {
this.app.use(route, express.static(__dirname + dir));
};
Server.prototype.setViewEngine = function() {
var views = require(__dirname + "/config/views").views;
if(views.enabled) {
this.app.set("view engine", views.engine || "pug");
this.app.set("view options", {
layout: views.layout || false
});
this.app.locals.pretty = views.pretty || false;
// Re-asign views directory
this.app.set("views", views.directory);
} else {
anf.log.warn("Views disabled");
}
}
Server.prototype.configure = function () {
// Set view engine
this.setViewEngine();
this.app.use(require('serve-favicon')(__dirname + '/public/favicon.png'));
// compress all requests
this.app.use(require('compression')())
// log every request to the console
this.app.use(require('morgan')('dev'));
/*this.app.use(require('express-bunyan-logger')({
immediate: true,
name: 'logger',
streams: [{
level: 'info',
stream: process.stdout
}]
}));*/
// HTTP Security Headers
if(this.env == "production") {
this.app.use(require("helmet")());
}
this.app.auth = auth;
/// Init passport auth
this.app.use(this.app.auth.initialize());
/**
this.app.utils = {
workflow: require('./utils/workflow')
};
/**/
this.app.config = config;
// Set port
this.app.set("port", config.port);
// Params validation
this.app.use(expressValidator( require("./config/validations") ));
this.app.use( bodyParser.urlencoded({ extended: true }) );
this.app.use( bodyParser.json() );
this.app.use(this.router);
/*this.app.use(function(req, res, next) {
if(this.shuttingDown) {
return;
}
next();
});*/
};
Server.prototype.setCors = function(corsFn) {
this.app.use(corsFn);
}
Server.prototype.register = function() {
// Main route
this.router.all('*', function (req, res, next) {
console.log('Someone made a request!');
next();
});
var resPath = "./config/responses";
var resList = require("fs").readdirSync(resPath);
resList.forEach(function(res) {
var resName = res.split(".")[0];
console.log("Register " + resName);
express.response[resName] = require(resPath + "/" + res);
});
};
Server.prototype.add_api_version = function(version, mod_route) {
//this.app.use(version, require(mod_route));
require(mod_route)(version, this.app);
};
Server.prototype.start = function () {
// Read dir to get all files into it.
// In this case you can read sync because this
// script just runs once ( when nodejs starts ).
var routes = require("fs").readdirSync( './config/routes/' );
var app = this.app;
// Travel all private routes files.
routes.forEach( function( routeFile ) {
if(routeFile[0] === "_") return; // Return same has continue in forEach loops
// Require and execute route module.
var namespace = routeFile.split(".")[0];
require( './config/routes/' + routeFile )( "/api/" + namespace, app );
});
//require("./config/routes/v1")("/api/v1", this.app);
//require("./config/routes/v2")("/api/v2", this.app);
require("./config/routes")(this.app);
// Handle 500
this.app.use(require('./api/controllers/ApplicationController').http500);
var self = this;
this.server = http.createServer(this.app);
this.server.listen(this.app.config.port);
this.server.on("error", function onError(error) {
if (error.syscall !== "listen") {
throw error;
}
var bind = typeof port === "string"
? "Pipe " + port
: "Port " + 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;
}
});
this.server.on("listening", function onListening() {
var addr = self.server.address();
var bind = typeof addr === "string"
? "pipe " + addr
: addr.port + " port";
console.log("Server is running at " + bind);
});
};
Server.prototype.stop = function() {
var process = require("process");
process.on('SIGINT', function() {
this.shuttingDown = true;
server.close(function(){
process.exit();
});
});
}
return Server;
}());
module.exports = Server;