fix: urls

This commit is contained in:
xxnuo
2026-01-01 17:01:23 +08:00
parent 212905899b
commit 4daafca5ab
3 changed files with 22 additions and 13 deletions

View File

@@ -15,13 +15,9 @@ const mimeTypes: Record<string, string> = {
'.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);

View File

@@ -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}`);
});

View File

@@ -4,7 +4,7 @@ import path from "path"
import tailwindcss from "@tailwindcss/vite"
export default defineConfig({
base: '/',
base: '/ui/',
plugins: [react(), tailwindcss()],
resolve: {
alias: {