-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplatform.js
100 lines (87 loc) · 3.17 KB
/
platform.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
/**
* Runs the X5GON platform server
*/
// external modules
const gatsbyExpress = require("gatsby-plugin-express");
const express = require("express");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const passport = require("passport");
const flash = require("connect-flash");
const path = require("path");
// configurations
const config = require("./config/config");
// internal modules
const PostgreSQL = require("./library/postgresQL");
const pgSession = require("connect-pg-simple")(session);
const Logger = require("./library/logger");
const Monitor = require("./library/process-monitor");
// create a logger for platform requests
const logger = Logger.createGroupInstance("requests", "platform", config.isProduction);
// create process monitoring instance
const monitor = new Monitor();
const pg = new PostgreSQL(config.pg);
// create express app
let app = express();
let http = require("http").Server(app);
// add the public folder
app.use(express.static(path.join(__dirname, "/public/")));
// configure application
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
// redirect specific requests to other services
require("./routes/proxies")(app, config);
// configure cookie parser
app.use(cookieParser(config.platform.sessionSecret));
// add session configurations
if (config.isProduction) {
app.set("trust proxy", 1);
}
app.use(session({
store: new pgSession({
pool: pg._pool
}),
secret: config.platform.sessionSecret,
saveUninitialized: false,
resave: false,
cookie: {
maxAge: 30 * 24 * 60 * 60 * 1000, // 30 days
...(config.isProduction && { domain: ".x5gon.org" })
}
}));
// use flash messages
app.use(flash());
// initialize authentication
app.use(passport.initialize());
app.use(passport.session({ secret: config.platform.sessionSecret }));
// passport configuration
require("./settings/passport")(passport, pg);
// socket.io configuration
require("./settings/sockets")(http, monitor);
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
// add handlebars configurations
require("./settings/handlebars")(app);
// sets the API routes - adding the postgresql connection, logger, config file,
// passport object (for authentication), and monitoring object
require("./routes/route.handler")(app, pg, logger, config, passport, monitor);
const frontEndPath = path.join(__dirname, "/frontend");
app.use(express.static(path.join(frontEndPath, "public")));
app.use(gatsbyExpress(path.join(frontEndPath, "/gatsby-express.json"), {
publicDir: path.join(frontEndPath, "/public"),
// redirects all /path/ to /path
// should be used with gatsby-plugin-remove-trailing-slashes
redirectSlashes: true
}));
// parameters used on the express app
const PORT = config.platform.port;
// start the server without https
const server = http.listen(PORT, () => logger.info(`platform listening on port ${PORT}`));
// export the server for testing
module.exports = server;