App.ts 3.28 KB
Newer Older
1
import { Handlers } from '@sentry/node';
Fullarton's avatar
Fullarton committed
2
import * as bodyParser from 'body-parser';
3
import * as compress from 'compression';
4
import * as cors from 'cors';
5
import * as express from 'express';
6
import { Request, Response, Router } from 'express';
7
import * as logger from 'morgan';
8
import { RoutingControllersOptions, useExpressServer } from 'routing-controllers';
9
import * as swaggerUi from 'swagger-ui-express';
10
import options from './lib/cors.config';
11 12
import { ErrorHandlerMiddleware } from './routers/middleware/customErrorHandler';
import { I18nMiddleware } from './routers/middleware/i18n';
13
import { roleAuthorizationChecker } from './routers/middleware/roleAuthorizationChecker';
14 15 16 17 18 19 20 21 22 23
import { AdminRouter } from './routers/rest/AdminRouter';
import { ApiRouter } from './routers/rest/ApiRouter';
import { ExpiryQuizRouter } from './routers/rest/ExpiryQuizRouter';
import { I18nApiRouter } from './routers/rest/I18nApiRouter';
import { LegacyApiRouter } from './routers/rest/LegacyApi';
import { LibRouter } from './routers/rest/LibRouter';
import { LobbyRouter } from './routers/rest/LobbyRouter';
import { MemberRouter } from './routers/rest/MemberRouter';
import { NicksRouter } from './routers/rest/NicksRouter';
import { QuizRouter } from './routers/rest/QuizRouter';
24
import { dynamicStatistics, staticStatistics } from './statistics';
Fullarton's avatar
Fullarton committed
25

26 27
declare var global: any;

28 29
export const routingControllerOptions: RoutingControllersOptions = {
  defaults: {
30
    nullResultCode: 405,
31 32 33 34 35
    undefinedResultCode: 204,
    paramOptions: {
      required: true,
    },
  },
36
  authorizationChecker: roleAuthorizationChecker,
37
  defaultErrorHandler: false,
38
  cors: options,
39 40 41 42
  controllers: [
    AdminRouter, ApiRouter, ExpiryQuizRouter, I18nApiRouter, LegacyApiRouter, LibRouter, LobbyRouter, MemberRouter, NicksRouter, QuizRouter,
  ],
  middlewares: [ErrorHandlerMiddleware, I18nMiddleware],
43 44
};

Fullarton's avatar
Fullarton committed
45 46 47
// Creates and configures an ExpressJS web server.
class App {

48 49 50 51
  get express(): express.Application {
    return this._express;
  }

Fullarton's avatar
Fullarton committed
52
  // ref to Express instance
53
  private readonly _express: express.Application;
Fullarton's avatar
Fullarton committed
54 55 56

  // Run configuration methods on the Express instance.
  constructor() {
57
    this._express = express();
58
    this._express.use(Handlers.requestHandler());
59

Fullarton's avatar
Fullarton committed
60 61
    this.middleware();
    this.routes();
62

63
    this._express.use(Handlers.errorHandler());
64
    useExpressServer(this._express, routingControllerOptions);
Fullarton's avatar
Fullarton committed
65 66 67 68
  }

  // Configure Express middleware.
  private middleware(): void {
69
    this._express.use(logger('dev'));
70
    this._express.use(bodyParser.json({ limit: '50mb' }));
71 72 73 74
    this._express.use(bodyParser.urlencoded({
      limit: '50mb',
      extended: true,
    }));
75
    this._express.options('*', cors(options));
76
    this._express.use(compress());
Fullarton's avatar
Fullarton committed
77 78 79 80
  }

  // Configure API endpoints.
  private routes(): void {
81
    this._express.use('/api/v1/api-docs', swaggerUi.serve, swaggerUi.setup(null, {
82
      swaggerUrl: `${staticStatistics.rewriteAssetCacheUrl}/api/v1/api-docs.json`,
83 84
    }));

Fullarton's avatar
Fullarton committed
85
    const router: Router = express.Router();
86
    router.get(`/`, (req: Request, res: Response) => {
Fullarton's avatar
Fullarton committed
87 88
      res.send(Object.assign({}, staticStatistics, dynamicStatistics()));
    });
89
    router.get(`/err`, () => {
90 91
      throw new Error('testerror');
    });
92

93
    this._express.use(`${staticStatistics.routePrefix}/`, router);
94
  }
Fullarton's avatar
Fullarton committed
95 96 97
}

export default new App().express;