122 lines
3.7 KiB
TypeScript
122 lines
3.7 KiB
TypeScript
|
|
import {createServer, type IncomingMessage, type ServerResponse} from 'node:http';
|
||
|
|
import {readFile, stat} from 'node:fs/promises';
|
||
|
|
import path from 'node:path';
|
||
|
|
import {fileURLToPath} from 'node:url';
|
||
|
|
import {createDeepSeekMiddleware} from './deepseekProxy';
|
||
|
|
import {createBaiduAsrMiddleware} from './baiduAsrProxy';
|
||
|
|
|
||
|
|
const host = process.env.HOST || '0.0.0.0';
|
||
|
|
const port = Number(process.env.PORT || 9000);
|
||
|
|
const distDirectory = fileURLToPath(new URL('../dist/', import.meta.url));
|
||
|
|
const apiHandler = createDeepSeekMiddleware();
|
||
|
|
const asrHandler = createBaiduAsrMiddleware();
|
||
|
|
|
||
|
|
const contentTypes: Record<string, string> = {
|
||
|
|
'.css': 'text/css; charset=utf-8',
|
||
|
|
'.html': 'text/html; charset=utf-8',
|
||
|
|
'.ico': 'image/x-icon',
|
||
|
|
'.js': 'text/javascript; charset=utf-8',
|
||
|
|
'.json': 'application/json; charset=utf-8',
|
||
|
|
'.map': 'application/json; charset=utf-8',
|
||
|
|
'.png': 'image/png',
|
||
|
|
'.svg': 'image/svg+xml',
|
||
|
|
'.webp': 'image/webp',
|
||
|
|
};
|
||
|
|
|
||
|
|
const sendText = (res: ServerResponse, statusCode: number, message: string) => {
|
||
|
|
res.statusCode = statusCode;
|
||
|
|
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
||
|
|
res.end(message);
|
||
|
|
};
|
||
|
|
|
||
|
|
const resolveAssetPath = (pathname: string) => {
|
||
|
|
const relativePath = pathname === '/' ? 'index.html' : pathname.replace(/^\/+/, '');
|
||
|
|
const resolvedPath = path.resolve(distDirectory, relativePath);
|
||
|
|
const allowedPrefix = `${path.resolve(distDirectory)}${path.sep}`;
|
||
|
|
return resolvedPath.startsWith(allowedPrefix) ? resolvedPath : null;
|
||
|
|
};
|
||
|
|
|
||
|
|
const serveFile = async (req: IncomingMessage, res: ServerResponse, filePath: string) => {
|
||
|
|
const fileInfo = await stat(filePath);
|
||
|
|
if (!fileInfo.isFile()) {
|
||
|
|
throw new Error('Not a file');
|
||
|
|
}
|
||
|
|
const extension = path.extname(filePath);
|
||
|
|
res.statusCode = 200;
|
||
|
|
res.setHeader('Content-Type', contentTypes[extension] || 'application/octet-stream');
|
||
|
|
res.setHeader('Cache-Control', filePath.endsWith('index.html') ? 'no-cache' : 'public, max-age=31536000, immutable');
|
||
|
|
if (req.method === 'HEAD') {
|
||
|
|
res.end();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
res.end(await readFile(filePath));
|
||
|
|
};
|
||
|
|
|
||
|
|
const handleStaticRequest = async (req: IncomingMessage, res: ServerResponse, pathname: string) => {
|
||
|
|
const filePath = resolveAssetPath(pathname);
|
||
|
|
if (!filePath) {
|
||
|
|
sendText(res, 403, 'Forbidden');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
await serveFile(req, res, filePath);
|
||
|
|
} catch {
|
||
|
|
if (path.extname(pathname)) {
|
||
|
|
sendText(res, 404, 'Not Found');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
await serveFile(req, res, path.join(distDirectory, 'index.html'));
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
const server = createServer(async (req, res) => {
|
||
|
|
const requestUrl = new URL(req.url || '/', `http://${req.headers.host || 'localhost'}`);
|
||
|
|
const {pathname} = requestUrl;
|
||
|
|
|
||
|
|
if (pathname === '/healthz') {
|
||
|
|
res.statusCode = 200;
|
||
|
|
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
||
|
|
res.setHeader('Cache-Control', 'no-store');
|
||
|
|
res.end('{"status":"ok"}');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (pathname === '/api/deepseek/chat') {
|
||
|
|
await apiHandler(req, res);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (pathname === '/api/baidu/asr') {
|
||
|
|
await asrHandler(req, res);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (pathname.startsWith('/api/')) {
|
||
|
|
sendText(res, 404, 'Not Found');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
||
|
|
res.setHeader('Allow', 'GET, HEAD');
|
||
|
|
sendText(res, 405, 'Method Not Allowed');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
await handleStaticRequest(req, res, decodeURIComponent(pathname));
|
||
|
|
} catch {
|
||
|
|
sendText(res, 500, 'Internal Server Error');
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
server.listen(port, host, () => {
|
||
|
|
console.info(`Digital human server listening on http://${host}:${port}`);
|
||
|
|
});
|
||
|
|
|
||
|
|
const shutdown = () => {
|
||
|
|
server.close(() => process.exit(0));
|
||
|
|
};
|
||
|
|
process.on('SIGINT', shutdown);
|
||
|
|
process.on('SIGTERM', shutdown);
|