import * as mongoSessionStore from 'connect-mongo' ; import * as cors from 'cors' ; import * as express from 'express' ; import * as session from 'express-session' ; import * as httpModule from 'http' ; import * as mongoose from 'mongoose' ; import api from './api' ; import { setupGoogle } from './google-auth' ; import { setupPasswordless } from './passwordless-auth' ; import { setupSockets } from './sockets' ; import { stripeWebhookAndCheckoutCallback } from './stripe' ; import logger from './logger' ; import * as compression from 'compression' ; import * as helmet from 'helmet' ; require ( 'dotenv' ) . config ( ) ; const dev = process . env . NODE_ENV !== 'production' ; const port = process . env . PORT || 8000 ; const options = { useNewUrlParser : true , useCreateIndex : true , useFindAndModify : false , useUnifiedTopology : true } ; mongoose . connect ( dev ? process . env . MONGO_URL_TEST : process . env . MONGO_URL , options ) ; const server = express ( ) ; server . use ( cors ( { origin : dev ? process . env . URL_APP : process . env . PRODUCTION_URL_APP , methods : 'GET,HEAD,PUT,PATCH,POST,DELETE' , credentials : true } ) ) ; server . use ( helmet ( ) ) ; server . use ( compression ( ) ) ; stripeWebhookAndCheckoutCallback ( { server } ) ; server . use ( express . json ( ) ) ; const MongoStore = mongoSessionStore ( session ) ; const sessionOptions = { name : process . env . SESSION_NAME , secret : process . env . SESSION_SECRET , store : new MongoStore ( { mongooseConnection : mongoose . connection , ttl : 14 * 24 * 60 * 60 , autoRemove : 'interval' , autoRemoveInterval : 1440 } ) , resave : false , saveUninitialized : false , cookie : { httpOnly : true , maxAge : 14 * 24 * 60 * 60 * 1000 , domain : dev ? 'localhost' : process . env . COOKIE_DOMAIN } as any } ; if ( ! dev ) { server . set ( 'trust proxy' , 1 ) ; sessionOptions . cookie . secure = true ; } const sessionMiddleware = session ( sessionOptions ) ; server . use ( sessionMiddleware ) ; setupGoogle ( { server } ) ; setupPasswordless ( { server } ) ; api ( server ) ; const httpServer = httpModule . createServer ( server ) ; setupSockets ( { httpServer , origin : dev ? process . env . URL_APP : process . env . PRODUCTION_URL_APP , sessionMiddleware } ) ; server . get ( '*' , ( _ , res ) => { res . sendStatus ( 403 ) ; } ) ; httpServer . listen ( port , ( ) => { logger . debug ( 'debug right before info' ) ; logger . info ( `> Ready on ${ dev ? process . env . URL_API : process . env . PRODUCTION_URL_API }` ) ; } ) ;