feat(backend): Setup Swagger
Setup algemene config en pad
This commit is contained in:
parent
3cd7496989
commit
41dcb57b25
4 changed files with 480 additions and 8 deletions
|
@ -25,6 +25,8 @@
|
|||
"js-yaml": "^4.1.0",
|
||||
"loki-logger-ts": "^1.0.2",
|
||||
"response-time": "^2.3.3",
|
||||
"swagger-jsdoc": "^6.2.8",
|
||||
"swagger-ui-express": "^5.0.1",
|
||||
"uuid": "^11.1.0",
|
||||
"winston": "^3.17.0",
|
||||
"winston-loki": "^6.1.3"
|
||||
|
@ -34,6 +36,8 @@
|
|||
"@types/express": "^5.0.0",
|
||||
"@types/node": "^22.13.4",
|
||||
"@types/response-time": "^2.3.8",
|
||||
"@types/swagger-jsdoc": "^6.0.4",
|
||||
"@types/swagger-ui-express": "^4.1.8",
|
||||
"globals": "^15.15.0",
|
||||
"ts-node": "^10.9.2",
|
||||
"tsx": "^4.19.3",
|
||||
|
|
|
@ -16,6 +16,8 @@ import { getLogger, Logger } from './logging/initalize.js';
|
|||
import { responseTimeLogger } from './logging/responseTimeLogger.js';
|
||||
import responseTime from 'response-time';
|
||||
import { EnvVars, getNumericEnvVar } from './util/envvars.js';
|
||||
import swaggerUi from 'swagger-ui-express';
|
||||
import swaggerMiddleware from './swagger';
|
||||
|
||||
const logger: Logger = getLogger();
|
||||
|
||||
|
@ -23,7 +25,6 @@ const app: Express = express();
|
|||
const port: string | number = getNumericEnvVar(EnvVars.Port);
|
||||
|
||||
app.use(express.json());
|
||||
app.use(responseTime(responseTimeLogger));
|
||||
|
||||
// TODO Replace with Express routes
|
||||
app.get('/', (_, res: Response) => {
|
||||
|
@ -33,6 +34,7 @@ app.get('/', (_, res: Response) => {
|
|||
});
|
||||
});
|
||||
|
||||
// Routes
|
||||
app.use('/student', studentRouter);
|
||||
app.use('/group', groupRouter);
|
||||
app.use('/assignment', assignmentRouter);
|
||||
|
@ -45,6 +47,12 @@ app.use('/theme', themeRoutes);
|
|||
app.use('/learningPath', learningPathRoutes);
|
||||
app.use('/learningObject', learningObjectRoutes);
|
||||
|
||||
// Add response time logging
|
||||
app.use(responseTime(responseTimeLogger));
|
||||
|
||||
// Swagger UI for API documentation
|
||||
app.use('/api-docs', swaggerUi.serve, swaggerMiddleware);
|
||||
|
||||
async function startServer() {
|
||||
await initORM();
|
||||
|
||||
|
|
36
backend/src/swagger.ts
Normal file
36
backend/src/swagger.ts
Normal file
|
@ -0,0 +1,36 @@
|
|||
import swaggerJSDoc from 'swagger-jsdoc';
|
||||
import { RequestHandler } from 'express';
|
||||
import swaggerUi from 'swagger-ui-express';
|
||||
|
||||
const swaggerJSDocOptions: swaggerJSDoc.Options = {
|
||||
definition: {
|
||||
openapi: '3.1.0',
|
||||
info: {
|
||||
title: 'Dwengo-1 Backend API',
|
||||
version: '0.1.0',
|
||||
description: 'Dwengo-1 Backend API Using Express, based on VZW Dwengo',
|
||||
license: {
|
||||
'name': 'MIT',
|
||||
'url': 'https://github.com/SELab-2/Dwengo-1/blob/336496ab6352ee3f8bf47490c90b5cf81526cef6/LICENSE'
|
||||
}
|
||||
},
|
||||
servers: [
|
||||
{
|
||||
url: 'http://localhost:3000',
|
||||
},
|
||||
{
|
||||
url: 'https://sel2-1.ugent.be/api'
|
||||
}
|
||||
]
|
||||
},
|
||||
apis: [ './src/routes/*.ts', './src/entities/**/*.ts' ]
|
||||
};
|
||||
|
||||
const swaggerOptions = {
|
||||
explorer: true
|
||||
};
|
||||
const swaggerDocument = swaggerJSDoc(swaggerJSDocOptions);
|
||||
|
||||
const swaggerMiddleware: RequestHandler = swaggerUi.setup(swaggerDocument, swaggerOptions);
|
||||
|
||||
export default swaggerMiddleware;
|
Loading…
Add table
Add a link
Reference in a new issue