-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
92 lines (82 loc) · 2.65 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
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
import * as dotenv from 'dotenv';
dotenv.config();
import { environment } from '~/../environment/environment';
import swaggerJsdoc from 'swagger-jsdoc';
import swaggerUiExpress, { SwaggerOptions } from 'swagger-ui-express';
import cors from 'cors';
import express, { Express, json } from 'express';
import { dbProvider } from '~/providers/db.provider';
import { router as publicRouter } from '~/router/public.router';
import { router as privateRouter } from '~/router/private.router';
import { Logger } from '~/lib/logger';
import { authMiddleware } from '~/middlewares/auth.middleware';
import { morganMiddleware } from '~/middlewares/morgan.middleware';
import { IDbParameters } from './environment/environment.type';
// Setup REST server
export const app: Express = express();
const swaggerOptions: SwaggerOptions = {
swaggerDefinition: {
openapi: '3.0.1',
info: {
title: 'Martian robots - Mars exploration REST API',
description: 'Backend for a Mars exploration experience with robots.',
contact: {
name: 'Pablo Eliseo',
url: 'https://github.com/pabloelisseo',
},
},
servers: [
// {
// url: 'https://',
// description: 'Production',
// },
{
url: `http://localhost:${environment.api.port}`,
description: 'Development',
},
],
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
},
security: [{
bearerAuth: [],
}],
},
apis: [
'**/*.ts',
],
};
const swaggerDocs = swaggerJsdoc(swaggerOptions);
app.use('/api-docs', swaggerUiExpress.serve, swaggerUiExpress.setup(swaggerDocs));
app.use(cors());
app.use(json());
app.use(morganMiddleware);
app.use('/', publicRouter);
app.use(authMiddleware);
app.use('/', privateRouter);
export async function instance(connectionParameters: IDbParameters | string): Promise<void> {
await dbProvider.instance(connectionParameters);
}
export async function closeInstance(): Promise<void> {
await dbProvider.disconnectInstance();
}
(async () => {
try {
if (environment.autoInstance) {
await instance(environment.db);
app.listen(environment.api.port, (): void => {
Logger.info(`App listening at http://localhost:${environment.api.port}`);
});
}
}
catch (err) {
Logger.error(err);
closeInstance();
}
})();