From 4c5e1a864b0a09ac9330b2ae7847eb8d73c7b461 Mon Sep 17 00:00:00 2001 From: Janis Hutz Date: Wed, 26 Aug 2026 10:47:10 +0200 Subject: [PATCH] chore: fix startup of backend --- backend/src/app.ts | 87 +++++++++++++++++++++++-------------------- backend/src/logger.ts | 2 +- backend/tsconfig.json | 29 +++++++++------ 3 files changed, 65 insertions(+), 53 deletions(-) diff --git a/backend/src/app.ts b/backend/src/app.ts index ffe7008..5fd7820 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -4,51 +4,58 @@ import fs from 'fs'; import jwt from 'jsonwebtoken'; import path from 'path'; -const app = express(); -app.use( expressSession( { - 'secret': '', - 'resave': true, - 'saveUninitialized': false -} ) ); +const run = () => { + const app = express(); + + app.use( expressSession( { + 'secret': '', + 'resave': true, + 'saveUninitialized': false + } ) ); -app.get( '/', ( request: express.Request, response: express.Response ) => { - response.send( 'HELLO WORLD' ); -} ); - - - -const privateKey = fs.readFileSync( path.join( - __dirname, - '/config/apple_private_key.p8' -) ).toString(); -const musicApiConfig = JSON.parse( fs.readFileSync( path.join( - __dirname, - '/config/apple-music-api.config.secret.json' -) ).toString() ); - -app.get( '/dev-token', ( request: express.Request, response: express.Response ) => { - // sign dev token - const now = new Date().getTime(); - const tomorrow = now + ( 24 * 3600 * 1000 ); - const jwtToken = jwt.sign( { - 'iss': musicApiConfig.teamID, - 'iat': Math.floor( now / 1000 ), - 'exp': Math.floor( tomorrow / 1000 ) - }, privateKey, { - 'algorithm': 'ES256', - 'keyid': musicApiConfig.keyID + app.get( '/', ( request: express.Request, response: express.Response ) => { + response.send( 'HELLO WORLD' ); } ); - response.send( jwtToken ); -} ); - -app.use( ( _request: express.Request, response: express.Response ) => { - response.sendFile( path.join( __dirname, '' ) ); -} ); -const PORT = process.env.PORT || 8080; + const privateKey = fs.readFileSync( path.join( + __dirname, + '/config/apple_private_key.p8' + ) ).toString(); + const musicApiConfig = JSON.parse( fs.readFileSync( path.join( + __dirname, + '/config/apple-music-api.config.secret.json' + ) ).toString() ); -app.listen( PORT ); + app.get( '/dev-token', ( request: express.Request, response: express.Response ) => { + // sign dev token + const now = new Date().getTime(); + const tomorrow = now + ( 24 * 3600 * 1000 ); + const jwtToken = jwt.sign( { + 'iss': musicApiConfig.teamID, + 'iat': Math.floor( now / 1000 ), + 'exp': Math.floor( tomorrow / 1000 ) + }, privateKey, { + 'algorithm': 'ES256', + 'keyid': musicApiConfig.keyID + } ); + + response.send( jwtToken ); + } ); + + app.use( ( _request: express.Request, response: express.Response ) => { + response.sendFile( path.join( __dirname, '' ) ); + } ); + + + const PORT = process.env.PORT || 8080; + + app.listen( PORT ); +}; + +export default { + run +}; diff --git a/backend/src/logger.ts b/backend/src/logger.ts index f8bd59d..a2f3c20 100644 --- a/backend/src/logger.ts +++ b/backend/src/logger.ts @@ -44,7 +44,7 @@ const configure = ( location: 'stderr' | 'file', minLevel: LogLevel, file?: stri throw new Error( 'File parameter required when location is "file"' ); } - loc = location === 'stderr' ? 'stderr' : file; + loc = location === 'stderr' ? 'stderr' : ( file ?? 'musicplayer.log' ); lev = levels.indexOf( minLevel ); }; diff --git a/backend/tsconfig.json b/backend/tsconfig.json index a0fcf4c..1d33eb1 100644 --- a/backend/tsconfig.json +++ b/backend/tsconfig.json @@ -1,13 +1,18 @@ { - "compilerOptions": { - "outDir": "./dist", - "allowJs": true, - "target": "ES6", - "skipLibCheck": true, - "allowSyntheticDefaultImports": true, - "types": ["node"], - "module": "NodeNext", - "moduleResolution": "NodeNext" - }, - "include": [ "./src/**/*" ], -} \ No newline at end of file + "compilerOptions": { + "outDir": "./dist", + "rootDir": "./src", + "allowJs": true, + "target": "ES6", + "skipLibCheck": true, + "allowSyntheticDefaultImports": true, + "types": [ + "node" + ], + "module": "NodeNext", + "moduleResolution": "NodeNext" + }, + "include": [ + "./src/**/*" + ] +}