-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
154 lines (131 loc) · 4.01 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
/**
* @module App
*
* @description
* App serves as the entry point for starting the application.
*/
"use strict";
var bodyParser = require("body-parser");
var conf = require("config");
var Log = require("node-android-logging");
if (conf.has("loglevel")) {
Log.setDefaults();
Log.disableStdout();
Log.enableStderr(conf.get("loglevel"));
}
var express = require("express")();
var fs = require("fs");
var Mysqlc = require("./Mysqlc");
var DbUpgrade = require("./DbUpgrade");
var swaggerInit = require("swagger-tools").initializeMiddleware;
// Mixes in _.coalesce
require("./util/coalesce");
module.exports = express; // for testing
/* Access and error logs */
if (process.env.APACHE_LOGS !== undefined) {
express.use(require("morgan")("combined"));
}
express.use(bodyParser.json());
express.use(bodyParser.urlencoded({ extended: true }));
/* */
express.all("/*", function(req, res, next) {
let root = __dirname + "/node_modules/eledger-web";
/* The default action is to send the index.html page and let Angular handle routing.
*
* The other actions include:
* - Swagger API endpoint
* - Static file endpoint
*/
var defaultAction = function() {
res.sendFile("index.html", {
root: root
});
};
/* The Swagger API endpoints all start with /api
*/
if (req.path.startsWith("/api")) {
/** I really don't want the browser to cache these api calls. **/
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Pragma", "no-cache");
res.setHeader("Expires", 0);
res.setHeader("Last-Modified", (new Date()).toUTCString());
/* Since we've established that this is not an angular endpoint, allow the next middleware to process.
*/
next();
} else if (req.path.startsWith("/static-stored-files")) {
res.sendFile(__dirname + req.path);
} else {
/* If the user is requesting one of the Angular routes, we'll send them the index.html file since Angular
* handles it from there.
*
* In order to avoid hard coding the Angular routes into this program as well, we look to see if a static
* file exists at the path they're requesting (within the root directory ./node_modules/eledger-web).
*
* If the file does exist at that path, they aren't requesting an API endpoint.
* If no file exists at that path, they are requesting an Angular2 API endpoint.
*/
fs.lstat(root + req.path, function(err, stats) {
if (err || !stats.isFile()) {
fs.lstat(__dirname + req.path, function(err, stats) {
if (err || ! stats.isFile()) {
defaultAction();
} else {
if (req.path.startsWith("/node_modules")) {
res.sendFile(__dirname + req.path);
} else {
defaultAction();
}
}
});
} else {
res.sendFile(root + req.path);
}
});
}
});
/* Select a port */
var port;
if (conf.has("express.port")) {
port = conf.get("express.port");
}
/**
* Order prefers:
* - process.env.PORT
* - config/**.json#port
* - 4443
*/
port = process.env.PORT || port || 4443;
/*
* Prepare DB
*/
try {
Mysqlc.connect();
DbUpgrade.initializeModels();
DbUpgrade.setupQueries();
DbUpgrade.upgrade().then(function() {
DbUpgrade.finalizeModels();
}).catch(function(rejection) {
setTimeout(function() {
if (rejection !== undefined && rejection.stack !== undefined) {
throw rejection;
} else {
Log.E(rejection);
throw new Error(rejection);
}
});
});
} catch (ex) {
Log.E(ex);
process.exit(1);
}
var sourceFile = "./api/swagger/index.json";
swaggerInit(require(sourceFile), function (swaggerTools) {
express.listen(port);
express.use(swaggerTools.swaggerMetadata());
express.use(swaggerTools.swaggerValidator());
express.use(swaggerTools.swaggerRouter({
"controllers": "api/controllers"
}));
express.use(swaggerTools.swaggerUi());
Log.I("Listening on port " + port);
});