50 lines
1.7 KiB
JavaScript
50 lines
1.7 KiB
JavaScript
function assertRouter(api, attachName) {
|
|
if (!api || typeof api.get !== 'function') {
|
|
throw new Error(`${attachName} requires an Express-compatible router`);
|
|
}
|
|
}
|
|
|
|
export function attachPortalImageMakeRuntimeConfigRoute(
|
|
api,
|
|
{
|
|
getImageMakeAdminConfigService = () => null,
|
|
} = {},
|
|
) {
|
|
assertRouter(api, 'attachPortalImageMakeRuntimeConfigRoute');
|
|
|
|
api.get('/internal/image-make/runtime-config', async (req, res) => {
|
|
const imageMakeAdminConfigService = getImageMakeAdminConfigService();
|
|
if (!imageMakeAdminConfigService?.authorizeRuntimeRequest) {
|
|
return res.status(503).json({ message: 'image_make 配置服务未启用' });
|
|
}
|
|
const authHeader = String(req.get('authorization') ?? '');
|
|
const token = authHeader.startsWith('Bearer ') ? authHeader.slice(7).trim() : '';
|
|
if (!imageMakeAdminConfigService.authorizeRuntimeRequest(token)) {
|
|
return res.status(401).json({ message: '未授权' });
|
|
}
|
|
const runtime = await imageMakeAdminConfigService.getRuntimeConfig();
|
|
if (!runtime.ok) {
|
|
return res.status(503).json({ message: runtime.message ?? 'image_make 运行时配置无效' });
|
|
}
|
|
return res.json(runtime);
|
|
});
|
|
}
|
|
|
|
export function attachPortalBlockedWordsRoute(
|
|
api,
|
|
{
|
|
waitForUserAuthReady = async () => {},
|
|
getWordFilterService = () => null,
|
|
} = {},
|
|
) {
|
|
assertRouter(api, 'attachPortalBlockedWordsRoute');
|
|
|
|
api.get('/config/blocked-words', async (_req, res) => {
|
|
await waitForUserAuthReady();
|
|
const wordFilterService = getWordFilterService();
|
|
if (!wordFilterService) return res.json({ words: [] });
|
|
const words = await wordFilterService.listAllForFrontend();
|
|
res.json({ words });
|
|
});
|
|
}
|