-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
86 lines (69 loc) · 2.08 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
// vendor modules
const path = require('path');
const Hapi = require('hapi');
const Good = require('good');
const Inert = require('inert');
const Vision = require('vision');
const Redis = require('catbox-redis');
const Handlebars = require('handlebars');
const Auth = require('hapi-auth-cookie');
// local modules
const db = require('./database');
const routes = require('./routes');
const config = require('./configs');
const authOptions = config.auth;
const logOptions = config.logging;
// SERVER INSTANTIATION AND CONFIGURATION
const files = { relativeTo: path.join(__dirname, 'public') }; // deliver files from public dir
const server = new Hapi.Server({
port: config.port,
host: config.address,
tls: config.tls,
routes: {
files,
cors: { // TODO remove cors on prod
origin: ['*'],
credentials: true
}
},
cache: {
engine: Redis,
host: '127.0.0.1',
partition: 'cache'
}
});
// REQUEST DECORATIONS AND METHODS
// db decoration
server.decorate('request', 'db', db);
// BOOTSTRAP AND START SERVER
async function bootstrap () {
// static file serving
await server.register({ plugin: Inert });
// HTML Templating
await server.register({ plugin: Vision });
server.views({
engines: { html: Handlebars },
relativeTo: __dirname,
path: './templates',
layout: true,
layoutPath: './templates/layout',
partialsPath: './templates/partials',
helpersPath: './templates/helpers',
isCached: false
});
// logging
await server.register({ plugin: Good, options: logOptions });
// session caching (30 days)
// TODO: double check cache expiresIn - use redis instead to persist session forever
server.app.cache = server.cache({ segment: 'sessions', expiresIn: 2147483647 });
// auth strategy
await server.register({ plugin: Auth });
authOptions(server);
// routes (must come after inert)
server.route(routes);
// start server and print
await server.start();
console.log(`Server running at: ${server.info.uri}`);
}
try { bootstrap(); }
catch (err) { console.log('Failed to bootstrap server: ', err); }