2024-01-20 23:34:31 +01:00
import e from "express"
import fs from "fs" ;
import { BODY_SIZE_LIMIT , COOKIE_SIGN_KEY , DASHBOARD_ROOT , ENDPOINT_AUTHENTICATION_ENABLED , ENDPOINT_AUTH_HEADER , ENDPOINT_AUTH_VALUE , IS_DEBUG , PORT , PROJECT_NAME , SERVER_URL } from "../Modules/Constants" ;
2024-01-22 23:04:46 +01:00
import { Debug , Msg , Warn } from "../Modules/Logger" ;
2024-01-20 23:34:31 +01:00
import { italic , magenta , red , yellow } from "colorette" ;
import cookieParser from "cookie-parser" ;
import path from "path" ;
import cors from "cors" ;
export const App = e ( )
. disable ( "etag" )
. disable ( "x-powered-by" )
. use ( cookieParser ( COOKIE_SIGN_KEY ) )
. use ( cors ( {
origin : DASHBOARD_ROOT ,
credentials : true
} ) )
. use ( e . json ( { limit : BODY_SIZE_LIMIT } ) )
. use ( e . urlencoded ( { limit : BODY_SIZE_LIMIT , extended : false } ) ) ;
async function Initialize() {
const Files = fs
. readdirSync ( path . join ( "." , Symbol . for ( "ts-node.register.instance" ) in process ? "Source" : "bin" , "Routes" ) )
. filter ( ( F ) = > F . endsWith ( ".js" ) || F . endsWith ( ".ts" ) ) ;
if ( ENDPOINT_AUTHENTICATION_ENABLED )
Warn ( ` Endpoint authentication requirement is enabled! You will not be able to connect to the server without the ${ yellow ( ENDPOINT_AUTH_HEADER as string ) } header. ` )
App . use ( ( req , res , next ) = > {
if ( ENDPOINT_AUTHENTICATION_ENABLED && req . header ( ENDPOINT_AUTH_HEADER as string ) !== ENDPOINT_AUTH_VALUE )
return res . status ( 403 ) . send ( ` ${ SERVER_URL } is currently locked behind authentication. Come back later! ` ) ;
2024-01-22 23:04:46 +01:00
Debug ( req . path ) ;
2024-01-20 23:34:31 +01:00
next ( ) ;
} )
for ( const File of Files ) {
const Contents = await import ( path . join ( ".." , "Routes" , File ) ) ;
if ( ! Contents . default ) continue ;
if ( ! Contents . default . App ) continue ;
App . use ( Contents . default . DefaultAPI || "/" , Contents . default . App ) ;
Msg ( ` Loaded route ${ italic ( File ) } ! ` ) ;
}
2024-01-22 23:04:46 +01:00
App . use ( ( _ , res ) = > res . status ( 404 ) . json ( { errorMessage : "Not Found" } ) ) ;
2024-01-26 23:29:46 +01:00
App . use ( ( err , _ , res , __ ) = > {
console . error ( err ) ;
res . status ( 500 ) . json ( { errorMessage : IS_DEBUG ? err : "Oops! Something broke on our end. Sorry!" } ) ;
} )
2024-01-20 23:34:31 +01:00
App . listen ( PORT , ( ) = > Msg ( ` ${ magenta ( PROJECT_NAME ) } now up on port ${ magenta ( PORT ) } ${ ( IS_DEBUG ? red ( "(debug environment)" ) : "" ) } ` ) ) ;
}
Initialize ( ) ;
// ! FESTIVAL-SPECIFIC STUFF
2024-01-22 00:41:59 +01:00
import axios from "axios" ;
2024-01-20 23:34:31 +01:00
2024-01-22 00:41:59 +01:00
axios . defaults . validateStatus = ( ) = > true ;
2024-01-22 23:04:46 +01:00
axios . defaults . headers . common [ "X-Do-Not-Redirect" ] = "true" ;