2024-01-28 11:07:10 +01:00
import e from "express"
import fs , { existsSync , mkdirSync } from "fs" ;
2024-02-04 00:36:28 +01:00
import { SAVED_DATA_PATH , 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-28 11:07:10 +01:00
import { Debug , Msg , Warn } from "../Modules/Logger" ;
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() {
2024-02-04 00:36:28 +01:00
if ( ! existsSync ( SAVED_DATA_PATH ) || ! existsSync ( ` ${ SAVED_DATA_PATH } /Songs ` ) )
mkdirSync ( ` ${ SAVED_DATA_PATH } /Songs ` , { recursive : true } ) ;
2024-01-28 11:07:10 +01:00
Debug ( ` The CWD is ${ magenta ( process . cwd ( ) ) } . ` ) ;
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! ` ) ;
Debug ( req . path ) ;
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 ) } ! ` ) ;
}
if ( existsSync ( "./dist" ) ) {
Warn ( ` Detected ${ yellow ( "dist" ) } folder! Using as static. ` ) ;
App . use ( "/" , e . static ( "dist" ) ) ;
App . use ( "/assets" , e . static ( "dist/assets" ) ) ;
}
2024-01-31 00:27:53 +01:00
App . use ( "/api/*" , ( _ , res ) = > res . status ( 404 ) . send ( "Not Found" ) ) ;
2024-01-28 11:07:10 +01:00
App . use ( "*" , ( _ , res ) = > res . sendFile ( path . join ( process . cwd ( ) , "dist" , "index.html" ) ) ) ;
App . use ( ( err , req , res , _ ) = > {
console . error ( err ) ;
2024-01-31 00:27:53 +01:00
res . status ( 500 ) . json ( IS_DEBUG ? err . message : "Oops! Something broke on our end. Sorry!" ) ;
2024-01-28 11:07:10 +01:00
} )
App . listen ( PORT , ( ) = > Msg ( ` ${ magenta ( PROJECT_NAME ) } now up on port ${ magenta ( PORT ) } ${ ( IS_DEBUG ? red ( "(debug environment)" ) : "" ) } ` ) ) ;
}
Initialize ( ) ;
// ! FESTIVAL-SPECIFIC STUFF
import axios from "axios" ;
axios . defaults . validateStatus = ( ) = > true ;
2024-01-22 23:04:46 +01:00
axios . defaults . headers . common [ "X-Do-Not-Redirect" ] = "true" ;