-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
95 lines (78 loc) · 2.27 KB
/
index.js
File metadata and controls
95 lines (78 loc) · 2.27 KB
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
const Hapi = require('@hapi/hapi');
const mongoose = require('mongoose');
const config = require('config');
const Player = require('./api/models/players');
const server = new Hapi.server({
...config.get('server')
});
const jwtSecret = process.env.JWT_SECRET;
const adminUser = {
id: 1,
name: 'Admin',
username: 'admin',
password: 'e2b7101fdf5bd976f445733df1c03008',
};
exports.adminUser = adminUser;
const validate = async decoded => {
if (decoded.id !== adminUser.id) {
const user = await Player.getById(decoded.id);
if (!user || user.deleted) {
return { isValid: false };
}
}
return { isValid: !!decoded };
};
const init = async () => {
const { host, port, database, username } = config.get('mongodb');
const passwd = process.env.MONGO_PASSWORD;
const mongoUrl = new URL(`mongodb://${host}:${port}/${database}`);
mongoUrl.username = username;
mongoUrl.password = passwd;
try {
await mongoose.connect(mongoUrl.href, { useNewUrlParser: true });
}
catch (err) {
console.error(err);
}
// Need to register this alone before registering openapi,
// so the jwt security option becomes available.
await server.register(require('hapi-auth-jwt2'));
server.auth.strategy('jwt', 'jwt', {
key: jwtSecret,
validate,
verifyOptions: {
algorithms: ['HS256'],
}
});
await server.register([
{
plugin: require('hapi-openapi'),
options: {
api: './api/swagger/swagger.yaml',
handlersPath: './api/controllers',
docs: {
path: '/api-docs',
},
cors: {
origin: ['*']
},
}
},
require('inert'),
require('vision'),
{
plugin: require('hapi-swaggered-ui'),
options: {
title: 'Codebreaker API',
path: '/docs',
swaggerEndpoint: '/api/v1/api-docs',
swaggerOptions: {
docExpansion: 'list',
},
},
}
]);
await server.start();
console.log(server.info.uri);
};
init().catch(console.error);