import express from "express" ; import morgan from "morgan" ; import helmet from "helmet" ; import cors from "cors" ; import limiter from "./utils/rateLimit" ; import { checkApiKey , errorHandler , notFound } from "./middlewares" ; import allowedIps from "./utils/allowedIps" ; import path from "path" ; import serveStatic from "serve-static" ; import api from "./api/_index" ; import v1 from "./v1/_index" ; import apiKeyController from "./api/keys" ; require ( "dotenv" ) . config ( ) ; import mongoose from "mongoose" ; try { mongoose . connect ( process . env . MONGODB_URL ! , { useNewUrlParser : true , useUnifiedTopology : true , } ) ; } catch ( error ) { console . warn ( "Failed to connect to mongo" ) ; console . warn ( error ) ; } const app = express ( ) ; app . use ( morgan ( "common" ) ) ; app . use ( helmet ( { contentSecurityPolicy : { directives : { defaultSrc : [ "'self'" ] , scriptSrc : [ "'self'" , "https://cdn.jsdelivr.net" ] , styleSrc : [ "'self'" , "https://cdn.jsdelivr.net" , "https://fonts.googleapis.com" ] , fontSrc : [ "'self'" , "fonts.gstatic.com" , "https://cdn.jsdelivr.net" ] } } , } ) ) ; app . use ( cors ( ) ) ; app . use ( express . json ( ) ) ; app . set ( "trust proxy" , 1 ) ; app . use ( serveStatic ( path . join ( __dirname , "../public" ) ) ) ; app . use ( "/key" , allowedIps , apiKeyController . getApiKey ) ; app . use ( "/api" , limiter , api ) ; app . use ( "/v1" , checkApiKey , v1 ) ; app . use ( notFound ) ; app . use ( errorHandler ) ; const port = process . env . PORT || 5050 ; app . listen ( port , ( ) => { console . log ( `Listening to port ${ port }` ) ; } ) ;