-
Notifications
You must be signed in to change notification settings - Fork 100
/
index.js
99 lines (86 loc) · 2.4 KB
/
index.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
const Hapi = require('@hapi/hapi')
require('dotenv').config()
const plugins = require('./plugins')
const routes = require('./routes')
const { logger } = require('./util/logger')
const { init: usersInit } = require('./lib/data/users')
const { init: queueInit } = require('./lib/data/lib/sqs.listener')
const options = {
port: 3000,
// // Commented out until Elasticache is configured
// cache: [{
// name: 'redis',
// provider: {
// constructor: require('@hapi/catbox-redis'),
// options: {
// partition: 'cache',
// host: 'your elasticache domain here',
// }
// }
// }]
}
const init = async () => {
const server = Hapi.Server(options)
await server.register(plugins)
// hapi-auth-cookie stuff
const cache = server.cache({
// cache: 'redis',
segment: 'sessions',
expiresIn: 3 * 24 * 60 * 60 * 1000
})
server.app.cache = cache
server.ext('onPreHandler', (req, h) => {
req.info.acceptEncoding = null
return h.continue
})
server.auth.strategy('session', 'cookie', {
cookie: {
isSecure: false,
name: 'hbfl-sid',
password: 'password-should-be-32-characters'
},
validateFunc: async (request, session) => {
const cached = await cache.get(session.sid)
if (!cached) {
return { valid: false }
}
return {
credentials: cached.account,
valid: true
}
}
})
server.auth.default('session')
// register routes
server.route(routes)
// logging
server.events.on('log', (_, event) => {
if (event.error) {
logger.error(`Server error: ${event.error.message || 'unknown'}`);
} else {
logger.info(`Server event: ${event}`)
}
})
server.events.on('request', (_, event) => {
if (event.tags.includes('unauthenticated')) return
if (event.tags.includes('error')) {
logger.error(`Request error: ${event.data || event.error.message || 'unknown'}`);
} else {
logger.info(`Request event: ${event}`)
}
})
// initialize database and start server
usersInit()
// Commented out until SQS is configured
// .then(() => queueInit())
.then(async () => {
try {
await server.start()
console.log(`Server started at http://localhost:${server.info.port}`)
} catch (err) {
console.error(`Server could not start. Error: ${err}`)
logger.error(`Server could not start. Error: ${err}`)
}
})
}
init()