2021-02-16 21:55:50 +00:00
|
|
|
import express from 'express';
|
|
|
|
import * as http from 'http';
|
2021-04-20 07:40:15 +00:00
|
|
|
import * as dotenv from 'dotenv';
|
2022-01-09 15:19:49 +00:00
|
|
|
import swaggerUi from 'swagger-ui-express';
|
|
|
|
import swaggerJSDoc from 'swagger-jsdoc';
|
2021-08-20 11:26:38 +00:00
|
|
|
// Router imports
|
|
|
|
import {partyPlanerRouter} from './src/models/partyplaner/PartyPlaner.router';
|
|
|
|
import {highlightMarkerRouter} from './src/models/twitch-highlight-marker/HighlightMarker.router';
|
|
|
|
import {dhbwServiceRouter} from './src/models/dhbw-service/DHBWService.router';
|
2021-09-20 20:17:52 +00:00
|
|
|
import logger from './src/middleware/logger';
|
|
|
|
import {dhbwRaPlaChangesRouter} from './src/models/dhbw-rapla-changes/DHBWRaPlaChanges.router';
|
2021-10-07 11:06:51 +00:00
|
|
|
import {raPlaMiddlewareRouter} from './src/models/rapla-middleware/RaPlaMiddleware.router';
|
2021-11-11 10:52:27 +00:00
|
|
|
import {betterzonRouter} from './src/models/betterzon/Betterzon.router';
|
2022-01-08 22:21:19 +00:00
|
|
|
import {crrRouter} from './src/models/climbing-route-rating/ClimbingRouteRating.router';
|
2021-04-20 07:40:15 +00:00
|
|
|
|
2021-09-29 10:55:08 +00:00
|
|
|
let cors = require('cors');
|
|
|
|
|
2021-04-20 07:40:15 +00:00
|
|
|
dotenv.config();
|
|
|
|
|
|
|
|
if (!process.env.PORT) {
|
2022-06-25 11:26:16 +00:00
|
|
|
logger.error('No port');
|
|
|
|
process.exit(1);
|
2021-04-20 07:40:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 11:09:34 +00:00
|
|
|
const port: number = parseInt(process.env.PORT, 10);
|
2021-02-16 21:55:50 +00:00
|
|
|
|
|
|
|
const app: express.Application = express();
|
|
|
|
const server: http.Server = http.createServer(app);
|
|
|
|
|
|
|
|
// here we are adding middleware to parse all incoming requests as JSON
|
2021-08-20 11:26:38 +00:00
|
|
|
app.use(express.json());
|
2021-02-16 21:55:50 +00:00
|
|
|
|
2022-06-25 11:26:16 +00:00
|
|
|
// Configure CORS
|
|
|
|
let allowedHosts = [
|
|
|
|
'https://rapla.p4ddy.com',
|
|
|
|
'https://betterzon.p4ddy.com'
|
|
|
|
];
|
|
|
|
app.use(cors({
|
|
|
|
origin: function (origin: any, callback: any) {
|
|
|
|
// Allow requests with no origin
|
|
|
|
if (!origin) return callback(null, true);
|
|
|
|
|
|
|
|
// Block requests with wrong origin
|
|
|
|
if (allowedHosts.indexOf(origin) === -1) {
|
|
|
|
return callback(new Error('The CORS policy doesn\'t allow access for your origin.'), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow all other requests
|
|
|
|
return callback(null, true);
|
|
|
|
}
|
|
|
|
}));
|
2021-09-29 10:55:08 +00:00
|
|
|
|
2022-01-09 15:19:49 +00:00
|
|
|
// Swagger documentation
|
|
|
|
const swaggerDefinition = {
|
2022-06-25 11:26:16 +00:00
|
|
|
openapi: '3.0.0',
|
|
|
|
info: {
|
|
|
|
title: 'Pluto Development REST API',
|
|
|
|
version: '2.0.0',
|
|
|
|
license: {
|
|
|
|
name: 'Licensed Under MIT',
|
|
|
|
url: 'https://spdx.org/licenses/MIT.html'
|
|
|
|
},
|
|
|
|
contact: {
|
|
|
|
name: 'Pluto Development',
|
|
|
|
url: 'https://www.pluto-development.de'
|
|
|
|
}
|
|
|
|
}
|
2022-01-09 15:19:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const options = {
|
2022-06-25 11:26:16 +00:00
|
|
|
swaggerDefinition,
|
|
|
|
// Paths to files containing OpenAPI definitions
|
|
|
|
apis: [
|
|
|
|
'./src/models/**/*.router.ts'
|
|
|
|
]
|
2022-01-09 15:19:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const swaggerSpec = swaggerJSDoc(options);
|
|
|
|
|
|
|
|
app.use(
|
2022-06-25 11:26:16 +00:00
|
|
|
'/docs',
|
|
|
|
swaggerUi.serve,
|
|
|
|
swaggerUi.setup(swaggerSpec)
|
2022-01-09 15:19:49 +00:00
|
|
|
);
|
|
|
|
|
2021-08-18 11:09:34 +00:00
|
|
|
// Add routers
|
2021-08-18 12:02:59 +00:00
|
|
|
app.use('/dhbw-service', dhbwServiceRouter);
|
|
|
|
app.use('/twitch-highlight-marker', highlightMarkerRouter);
|
|
|
|
app.use('/partyplaner', partyPlanerRouter);
|
2021-09-20 20:17:52 +00:00
|
|
|
app.use('/raplachanges', dhbwRaPlaChangesRouter);
|
2021-10-07 11:06:51 +00:00
|
|
|
app.use('/rapla-middleware', raPlaMiddlewareRouter);
|
2021-11-11 10:52:27 +00:00
|
|
|
app.use('/betterzon', betterzonRouter);
|
2022-01-08 22:21:19 +00:00
|
|
|
app.use('/crr', crrRouter);
|
2021-02-16 21:55:50 +00:00
|
|
|
|
|
|
|
// this is a simple route to make sure everything is working properly
|
|
|
|
app.get('/', (req: express.Request, res: express.Response) => {
|
2022-06-25 11:26:16 +00:00
|
|
|
res.status(200).send('Welcome to the Pluto Development REST API V2!');
|
2021-02-16 21:55:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(port, () => {
|
2022-06-25 11:26:16 +00:00
|
|
|
logger.info('Server listening on Port ' + port);
|
2021-02-16 21:55:50 +00:00
|
|
|
});
|