import http from 'http' ; import express from 'express' ; import 'express-async-errors' ; import dotenv from 'dotenv' ; dotenv . config ( ) ; import config from './config' ; import { Routers } from './routers' ; import { ErrorHandler } from './middleware/error-handler' ; import bodyParser from 'body-parser' ; import * as acl from 'express-dynacl' ; import { ensureDirs } from './file-storage' ; import { dbConnect } from './db' ; ( async ( ) => { await ensureDirs ( ) ; await dbConnect ( ) ; console . log ( 'Connected to the database.' ) ; const app = express ( ) ; if ( config . cors . enabled ) { const cors = ( await import ( 'cors' ) ) . default ; app . use ( cors ( config . cors ) ) ; console . log ( '[SERVER] CORS enabled' + ( config . cors . origin ? ' for origin ' + config . cors . origin : '' ) ) ; } await import ( 'express-async-errors' ) ; if ( config . server . compression ) { const compression = await import ( 'compression' ) ; app . use ( compression ( ) ) ; } app . use ( bodyParser . json ( { } ) ) ; app . use ( bodyParser . urlencoded ( { extended : true , limit : '10000kb' , } ) ) ; const jwt = ( await import ( 'express-jwt' ) ) . default ; app . use ( jwt ( config . jwt ) , ( err , req , res , next ) => err . code === 'invalid_token' ? next ( ) : next ( err ) ) ; const aclOptions = { roles : config . acl . roles , defaultRoles : [ 'guest' ] , logConsole : true , } ; acl . config ( aclOptions ) ; app . use ( Routers ) ; app . use ( ErrorHandler ) ; const host = config . server . host || '127.0.0.1' ; const port = config . server . port || 80 ; http . createServer ( app ) . listen ( port , host , ( ) => { console . log ( '[SERVER] Listening on ' + host + ':' + port + '!' ) ; } ) ; } ) ( ) ;