import bodyParser from 'body-parser'; import { errors } from 'celebrate'; import cors from 'cors'; import { Application, NextFunction, Request, Response } from 'express'; import { expressjwt } from 'express-jwt'; import Container from 'typedi'; import config from '../config'; import SystemService from '../services/system'; import Logger from './logger'; export default ({ app }: { app: Application }) => { app.set('trust proxy', 'loopback'); app.use(cors()); app.use(bodyParser.json({ limit: '50mb' })); app.use(bodyParser.urlencoded({ limit: '50mb', extended: true })); app.use( expressjwt({ secret: config.secret, algorithms: ['HS384'], }), ); app.put( '/api/reload', async (req: Request, res: Response, next: NextFunction) => { try { const systemService = Container.get(SystemService); const result = await systemService.reloadSystem(); res.send(result); } catch (e) { Logger.error('🔥 error: %o', e); return next(e); } }, ); app.put( '/api/system', async (req: Request, res: Response, next: NextFunction) => { try { const systemService = Container.get(SystemService); const result = await systemService.reloadSystem('system'); res.send(result); } catch (e) { Logger.error('🔥 error: %o', e); return next(e); } }, ); app.put( '/api/data', async (req: Request, res: Response, next: NextFunction) => { try { const systemService = Container.get(SystemService); const result = await systemService.reloadSystem('data'); res.send(result); } catch (e) { Logger.error('🔥 error: %o', e); return next(e); } }, ); app.use((req, res, next) => { const err: any = new Error('Not Found'); err['status'] = 404; next(err); }); app.use(errors()); app.use( ( err: Error & { status: number }, req: Request, res: Response, next: NextFunction, ) => { if (err.name === 'UnauthorizedError') { return res .status(err.status) .send({ code: 401, message: err.message }) .end(); } return next(err); }, ); app.use( ( err: Error & { status: number }, req: Request, res: Response, next: NextFunction, ) => { res.status(err.status || 500); res.json({ code: err.status || 500, message: err.message, }); }, ); };