diff --git a/src/plugins/api-server/backend/main.ts b/src/plugins/api-server/backend/main.ts index c4ac67d5..c558c4ff 100644 --- a/src/plugins/api-server/backend/main.ts +++ b/src/plugins/api-server/backend/main.ts @@ -19,7 +19,7 @@ export const backend = createBackend({ async start(ctx) { const config = await ctx.getConfig(); - await this.init(ctx); + this.init(ctx); registerCallback((songInfo) => { this.songInfo = songInfo; }); @@ -60,8 +60,7 @@ export const backend = createBackend({ }, // Custom - async init(ctx) { - const config = await ctx.getConfig(); + init(backendCtx) { this.app = new Hono(); this.app.use('*', cors()); @@ -74,6 +73,8 @@ export const backend = createBackend({ // middlewares this.app.use('/api/*', async (ctx, next) => { + const config = await backendCtx.getConfig(); + if (config.authStrategy !== AuthStrategy.NONE) { return await jwt({ secret: config.secret, @@ -83,6 +84,7 @@ export const backend = createBackend({ }); this.app.use('/api/*', async (ctx, next) => { const result = await JWTPayloadSchema.spa(await ctx.get('jwtPayload')); + const config = await backendCtx.getConfig(); const isAuthorized = config.authStrategy === AuthStrategy.NONE || @@ -98,12 +100,12 @@ export const backend = createBackend({ // routes registerControl( this.app, - ctx, + backendCtx, () => this.songInfo, () => this.currentRepeatMode, () => this.volume, ); - registerAuth(this.app, ctx); + registerAuth(this.app, backendCtx); // swagger this.app.openAPIRegistry.registerComponent( diff --git a/src/plugins/api-server/backend/types.ts b/src/plugins/api-server/backend/types.ts index 1419bafb..ec7b91f9 100644 --- a/src/plugins/api-server/backend/types.ts +++ b/src/plugins/api-server/backend/types.ts @@ -15,7 +15,7 @@ export type BackendType = { currentRepeatMode?: RepeatMode; volume?: number; - init: (ctx: BackendContext) => Promise; + init: (ctx: BackendContext) => void; run: (hostname: string, port: number) => void; end: () => void; };