fix(api-server): fix #3572

This commit is contained in:
JellyBrick
2025-07-13 16:43:29 +09:00
parent e2c849f6c4
commit 33a09cc8e1
2 changed files with 8 additions and 6 deletions

View File

@ -19,7 +19,7 @@ export const backend = createBackend<BackendType, APIServerConfig>({
async start(ctx) { async start(ctx) {
const config = await ctx.getConfig(); const config = await ctx.getConfig();
await this.init(ctx); this.init(ctx);
registerCallback((songInfo) => { registerCallback((songInfo) => {
this.songInfo = songInfo; this.songInfo = songInfo;
}); });
@ -60,8 +60,7 @@ export const backend = createBackend<BackendType, APIServerConfig>({
}, },
// Custom // Custom
async init(ctx) { init(backendCtx) {
const config = await ctx.getConfig();
this.app = new Hono(); this.app = new Hono();
this.app.use('*', cors()); this.app.use('*', cors());
@ -74,6 +73,8 @@ export const backend = createBackend<BackendType, APIServerConfig>({
// middlewares // middlewares
this.app.use('/api/*', async (ctx, next) => { this.app.use('/api/*', async (ctx, next) => {
const config = await backendCtx.getConfig();
if (config.authStrategy !== AuthStrategy.NONE) { if (config.authStrategy !== AuthStrategy.NONE) {
return await jwt({ return await jwt({
secret: config.secret, secret: config.secret,
@ -83,6 +84,7 @@ export const backend = createBackend<BackendType, APIServerConfig>({
}); });
this.app.use('/api/*', async (ctx, next) => { this.app.use('/api/*', async (ctx, next) => {
const result = await JWTPayloadSchema.spa(await ctx.get('jwtPayload')); const result = await JWTPayloadSchema.spa(await ctx.get('jwtPayload'));
const config = await backendCtx.getConfig();
const isAuthorized = const isAuthorized =
config.authStrategy === AuthStrategy.NONE || config.authStrategy === AuthStrategy.NONE ||
@ -98,12 +100,12 @@ export const backend = createBackend<BackendType, APIServerConfig>({
// routes // routes
registerControl( registerControl(
this.app, this.app,
ctx, backendCtx,
() => this.songInfo, () => this.songInfo,
() => this.currentRepeatMode, () => this.currentRepeatMode,
() => this.volume, () => this.volume,
); );
registerAuth(this.app, ctx); registerAuth(this.app, backendCtx);
// swagger // swagger
this.app.openAPIRegistry.registerComponent( this.app.openAPIRegistry.registerComponent(

View File

@ -15,7 +15,7 @@ export type BackendType = {
currentRepeatMode?: RepeatMode; currentRepeatMode?: RepeatMode;
volume?: number; volume?: number;
init: (ctx: BackendContext<APIServerConfig>) => Promise<void>; init: (ctx: BackendContext<APIServerConfig>) => void;
run: (hostname: string, port: number) => void; run: (hostname: string, port: number) => void;
end: () => void; end: () => void;
}; };