-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.ts
52 lines (44 loc) · 1.42 KB
/
app.ts
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
import * as Hapi from 'hapi';
import { BaseRoute } from './src/routes/base/BaseRoute';
const inert = require('inert'); // Usado para exposição de arquivos estáticos.
const vision = require('vision'); // Usado para administração de template engines
const hapiJwt = require('hapi-auth-jwt2');
const hapiSwagger = require('hapi-swagger');
const validate = (decoded: any, request: any, callback: any) => {
if (decoded) return callback(null, true);
return callback(null, false);
};
async function startApi(): Promise<Hapi.Server> {
try {
// Instancia o HapiJS
const server = new Hapi.Server();
await server.connection({ port: process.env.PORT || 3000 });
// register plugins
await server.register([
inert,
vision,
{
register: hapiSwagger,
options: {
info: { title: 'Nome do Projeto', version: '0.1' },
},
},
hapiJwt,
]);
// Autenticação JWT
await server.auth.strategy('jwt', 'jwt', {
key: process.env.JWT_SECRET,
validateFunc: validate,
verifyOptions: { algorithms: ['HS256'] },
});
await server.route(BaseRoute.routes());
await server.start();
const uri = server.info ? server.info.uri : 0;
console.info(`SERVIDOR EXECUTANDO: ${uri}`);
return server;
} catch (error) {
console.error(`Falha na inicialização da API: ${error}`);
return new Hapi.Server();
}
}
export default startApi;