import { BaseApplication , ConfigurationService , GeneratorController , GlobalMiddleware , MetadataController , RegisterApplication , TypeORMService } from "@triptyk/nfw-core" ; import * as Compression from "compression" ; import * as Cors from "cors" ; import * as Express from "express" ; import * as Helmet from "helmet" ; import * as Passport from "passport" ; import { autoInjectable } from "tsyringe" ; import { AuthController } from "./controllers/auth.controller" ; import { BackupController } from "./controllers/backup.controller" ; import { DocumentController } from "./controllers/document.controller" ; import { StatusController } from "./controllers/status.controller" ; import { UserController } from "./controllers/user.controller" ; import { ErrorMiddleware } from "./middlewares/error.middleware" ; import { NotFoundMiddleware } from "./middlewares/not-found.middleware" ; import { RateLimitMiddleware } from "./middlewares/rate-limit.middleware" ; import { LoggerService } from "./services/logger.service" ; import { MailService } from "./services/mail-sender.service" ; import { MulterService } from "./services/multer.service" ; import { PassportService } from "./services/passport.service" ; @ RegisterApplication ( { controllers : [ AuthController , BackupController , UserController , DocumentController , StatusController , MetadataController , GeneratorController ] , services : [ MailService , TypeORMService , MulterService , PassportService , LoggerService , ConfigurationService ] } ) @ GlobalMiddleware ( RateLimitMiddleware ) @ GlobalMiddleware ( NotFoundMiddleware , null , "after" ) @ GlobalMiddleware ( ErrorMiddleware , null , "after" ) @ autoInjectable ( ) export class Application extends BaseApplication { public async afterInit ( ) { return true ; } public constructor ( private loggerService , private configurationService ) { super ( ) ; } public listen ( port ) { return super . listen ( port ) . then ( ( ) => { this . loggerService . logger . info ( `HTTP server is now running on port ${ port }` ) ; } ) ; } public async init ( ) { super . init ( ) ; const { authorized , api } = this . configurationService . config ; inheritdoc this . app . use ( Express . urlencoded ( { extended : false } ) ) ; this . app . use ( Express . json ( { type : "application/vnd.api+json" } ) ) ; inheritdoc this . app . use ( Compression ( ) ) ; /**
         * Public resources
         */ this . app . use ( "/static" , Express . static ( "dist/uploads/documents" ) ) ; inheritdoc this . app . use ( Helmet ( ) ) ; inheritdoc const CORSOptions = { allowedHeaders : [ "Content-Type" , "Authorization" ] , methods : [ "GET" , "POST" , "PUT" , "PATCH" , "DELETE" ] , origin : authorized } ; this . app . use ( Cors ( CORSOptions ) ) ; inheritdoc this . app . use ( Passport . initialize ( ) ) ; this . app . enable ( "trust proxy" ) ; this . app . use ( `/api/${ api . version }` , this . router ) ; return this . app ; } }