-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
150 lines (121 loc) · 4.92 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
//----------------------------------------------------------------------------------------------------------------------
// Main server module for Tome.
//----------------------------------------------------------------------------------------------------------------------
// Config
const { config } = require('./server/api/managers/config');
// Logging
const logging = require('trivial-logging');
logging.init(config);
logging.setRootLogger('tome server');
const logger = logging.loggerFor(module);
//----------------------------------------------------------------------------------------------------------------------
const _ = require('lodash');
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const KnexSessionStore = require('connect-session-knex')(session);
const passport = require('passport');
// Managers
const dbMan = require('./server/database');
// Auth
const GoogleAuth = require('./server/auth/google');
// Routes
const accountsRoute = require('./server/routes/accounts');
const commentRoute = require('./server/routes/comments');
const historyRoute = require('./server/routes/history');
const rolesRoute = require('./server/routes/roles');
const recentRoute = require('./server/routes/recent');
const searchRoute = require('./server/routes/search');
const wikiRoute = require('./server/routes/wiki');
const routeUtils = require('./server/routes/utils');
//----------------------------------------------------------------------------------------------------------------------
// Build the express app
const app = express();
// Basic request logging
app.use(routeUtils.requestLogger(logger));
// Basic error logging
app.use(routeUtils.errorLogger(logger));
// Session support
app.use(cookieParser());
app.use(bodyParser.json());
// Let the database get setup first
const loading = dbMan.getDB()
.then((db) =>
{
const store = new KnexSessionStore({
sidfieldname: config.key || 'tome_session',
knex: db,
createTable: true
});
app.use(session({
secret: config.secret || 'nosecret',
key: config.key || 'tome_session',
resave: false,
store,
// maxAge = 7 days
cookie: { maxAge: 7 * 24 * 60 * 60 * 1000, secure: !config.debug },
saveUninitialized: false
}));
// Passport support
app.use(passport.initialize());
app.use(passport.session());
GoogleAuth.initialize(app);
// Auth override
if(config.overrideAuth)
{
// Middleware to skip authentication, for unit testing. We only allow this if we're in debug mode /and/ we've set
// the user on `app`, something that can't be done externally.
app.use((req, resp, next) =>
{
const user = app.get('user');
req.user = _.isUndefined(user) ? req.user : user;
next();
});
} // end if
// Setup static serving
app.use(express.static(path.resolve('./dist')));
app.use('/assets', express.static(path.resolve('./assets')));
// Set up our application routes
app.use('/account', accountsRoute);
app.use('/comment', commentRoute);
app.use('/history', historyRoute);
app.use('/recent', recentRoute);
app.use('/roles', rolesRoute);
app.use('/search', searchRoute);
app.use('/wiki', wikiRoute);
// Serve index.html for any html requests, but 404 everything else.
app.get('*', (request, response) =>
{
response.format({
html: routeUtils.serveIndex,
json: (request, response) =>
{
response.status(404)
.json({
name: 'NotFoundError',
message: `Requested url '${ request.url }' not found`,
code: 'ERR_NOT_FOUND'
});
}
});
});
// Start the server
const server = app.listen(config.http.port, () =>
{
const { address, port } = server.address();
const version = require('./package').version;
const host = address === '::' ? 'localhost' : address;
logger.info(`Tome v${ version } listening at http://${ host }:${ port }.`);
});
return server;
})
.tap((/* server */) =>
{
// Setup our websocket handling
// socketMan.setServer(io(server));
});
//----------------------------------------------------------------------------------------------------------------------
module.exports = { app, loading };
//----------------------------------------------------------------------------------------------------------------------