feat: major code refactor, add login and register endpoints with swagger
All checks were successful
Update changelog / changelog (push) Successful in 27s
All checks were successful
Update changelog / changelog (push) Successful in 27s
This commit is contained in:
103
src/app.ts
103
src/app.ts
@@ -1,42 +1,79 @@
|
||||
import * as express from 'express';
|
||||
import router from './routes';
|
||||
import * as dotenv from "dotenv";
|
||||
import * as dotenv from 'dotenv';
|
||||
dotenv.config({ quiet: true });
|
||||
|
||||
const app = express();
|
||||
import express from 'express';
|
||||
import { version } from '../package.json';
|
||||
import miscRouter from './routes/miscRoutes';
|
||||
import userRouter from './routes/userRoutes';
|
||||
import { AppDataSource } from './data-source'
|
||||
import inferUser from './middleware/inferUser';
|
||||
|
||||
const swaggerJsdocOpts = {
|
||||
failOnErrors: true,
|
||||
definition: {
|
||||
openapi: '3.0.0',
|
||||
info: {
|
||||
title: 'kittyurl',
|
||||
version: '0.0.1'
|
||||
}
|
||||
},
|
||||
apis: ['./src/routes/*.ts']
|
||||
};
|
||||
const swaggerUi = require('swagger-ui-express');
|
||||
const swaggerJsdoc = require('swagger-jsdoc');
|
||||
const swaggerSpec = swaggerJsdoc(swaggerJsdocOpts);
|
||||
AppDataSource.initialize().then(async () => {
|
||||
|
||||
app.use(express.json());
|
||||
app.use(router);
|
||||
if (process.env.DEBUG === "true") {
|
||||
app.use('/swagger', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
|
||||
}
|
||||
await AppDataSource.runMigrations();
|
||||
|
||||
// Handle 404s
|
||||
// https://stackoverflow.com/a/9802006
|
||||
app.use(function(req, res) {
|
||||
res.status(404);
|
||||
const app: express.Express = express();
|
||||
|
||||
if (req.accepts('json')) {
|
||||
res.json({ status: 'error', error: 'Not found' });
|
||||
return;
|
||||
app.use(express.json());
|
||||
app.use(inferUser);
|
||||
app.use(miscRouter, userRouter);
|
||||
|
||||
if (process.env['DEBUG'] === 'true') {
|
||||
const swaggerJsdocOpts = {
|
||||
failOnErrors: true,
|
||||
definition: {
|
||||
openapi: '3.0.4',
|
||||
info: {
|
||||
title: 'kittyurl API',
|
||||
description: 'A Typescript API for the kittyurl url shortener project.',
|
||||
version: version,
|
||||
contact: {
|
||||
name: 'Git repository for entire project',
|
||||
url: 'https://gitea.7o7.cx/kittyteam/kittyurl'
|
||||
},
|
||||
license: {
|
||||
name: 'AGPLv3',
|
||||
url: 'https://www.gnu.org/licenses/agpl-3.0.en.html'
|
||||
}
|
||||
},
|
||||
components: {
|
||||
securitySchemes: {
|
||||
BearerJWT: {
|
||||
type: 'http',
|
||||
scheme: 'bearer',
|
||||
bearerFormat: 'JWT',
|
||||
description: 'JWT Authorization header using the Bearer scheme.<br/>Enter your JWT from /api/v1/user/signIn to authorize.'
|
||||
}
|
||||
}
|
||||
},
|
||||
security: [
|
||||
{ bearerAuth: [] }
|
||||
],
|
||||
},
|
||||
apis: ['./src/routes/*.ts', './src/schemas/*.ts']
|
||||
};
|
||||
const swaggerUi = require('swagger-ui-express');
|
||||
const swaggerJsdoc = require('swagger-jsdoc');
|
||||
const swaggerSpec = swaggerJsdoc(swaggerJsdocOpts);
|
||||
app.use('/kttydocs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
|
||||
app.get('/kttydocs.json', (req: express.Request, res: express.Response) => {
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.send(swaggerSpec);
|
||||
});
|
||||
}
|
||||
|
||||
res.type('txt').send('Not found');
|
||||
});
|
||||
app.listen(6567, () => console.log('(HTTP Server) Listening on port 6567.'));
|
||||
// Handle 404s
|
||||
// https://stackoverflow.com/a/9802006
|
||||
app.use(function(req: express.Request, res: express.Response) {
|
||||
res.status(404);
|
||||
|
||||
if (req.accepts('json')) {
|
||||
res.json({ status: 'error', error: 'Not found' });
|
||||
return;
|
||||
}
|
||||
|
||||
res.type('txt').send('Not found');
|
||||
});
|
||||
app.listen(6567, () => console.log('(HTTP Server) Listening on port 6567.'));
|
||||
|
||||
}).catch(error => console.log(error))
|
||||
|
||||
Reference in New Issue
Block a user