-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.ts
171 lines (149 loc) · 4.89 KB
/
app.ts
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
///<reference path="typings/tsd.d.ts" />
import express = require("express");
import http = require("http");
import path = require("path");
import middlewareLogger = require("morgan");
import methodOverride = require("method-override");
import session = require("express-session");
import bodyParser = require("body-parser");
import errorHandler = require("errorhandler");
import _ = require("lodash");
import moduleManager = require("./modules/backend/moduleManager");
import utils = require("mykoop-utils");
import frontendCompilation = require("./modules/frontend/index");
import getModulesDefinitions = require("./modules/backend/getModulesDefinitions");
var SessionStore = require("express-mysql-session");
var logger = utils.getLogger(module);
// Define global variables to ensure coherence between backend and frontend
process.__PROD__ = utils.__PROD__;
__PROD__ = utils.__PROD__;
process.__DEV__ = utils.__DEV__;
__DEV__ = utils.__DEV__;
//hijack require to parse json5
require("json5/lib/require");
var configs = require("./modules/common/mykoop-config.json5");
var sessionStore;
try {
var connectionInfo = require("dbConfig.json5");
sessionStore = new SessionStore({
host: connectionInfo.host,
port: connectionInfo.port,
user: connectionInfo.user,
password: connectionInfo.password,
database: connectionInfo.database,
useConnectionPooling: true
});
} catch (e) {
logger.warn(
"Unable to find Database configuration [dbConfig.json5].\
Will use in-memory sessions."
, e);
}
var favicon = require("serve-favicon");
var multer = require("multer");
var app = express();
// Setting core modules
import router = require("./modules/backend/router");
moduleManager.setCore("router", new router.Router(app));
// Loading modules
logger.info("Loading modules...");
var modulesDefinitions = getModulesDefinitions({
excludes: configs.mykoopModuleExcludeList,
searchNodeModules: configs.mykoopLoadModuleFromNode,
path: path.resolve(configs.mykoopModulesList || "")
});
moduleManager.loadModules(modulesDefinitions);
app.disable("etag");
// all environments
app.set("port", process.env.PORT || 1337);
app.use(favicon(__dirname + "/public/favicon.ico"));
app.use(middlewareLogger("dev"));
app.options("*", function(req, res, next) {
var headers = {};
// IE8 does not allow domains to be specified, just the *
// headers["Access-Control-Allow-Origin"] = req.headers.origin;
headers["Access-Control-Allow-Origin"] = "*";
headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS";
headers["Access-Control-Allow-Credentials"] = false;
headers["Access-Control-Max-Age"] = '86400'; // 24 hours
headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept";
res.writeHead(200, headers);
res.end();
});
app.use(function(req, res, next) {
res.set("Access-Control-Allow-Origin", "*");
next();
});
// Frontend routes
import routes = require("./routes/index");
routes(app);
app.use(methodOverride());
app.use(session({
resave: true,
saveUninitialized: true,
//FIXME: Get this out of GitHub. / Make this configurable.
secret: "8bb6b8987c0e3244e30690cb9baf4d0a7085491f",
store: sessionStore || undefined
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(multer());
// development only
if (utils.__DEV__) {
app.use(errorHandler());
app.use(express.static(path.join(__dirname, "public")));
}
var loggingError: any = [
/*0*/_.noop,
/*1*/_.noop,
/*2*/_.noop,
/*3*/_.noop,
/*4*/_.bind(logger.verbose, logger),
/*5*/_.bind(logger.error, logger)
]
function errorResponse(err, status) {
status = status || err.statusCode || 500;
loggingError[Math.floor(status/100)](err);
this.status(status);
if(!err) {
return this.end();
}
if(err instanceof utils.errors) {
return this.send(err.serialize());
}
if(_.isFunction(err.toString)) {
return this.send(err.toString());
}
this.send(err);
}
app.use(function (req, res, next) {
res.error = errorResponse;
next();
});
//FIXME: Allow the modules to attach helpers to res and req through the API so
// the user module can do this without us knowing...
var validatePermissions = require("mykoop-user/lib/common/validatePermissions");
app.use(function (req, res, next) {
req.userHasPermissions = function(permissions) {
return true;
if (!_.isPlainObject(permissions)) {
logger.warn(
"You need to call req.userHasPermissions with an object literal."
);
return true;
}
if (_.isEmpty(permissions)) {
return true;
}
if (!req.session.user) {
return false;
}
return validatePermissions(req.session.user.perms, permissions);
};
next();
});
// Initialise module and add backend routes
moduleManager.initializeLoadedModules();
http.createServer(app).listen(app.get("port"), function () {
logger.info("Express server listening on port " + app.get("port"));
});