2023-06-22 19:36:52 +05:00
const fs = require ( "fs" ) ;
const colors = require ( "colors" ) ;
const consolePrefix = ` ${ "[" . blue } ${ "dbd-soft-ui" . yellow } ${ "]" . blue } ` ;
const Nodeactyl = require ( "nodeactyl" ) ;
2023-06-19 14:21:58 +05:00
module . exports = {
2023-06-22 19:36:52 +05:00
init : async function ( config , themeConfig , app , db ) {
let info ;
if ( themeConfig ? . customThemeOptions ? . info ) info = await themeConfig . customThemeOptions . info ( { config : config } ) ;
2023-06-19 14:21:58 +05:00
2023-06-22 19:36:52 +05:00
if ( themeConfig ? . admin ? . pterodactyl ? . enabled ) {
themeConfig . nodeactyl = new Nodeactyl . NodeactylClient ( themeConfig . admin ? . pterodactyl ? . panelLink , themeConfig . admin ? . pterodactyl ? . apiKey ) ;
2023-06-19 14:21:58 +05:00
2023-06-22 19:36:52 +05:00
try {
await themeConfig . nodeactyl . getAccountDetails ( ) ;
} catch ( error ) {
console . log ( ` ${ consolePrefix } ${ ( "Failed to connect to Pterodactyl panel!\nEnsure you've used a CLIENT api key, (found at " + themeConfig . admin . pterodactyl . panelLink + "/account/api)" ) . red } ` ) ;
}
}
2023-06-19 14:21:58 +05:00
2023-06-22 19:36:52 +05:00
const eventFolders = fs . readdirSync ( ` ${ _ _dirname } /../pages ` ) ;
2023-06-19 14:21:58 +05:00
2023-06-22 19:36:52 +05:00
for ( const folder of eventFolders ) {
const eventFiles = fs . readdirSync ( ` ${ _ _dirname } /../pages/ ${ folder } ` ) . filter ( ( file ) => file . endsWith ( ".js" ) ) ;
2023-06-19 14:21:58 +05:00
2023-06-22 19:36:52 +05:00
for ( const file of eventFiles ) {
const e = require ( ` ${ _ _dirname } /../pages/ ${ folder } / ${ file } ` ) ;
try {
if ( folder === "admin" ) {
await app . get ( e . page , async function ( req , res ) {
if ( ! req . session . user ) return res . sendStatus ( 401 ) ;
if ( ! config . ownerIDs ? . includes ( req . session . user . id ) ) return res . sendStatus ( 403 ) ;
e . execute ( req , res , app , config , themeConfig , info , db ) ;
} ) ;
} else if ( folder === "post" ) {
await app . post ( e . page , function ( req , res ) {
e . execute ( req , res , app , config , themeConfig , info , db ) ;
} ) ;
} else if ( folder === "get" ) {
await app . use ( e . page , async function ( req , res ) {
e . execute ( req , res , app , config , themeConfig , info , db ) ;
} ) ;
}
} catch ( error ) {
console . log ( ` ${ consolePrefix } ${ "Failed to load:" . cyan } ${ colors . red ( e . page ) } ` ) ;
console . log ( ` Page handler ${ file } : ${ error } ` ) ;
}
}
}
app . use ( "*" , async function ( req , res ) {
res . status ( 404 ) ;
config . errorPage ( req , res , undefined , 404 ) ;
} ) ;
// eslint-disable-next-line no-unused-vars
app . use ( ( err , req , res , next ) => {
res . status ( 500 ) ;
config . errorPage ( req , res , err , 500 ) ;
} ) ;
console . log ( ` ${ consolePrefix } ${ "Initialised all pages!" . cyan } ` ) ;
} ,
} ;