-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
75 lines (62 loc) · 1.6 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
import express from 'express';
import * as http from 'http';
import * as dotenv from 'dotenv';
import swaggerUi from 'swagger-ui-express';
import swaggerJSDoc from 'swagger-jsdoc';
import logger from './src/middleware/logger';
let cors = require('cors');
// Router imports here
import {v1Router} from './src/models/v1/v1.router';
// dotenv
dotenv.config();
// Check for correct env file loading
if (!process.env.PORT) {
logger.error('No port');
process.exit(1);
}
// Initialize express application
const port: number = parseInt(process.env.PORT, 10);
const app: express.Application = express();
const server: http.Server = http.createServer(app);
// Middleware to parse incoming requests as json
app.use(express.json());
// Enable CORS
app.use(cors());
// Swagger documentation
const swaggerDefinition = {
openapi: '3.0.0',
info: {
title: 'what2ride api',
version: '1.0.0',
license: {
name: 'Licensed Under MIT',
url: 'https://spdx.org/licenses/MIT.html'
},
contact: {
name: 'Patrick Müller',
url: 'https://what2ride.info'
}
}
};
const options = {
swaggerDefinition,
// Paths to files containing OpenAPI definitions
apis: [
'./src/models/**/*.router.ts'
]
};
const swaggerSpec = swaggerJSDoc(options);
app.use(
'/docs',
swaggerUi.serve,
swaggerUi.setup(swaggerSpec)
);
// Add routers here
app.use('/v1', v1Router);
// this is a simple route to make sure everything is working properly
app.get('/', (req: express.Request, res: express.Response) => {
res.status(200).send('Welcome to the what2ride API!');
});
server.listen(port, () => {
logger.info('Server listening on Port ' + port);
});