Added API

This commit is contained in:
2022-08-26 06:36:45 +07:00
parent 56fbe3109c
commit 96bba465d9
8 changed files with 156 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
import HTTP_STATUS from "../../../libs/HTTPStatus";
import Environment from "../../../providers/Environment";
import App from "../../../providers/App";
class HealthCheck {
public static async perform(req: any, res: any, next: any) {
Promise.resolve().then(async () => {
return res.status(HTTP_STATUS.OK).json({
status: "OK",
environment: Environment.get().NODE_ENV,
version: App.versionNumber,
uptime: process.uptime(),
memoryUsage: process.memoryUsage(),
cpuUsage: process.cpuUsage(),
pid: process.pid,
ppid: process.ppid
});
}).catch(next);
}
}
export default HealthCheck;
+21
View File
@@ -0,0 +1,21 @@
import HTTP_STATUS from "../libs/HTTPStatus";
import Logger from "../libs/Logger";
import { ServiceError } from "./Errors";
class ExpressExceptionHandler {
public static errorLogger(error: any, req: any, res: any, next: any) {
if(error instanceof ServiceError) {
res.status(error.statusCode).json({
error: error.message
})
}
else {
res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({
error: "Internal server error"
})
throw error;
}
}
}
export default ExpressExceptionHandler;
+1
View File
@@ -9,5 +9,6 @@ App.loadPrisma();
App.loadLocale(); App.loadLocale();
App.loadDiscord(); App.loadDiscord();
App.load_osu(); App.load_osu();
App.loadExpress();
export default App; export default App;
+11
View File
@@ -3,6 +3,7 @@ import Configuration from './Configuration';
import Prisma from './Prisma'; import Prisma from './Prisma';
import Discord from './Discord'; import Discord from './Discord';
import osu from './osuAPI'; import osu from './osuAPI';
import Express from './Express';
import Logger from '../libs/Logger'; import Logger from '../libs/Logger';
import Locale from './Locale'; import Locale from './Locale';
@@ -41,6 +42,16 @@ class App {
Logger.log('info', 'Loading osu! Client'); Logger.log('info', 'Loading osu! Client');
osu.init(); osu.init();
} }
public loadExpress(): void {
Logger.log('info', 'Loading Express');
Express.init();
}
public endExpress(): void {
Logger.log('info', 'Ending Express');
Express.end();
}
} }
export default new App(); export default new App();
+6
View File
@@ -31,6 +31,9 @@ class Environment {
const PRIVATE_BOT = process.env.PRIVATE_BOT; const PRIVATE_BOT = process.env.PRIVATE_BOT;
const SUPPORT_URL = process.env.SUPPORT_URL; const SUPPORT_URL = process.env.SUPPORT_URL;
const WEB_HOST = process.env.WEB_HOST;
const WEB_PORT = process.env.WEB_PORT;
const OSU_API_KEY = process.env.OSU_API_KEY; const OSU_API_KEY = process.env.OSU_API_KEY;
const YOUTUBE_COOKIE_BASE64 = process.env.YOUTUBE_COOKIE_BASE64; const YOUTUBE_COOKIE_BASE64 = process.env.YOUTUBE_COOKIE_BASE64;
@@ -47,6 +50,9 @@ class Environment {
PRIVATE_BOT, PRIVATE_BOT,
SUPPORT_URL, SUPPORT_URL,
WEB_HOST,
WEB_PORT,
OSU_API_KEY, OSU_API_KEY,
YOUTUBE_COOKIE_BASE64, YOUTUBE_COOKIE_BASE64,
+53
View File
@@ -0,0 +1,53 @@
import express from 'express';
import cors from 'cors';
import Logger from "../libs/Logger";
import Environment from "./Environment";
import ExpressException from '../exception/ExpressException';
import Routes from './Routes';
class Express {
public server: any;
public express: express.Application;
constructor () {
this.server = null;
this.express = express();
}
public init(): void {
this.mountMiddlewares();
this.mountRoutes();
this.express = this.express.use(ExpressException.errorLogger);
this.server = this.express.listen(Environment.get().WEB_PORT, Environment.get().WEB_HOST, () => {
Logger.log('info', `Express Webserver listening at ${Environment.get().WEB_HOST}:${Environment.get().WEB_PORT}`);
});
}
public end(): void {
if(this.server != null)
this.server.close();
}
private mountRoutes (): void {
this.express = Routes.mountAPI(this.express);
this.express = Routes.mountWeb(this.express);
}
private mountMiddlewares (): void {
//this.express = RateLimit.init(this.express);
this.express = this.express.use(cors());
//This always have to be at the bottom!
this.express = this.express.use(express.json());
}
}
export default new Express();
+22
View File
@@ -0,0 +1,22 @@
import express, { Application } from 'express';
import Logger from '../libs/Logger';
import apiv1Router from '../routes/API/v1';
class Routes {
public mountWeb(_express: express.Application): Application {
Logger.log('info', 'Mounting Web routes');
return _express;
}
public mountAPI(_express: express.Application): Application {
Logger.log('info', 'Mounting API routes');
_express.use('/api/v1/', apiv1Router);
return _express;
}
}
export default new Routes;
+20
View File
@@ -0,0 +1,20 @@
import * as express from 'express';
import Environment from '../../providers/Environment'
import HealthCheck from '../../controllers/API/v1/HealthCheck';
const router = express.Router();
let isDevEnv = (req: any, res: any, next: any) => {
if(Environment.get().NODE_ENV == 'development') {
return next();
}
else {
return res.sendStatus(403);
}
};
router.get('/health', HealthCheck.perform);
export default router;