Use classes to build routes
MODIFY
src/server/rest/CentralRestServer.ts
src/server/rest/CentralRestServer.ts
@@ -11,13 +11,13 @@ import ChargingStationConfiguration from '../../types/configuration/ChargingStat
import Configuration from '../../utils/Configuration';
import Constants from '../../utils/Constants';
import ExpressTools from '../ExpressTools';
+import GlobalRouter from './router/GlobalRouter';
import Logging from '../../utils/Logging';
import { ServerAction } from '../../types/Server';
import SessionHashService from './service/SessionHashService';
import UserToken from '../../types/UserToken';
import Utils from '../../utils/Utils';
import cluster from 'cluster';
-import { globalRouter } from './router/GlobalRouter';
import http from 'http';
import sanitize from 'express-sanitizer';
import socketio from 'socket.io';
@@ -50,7 +50,7 @@ export default class CentralRestServer {
// Authentication
this.expressApplication.use(AuthService.initialize());
// Routers
- this.expressApplication.use('/', globalRouter);
+ this.expressApplication.use('/v1', new GlobalRouter().buildRoutes());
// Auth services
this.expressApplication.all('/client/auth/:action', CentralRestServerAuthentication.authService.bind(this));
// Secured API

MODIFY
src/server/rest/router/GlobalRouter.ts
src/server/rest/router/GlobalRouter.ts
+import AuthRouter from './auth/AuthRouter';
import AuthService from '../service/AuthService';
-import { authRouter } from './auth/AuthRouter';
+import SwaggerRouter from './doc/SwaggerRouter';
+import TenantRouter from './api/TenantRouter';
+import UtilRouter from './util/UtilRouter';
import express from 'express';
-import { swaggerRouter } from './doc/SwaggerRouter';
-import { tenantRouter } from './api/TenantRouter';
-import { utilRouter } from './util/UtilRouter';
-export const globalRouter = express.Router();
+export default class GlobalRouter {
+ private router: express.Router;
-// API version 1 top level routes
+ public constructor() {
+ this.router = express.Router();
+ }
-// Protected API
-globalRouter.use('/v1/api', AuthService.authenticate(), tenantRouter);
+ public buildRoutes(): express.Router {
+ this.buildRouteAuth();
+ this.buildRouteAPI();
+ this.buildRouteUtil();
+ this.buildRouteDocs();
+ return this.router;
+ }
-// Auth
-globalRouter.use('/v1/auth', authRouter);
+ protected buildRouteAuth(): void {
+ this.router.use('/auth', new AuthRouter().buildRoutes());
+ }
-// Util
-globalRouter.use('/v1/util', utilRouter);
+ protected buildRouteAPI(): void {
+ this.router.use('/api', AuthService.authenticate(), [
+ new TenantRouter().buildRoutes()
+ ]);
+ }
-// Docs
-globalRouter.use('/v1/docs', swaggerRouter);
+ protected buildRouteUtil(): void {
+ this.router.use('/util', new UtilRouter().buildRoutes());
+ }
+
+ protected buildRouteDocs(): void {
+ this.router.use('/docs', new SwaggerRouter().buildRoutes());
+ }
+}

MODIFY
src/server/rest/router/api/TenantRouter.ts
src/server/rest/router/api/TenantRouter.ts
@@ -5,14 +5,29 @@ import { ServerAction } from '../../../../types/Server';
import TenantService from '../../service/TenantService';
import sanitize from 'mongo-sanitize';
-export const tenantRouter = express.Router();
+export default class TenantRouter {
+ private router: express.Router;
-tenantRouter.get(`/${ServerAction.REST_TENANTS}`, async (req: Request, res: Response, next: NextFunction) => {
+ public constructor() {
+ this.router = express.Router();
+ }
+
+ public buildRoutes(): express.Router {
+ this.buildRouteTenants();
+ this.buildRouteTenant();
+ return this.router;
+ }
+
+ protected buildRouteTenants(): void {
+ this.router.get(`/${ServerAction.REST_TENANTS}`, async (req: Request, res: Response, next: NextFunction) => {
await RouterUtils.handleServerAction(TenantService.handleGetTenants.bind(this), ServerAction.REST_TENANTS, req, res, next);
});
+ }
-tenantRouter.get(`/${ServerAction.REST_TENANTS}/:id`, async (req: Request, res: Response, next: NextFunction) => {
+ protected buildRouteTenant(): void {
+ this.router.get(`/${ServerAction.REST_TENANTS}/:id`, async (req: Request, res: Response, next: NextFunction) => {
req.query.ID = sanitize(req.params.id);
await RouterUtils.handleServerAction(TenantService.handleGetTenant.bind(this), ServerAction.REST_TENANTS, req, res, next);
});
-
+ }
+}

MODIFY
src/server/rest/router/auth/AuthRouter.ts
src/server/rest/router/auth/AuthRouter.ts
@@ -4,20 +4,42 @@ import AuthService from '../../service/AuthService';
import RouterUtils from '../RouterUtils';
import { ServerAction } from '../../../../types/Server';
-export const authRouter = express.Router();
+export default class AuthRouter {
+ private router: express.Router;
-authRouter.post(`/${ServerAction.SIGNIN}`, async (req: Request, res: Response, next: NextFunction) => {
+ public constructor() {
+ this.router = express.Router();
+ }
+
+ public buildRoutes(): express.Router {
+ this.buildRouteSignIn();
+ this.buildRouteSignOn();
+ this.buildRouteSignOut();
+ this.buildRoutePasswordReset();
+ return this.router;
+ }
+
+ protected buildRouteSignIn(): void {
+ this.router.post(`/${ServerAction.SIGNIN}`, async (req: Request, res: Response, next: NextFunction) => {
await RouterUtils.handleServerAction(AuthService.handleLogIn.bind(this), ServerAction.SIGNIN, req, res, next);
});
+ }
-authRouter.post(`/${ServerAction.SIGNON}`, async (req: Request, res: Response, next: NextFunction) => {
+ protected buildRouteSignOn(): void {
+ this.router.post(`/${ServerAction.SIGNON}`, async (req: Request, res: Response, next: NextFunction) => {
await RouterUtils.handleServerAction(AuthService.handleRegisterUser.bind(this), ServerAction.SIGNON, req, res, next);
});
+ }
-authRouter.get(`/${ServerAction.SIGNOUT}`, async (req: Request, res: Response, next: NextFunction) => {
+ protected buildRouteSignOut(): void {
+ this.router.get(`/${ServerAction.SIGNOUT}`, async (req: Request, res: Response, next: NextFunction) => {
await RouterUtils.handleServerAction(AuthService.handleUserLogOut.bind(this), ServerAction.SIGNOUT, req, res, next);
});
+ }
-authRouter.post(`/${ServerAction.REST_PASSWORD_RESET}`, async (req: Request, res: Response, next: NextFunction) => {
+ protected buildRoutePasswordReset(): void {
+ this.router.post(`/${ServerAction.REST_PASSWORD_RESET}`, async (req: Request, res: Response, next: NextFunction) => {
await RouterUtils.handleServerAction(AuthService.handleUserPasswordReset.bind(this), ServerAction.REST_PASSWORD_RESET, req, res, next);
});
+ }
+}

MODIFY
src/server/rest/router/doc/SwaggerRouter.ts
src/server/rest/router/doc/SwaggerRouter.ts
@@ -3,15 +3,22 @@ import fs from 'fs';
import global from '../../../../types/GlobalType';
import swaggerUi from 'swagger-ui-express';
-export const swaggerRouter = express.Router();
-
const options = {
explorer: false
};
-const oasDocument = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/docs/open-api-standard.json`, 'utf8'));
+export default class SwaggerRouter {
+ private router: express.Router;
-swaggerRouter.use('/', swaggerUi.serve);
+ public constructor() {
+ this.router = express.Router();
+ }
-swaggerRouter.get('/', swaggerUi.setup(oasDocument, options));
+ public buildRoutes(): express.Router {
+ const oasDocument = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/docs/open-api-standard.json`, 'utf8'));
+ this.router.use('/', swaggerUi.serve);
+ this.router.get('/', swaggerUi.setup(oasDocument, options));
+ return this.router;
+ }
+}

MODIFY
src/server/rest/router/util/UtilRouter.ts
src/server/rest/router/util/UtilRouter.ts
@@ -3,9 +3,21 @@ import express, { NextFunction, Request, Response } from 'express';
import { ServerAction } from '../../../../types/Server';
import { StatusCodes } from 'http-status-codes';
-export const utilRouter = express.Router();
+export default class UtilRouter {
+ private router: express.Router;
-utilRouter.get(`/${ServerAction.REST_PING}`, async (req: Request, res: Response, next: NextFunction) => {
+ public constructor() {
+ this.router = express.Router();
+ }
+
+ public buildRoutes(): express.Router {
+ this.buildRoutePing();
+ return this.router;
+ }
+
+ protected buildRoutePing(): void {
+ this.router.get(`/${ServerAction.REST_PING}`, async (req: Request, res: Response, next: NextFunction) => {
res.sendStatus(StatusCodes.OK);
});
-
+ }
+}

Use classes to build routes