From 4daafca5abbc2914126ccaa2f9371c05ed2cf6ac Mon Sep 17 00:00:00 2001 From: xxnuo <54252779+xxnuo@users.noreply.github.com> Date: Thu, 1 Jan 2026 17:01:23 +0800 Subject: [PATCH] fix: urls --- src/middleware/ui.ts | 7 +------ src/server/index.ts | 26 ++++++++++++++++++++------ ui/vite.config.ts | 2 +- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/middleware/ui.ts b/src/middleware/ui.ts index 12ca34d..374ef56 100644 --- a/src/middleware/ui.ts +++ b/src/middleware/ui.ts @@ -15,13 +15,9 @@ const mimeTypes: Record = { '.ico': 'image/x-icon', }; -export const UI: RequestHandler = async (req: Request, res: Response, next: NextFunction) => { +export const uiStatic: RequestHandler = async (req: Request, res: Response, next: NextFunction) => { let filePath = req.path; - if (filePath.startsWith('/ui/')) { - filePath = filePath.substring(3); - } - if (filePath === '/' || filePath === '') { filePath = '/index.html'; } @@ -31,7 +27,6 @@ export const UI: RequestHandler = async (req: Request, res: Response, next: Next if (assetPath) { const ext = filePath.substring(filePath.lastIndexOf('.')); const mimeType = mimeTypes[ext] || 'application/octet-stream'; - res.setHeader('Content-Type', mimeType); const buffer = await readFile(assetPath); res.send(buffer); diff --git a/src/server/index.ts b/src/server/index.ts index c95d7b7..3fc54a8 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -1,4 +1,4 @@ -import express from 'express'; +import express, { Request, Response, NextFunction } from 'express'; import fs from 'fs/promises'; import swaggerUi from 'swagger-ui-express'; import { getConfig } from '@/config/index.js'; @@ -9,7 +9,7 @@ import { cleanupLegacyBin } from '@/assets/index.js'; import { requestId, errorHandler, cors } from '@/middleware/index.js'; import { RegisterRoutes } from '@/generated/routes.js'; import swaggerDocument from '@/generated/swagger.json'; -import { UI } from '@/middleware/ui.js'; +import { uiStatic } from '@/middleware/ui.js'; import { swaggerStatic } from '@/middleware/swagger.js'; export async function run() { @@ -33,15 +33,29 @@ export async function run() { RegisterRoutes(app); - app.use('/docs', swaggerStatic, swaggerUi.serve, swaggerUi.setup(swaggerDocument)); + app.use('/ui', (req: Request, res: Response, next: NextFunction) => { + if (req.originalUrl === '/ui') { + return res.redirect(301, '/ui/'); + } + next(); + }, uiStatic); - app.use(UI); + app.use('/docs', (req: Request, res: Response, next: NextFunction) => { + if (req.originalUrl === '/docs') { + return res.redirect(301, '/docs/'); + } + next(); + }, swaggerStatic, swaggerUi.serve, swaggerUi.setup(swaggerDocument)); + + app.get('/', (_, res) => res.redirect(301, '/ui/')); + + app.use((_, res) => res.status(404).send('404')); app.use(errorHandler()); const server = app.listen(parseInt(config.port), config.host, () => { - logger.important(`HTTP Service URL: http://${config.host}:${config.port}`); - logger.important(`Swagger Docs: http://${config.host}:${config.port}/docs`); + logger.important(`Web UI: http://${config.host}:${config.port}/ui/`); + logger.important(`Swagger Docs: http://${config.host}:${config.port}/docs/`); logger.important(`Log level set to: ${config.logLevel}`); }); diff --git a/ui/vite.config.ts b/ui/vite.config.ts index 87b3d52..e3a47ba 100644 --- a/ui/vite.config.ts +++ b/ui/vite.config.ts @@ -4,7 +4,7 @@ import path from "path" import tailwindcss from "@tailwindcss/vite" export default defineConfig({ - base: '/', + base: '/ui/', plugins: [react(), tailwindcss()], resolve: { alias: {