import 'reflect-metadata' ; import 'newrelic' ; import * as Sentry from '@sentry/node' ; import '../src/Controller/HealthCheckController' ; import '../src/Controller/RevisionsController' ; import '../src/Controller/ItemsController' ; import '../src/Controller/AdminController' ; import * as helmet from 'helmet' ; import * as cors from 'cors' ; import { urlencoded , json , Request , Response , NextFunction , RequestHandler , ErrorRequestHandler } from 'express' ; import * as winston from 'winston' ; import * as dayjs from 'dayjs' ; import * as utc from 'dayjs/plugin/utc' ; import { InversifyExpressServer } from 'inversify-express-utils' ; import { ContainerConfigLoader } from '../src/Bootstrap/Container' ; import TYPES from '../src/Bootstrap/Types' ; import { Env } from '../src/Bootstrap/Env' ; const container = new ContainerConfigLoader ; void container . load ( ) . then ( container => { dayjs . extend ( utc ) ; const env = new Env ( ) ; env . load ( ) ; const server = new InversifyExpressServer ( container ) ; server . setConfig ( ( app ) => { app . use ( ( _request , response , next ) => { response . setHeader ( 'X-SSJS-Version' , container . get ( TYPES . VERSION ) ) ; next ( ) ; } ) ; app . use ( helmet ( { contentSecurityPolicy : { directives : { defaultSrc : [ "https: 'self'" ] , baseUri : [ "'self'" ] , childSrc : [ "*" , "blob:" ] , connectSrc : [ "*" ] , fontSrc : [ "*" , "'self'" ] , formAction : [ "'self'" ] , frameAncestors : [ "*" , "*.standardnotes.org" ] , frameSrc : [ "*" , "blob:" ] , imgSrc : [ "'self'" , "*" , "data:" ] , manifestSrc : [ "'self'" ] , mediaSrc : [ "'self'" ] , objectSrc : [ "'self'" ] , scriptSrc : [ "'self'" ] , styleSrc : [ "'self'" ] } } } ) ) ; app . use ( json ( { limit : '50mb' } ) ) ; app . use ( urlencoded ( { extended : true , limit : '50mb' , parameterLimit : 5000 } ) ) ; app . use ( cors ( ) ) ; if ( env . get ( 'SENTRY_DSN' , true ) ) { Sentry . init ( { dsn : env . get ( 'SENTRY_DSN' ) , integrations : [ new Sentry . Integrations . Http ( { tracing : false , breadcrumbs : true } ) , ] , tracesSampleRate : 0 , } ) ; app . use ( Sentry . Handlers . requestHandler ( ) as RequestHandler ) ; } } ) ; const logger = container . get ( TYPES . Logger ) ; server . setErrorConfig ( ( app ) => { if ( env . get ( 'SENTRY_DSN' , true ) ) { app . use ( Sentry . Handlers . errorHandler ( ) as ErrorRequestHandler ) ; } app . use ( ( error , _request , response , _next ) => { logger . error ( error . stack ) ; response . status ( 500 ) . send ( { error : { message : 'Unfortunately, we couldn\'t handle your request. Please try again or contact our support if the error persists.' , } , } ) ; } ) ; } ) ; const serverInstance = server . build ( ) ; serverInstance . listen ( env . get ( 'PORT' ) ) ; logger . info ( `Server started on port ${ process . env . PORT }` ) ; } ) ;