|
|
@@ -0,0 +1,388 @@
|
|
|
+import 'dotenv/config'
|
|
|
+import fs from 'node:fs/promises';
|
|
|
+import { URL } from 'node:url';
|
|
|
+import { Transform } from 'node:stream';
|
|
|
+import { Readable } from 'node:stream';
|
|
|
+import { Hono } from 'hono';
|
|
|
+import { logger } from 'hono/logger';
|
|
|
+import { createServer as createNodeServer } from 'node:http';
|
|
|
+import process from 'node:process';
|
|
|
+import { createAdaptorServer } from '@hono/node-server'
|
|
|
+import { parse } from 'regexparam';
|
|
|
+
|
|
|
+// 创建 Hono 应用
|
|
|
+const app = new Hono();
|
|
|
+
|
|
|
+// 全局使用 Hono 日志中间件
|
|
|
+app.use('*', logger());
|
|
|
+
|
|
|
+// 常量定义
|
|
|
+const isProduction = process.env.NODE_ENV === 'production';
|
|
|
+const port = process.env.PORT || 8080;
|
|
|
+const base = process.env.BASE || '/';
|
|
|
+const ABORT_DELAY = 10000;
|
|
|
+
|
|
|
+console.log('========================================');
|
|
|
+console.log('开始初始化服务器...');
|
|
|
+console.log(`环境: ${isProduction ? '生产环境' : '开发环境'}`);
|
|
|
+console.log(`端口: ${port}`);
|
|
|
+console.log(`基础路径: ${base}`);
|
|
|
+console.log('========================================');
|
|
|
+
|
|
|
+// 解析基础路径为 URL 对象
|
|
|
+const baseUrl = new URL(base, `http://localhost:${port}`);
|
|
|
+console.log(`基础URL解析完成: ${baseUrl.href}`);
|
|
|
+
|
|
|
+// 创建服务器实例
|
|
|
+console.log('正在创建服务器实例...');
|
|
|
+const parentServer = createAdaptorServer({
|
|
|
+ fetch: app.fetch,
|
|
|
+ createServer: createNodeServer,
|
|
|
+ port: port,
|
|
|
+})
|
|
|
+console.log('服务器实例创建成功');
|
|
|
+
|
|
|
+// 生产环境中间件
|
|
|
+let compressionMiddleware;
|
|
|
+let sirvMiddleware;
|
|
|
+if (isProduction) {
|
|
|
+ console.log('生产环境: 加载压缩和静态文件中间件...');
|
|
|
+ compressionMiddleware = (await import('compression')).default();
|
|
|
+ sirvMiddleware = (await import('sirv')).default('./dist/client', {
|
|
|
+ extensions: [],
|
|
|
+ baseUrl: base
|
|
|
+ });
|
|
|
+ console.log('生产环境中间件加载完成');
|
|
|
+}
|
|
|
+
|
|
|
+// Vite 开发服务器
|
|
|
+/** @type {import('vite').ViteDevServer | undefined} */
|
|
|
+let vite;
|
|
|
+if (!isProduction) {
|
|
|
+ console.log('开发环境: 初始化 Vite 开发服务器...');
|
|
|
+ const { createServer } = await import('vite');
|
|
|
+ vite = await createServer({
|
|
|
+ server: {
|
|
|
+ middlewareMode: {
|
|
|
+ server: parentServer
|
|
|
+ },
|
|
|
+ hmr: {
|
|
|
+ port: 8081,
|
|
|
+ clientPort: 443,
|
|
|
+ path: 'vite-hmr'
|
|
|
+ },
|
|
|
+ proxy: {
|
|
|
+ '/vite-hmr': {
|
|
|
+ target: 'ws://localhost:8081',
|
|
|
+ ws: true,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ appType: 'custom',
|
|
|
+ base,
|
|
|
+ });
|
|
|
+ console.log('Vite 开发服务器初始化完成');
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+// 开发环境模板处理函数 - 仅处理模板转换
|
|
|
+const processDevTemplate = async (template) => {
|
|
|
+ if (!isProduction && vite) {
|
|
|
+ console.log('开发环境: 处理模板...');
|
|
|
+ const processedTemplate = await vite.transformIndexHtml('/', template);
|
|
|
+ console.log('开发环境模板处理完成');
|
|
|
+ return processedTemplate;
|
|
|
+ }
|
|
|
+ return template;
|
|
|
+};
|
|
|
+
|
|
|
+// 生产环境模板处理函数 - 仅处理资源路径替换
|
|
|
+const processProdTemplate = async (template) => {
|
|
|
+ console.log('生产环境: 处理模板资源路径...');
|
|
|
+ try {
|
|
|
+ // 读取 manifest
|
|
|
+ const manifestPath = new URL('./dist/client/.vite/manifest.json', import.meta.url);
|
|
|
+ const manifestContent = await fs.readFile(manifestPath, 'utf-8');
|
|
|
+ const manifest = JSON.parse(manifestContent);
|
|
|
+ console.log('生产环境: 成功读取 manifest.json');
|
|
|
+
|
|
|
+ // 获取 src/client/index.tsx 对应的资源信息
|
|
|
+ const indexManifest = manifest['src/client/index.tsx'];
|
|
|
+ if (!indexManifest) {
|
|
|
+ throw new Error('manifest 中未找到 src/client/index.tsx 入口配置');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取 src/style.css 对应的资源信息
|
|
|
+ const styleManifest = manifest['src/style.css'];
|
|
|
+ if (!styleManifest) {
|
|
|
+ throw new Error('manifest 中未找到 src/style.css 入口配置');
|
|
|
+ }
|
|
|
+
|
|
|
+ const cssPath = new URL(styleManifest.file, baseUrl).pathname;
|
|
|
+ const cssLinks = `<link href="${cssPath}" rel="stylesheet" />`;
|
|
|
+
|
|
|
+ // 替换入口脚本
|
|
|
+ const jsEntryPath = new URL(indexManifest.file, baseUrl).pathname;
|
|
|
+ const entryScript = `<script type="module" src="${jsEntryPath}"></script>`;
|
|
|
+
|
|
|
+ // 执行替换
|
|
|
+ const processedTemplate = template
|
|
|
+ .replace(/<link href="\/src\/style.css" rel="stylesheet"\/>/, cssLinks)
|
|
|
+ .replace(/<script type="module" src="\/src\/client\/index.tsx"><\/script>/, entryScript);
|
|
|
+
|
|
|
+ console.log('生产环境模板处理完成');
|
|
|
+ return processedTemplate;
|
|
|
+
|
|
|
+ } catch (err) {
|
|
|
+ console.error('生产环境模板处理失败:', err);
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+// SSR 渲染中间件函数
|
|
|
+const createSsrHandler = (template, render, normalizedUrl) => {
|
|
|
+ return async (c) => {
|
|
|
+ let didError = false;
|
|
|
+ let abortController;
|
|
|
+
|
|
|
+ // 创建一个可读流用于 SSR 渲染内容
|
|
|
+ const [htmlStart, htmlEnd] = template.split(`<!--app-html-->`);
|
|
|
+ const ssrStream = new Readable({ read: () => {} });
|
|
|
+
|
|
|
+ // 写入 HTML 头部
|
|
|
+ ssrStream.push(htmlStart);
|
|
|
+
|
|
|
+ // 设置响应头和状态码
|
|
|
+ c.header('Content-Type', 'text/html');
|
|
|
+
|
|
|
+ // 处理渲染
|
|
|
+ const { pipe, abort } = render(normalizedUrl, {
|
|
|
+ onShellError() {
|
|
|
+ didError = true;
|
|
|
+ c.status(500);
|
|
|
+ ssrStream.push('<h1>服务器渲染出错</h1>');
|
|
|
+ ssrStream.push(null); // 结束流
|
|
|
+ },
|
|
|
+ onShellReady() {
|
|
|
+ console.log(`开始渲染页面: ${normalizedUrl}`);
|
|
|
+ // 将渲染结果通过管道传入 ssrStream
|
|
|
+ const transformStream = new Transform({
|
|
|
+ transform(chunk, encoding, callback) {
|
|
|
+ ssrStream.push(chunk, encoding);
|
|
|
+ callback();
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ pipe(transformStream);
|
|
|
+
|
|
|
+ // 当 transformStream 完成时,添加 HTML 尾部
|
|
|
+ transformStream.on('finish', () => {
|
|
|
+ ssrStream.push(htmlEnd);
|
|
|
+ ssrStream.push(null); // 结束流
|
|
|
+ console.log(`页面渲染完成: ${normalizedUrl}`);
|
|
|
+ });
|
|
|
+ },
|
|
|
+ onError(error) {
|
|
|
+ didError = true;
|
|
|
+ console.error('渲染过程出错:', error);
|
|
|
+ },
|
|
|
+ });
|
|
|
+
|
|
|
+ // // 设置超时中止
|
|
|
+ // abortController = new AbortController();
|
|
|
+ // const abortTimeout = setTimeout(() => {
|
|
|
+ // console.log(`渲染超时,终止请求: ${normalizedUrl}`);
|
|
|
+ // abort();
|
|
|
+ // abortController.abort();
|
|
|
+ // }, ABORT_DELAY);
|
|
|
+
|
|
|
+ // 将流通过 Hono 响应返回
|
|
|
+ return c.body(
|
|
|
+ ssrStream
|
|
|
+ );
|
|
|
+ };
|
|
|
+};
|
|
|
+
|
|
|
+// 生产环境:直接加载API路由和模板
|
|
|
+let productionApiModule = null;
|
|
|
+let productionTemplate = null;
|
|
|
+
|
|
|
+// 生产环境初始化
|
|
|
+const initProduction = async () => {
|
|
|
+ if (isProduction && !productionApiModule) {
|
|
|
+ try {
|
|
|
+ console.log('生产环境: 加载API路由和模板...');
|
|
|
+ const module = await import('./dist/server/index.js');
|
|
|
+ productionApiModule = module;
|
|
|
+ productionTemplate = await processProdTemplate(module.template);
|
|
|
+
|
|
|
+ // 1. 先挂载API路由(优先匹配)
|
|
|
+ app.route('/', module.api);
|
|
|
+ console.log('生产环境: API路由已挂载到主应用');
|
|
|
+
|
|
|
+ // 2. 再注册handleProduction作为兜底中间件(仅处理未被API匹配的请求)
|
|
|
+ app.use(async (c) => {
|
|
|
+ return handleProduction(c);
|
|
|
+ });
|
|
|
+ console.log('生产环境: 兜底中间件已注册');
|
|
|
+ } catch (err) {
|
|
|
+ console.error('生产环境: API路由加载失败:', err);
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+// 开发环境请求处理
|
|
|
+const handleDevelopment = async (c) => {
|
|
|
+ try {
|
|
|
+ const req = c.env.incoming;
|
|
|
+ const res = c.env.outgoing;
|
|
|
+ const url = new URL(req.url, `http://${req.headers.host}`);
|
|
|
+ const path = url.pathname;
|
|
|
+
|
|
|
+ // 检查是否匹配基础路径
|
|
|
+ if (!path.startsWith(baseUrl.pathname)) {
|
|
|
+ return c.text('未找到', 404);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理基础路径
|
|
|
+ const normalizedUrl = path.replace(baseUrl.pathname, '/') || '/';
|
|
|
+
|
|
|
+ // 使用 Vite 中间件处理请求
|
|
|
+ const handled = await new Promise((resolve) => {
|
|
|
+ vite.middlewares(req, res, () => resolve(false));
|
|
|
+ });
|
|
|
+
|
|
|
+ if (handled) {
|
|
|
+ return c.body;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 动态加载最新 API 模块
|
|
|
+ const apiModule = await vite.ssrLoadModule('./src/server/index.tsx');
|
|
|
+
|
|
|
+ // 创建临时子应用并挂载路由
|
|
|
+ const apiApp = new Hono();
|
|
|
+
|
|
|
+ apiApp.route('/', apiModule.api);
|
|
|
+
|
|
|
+ // 处理开发环境模板
|
|
|
+ const template = await processDevTemplate(apiModule.template);
|
|
|
+ apiApp.use(createSsrHandler(template, apiModule.render, normalizedUrl));
|
|
|
+
|
|
|
+ return apiApp.fetch(c.req.raw, {
|
|
|
+ ...c.env,
|
|
|
+ incoming: c.env.incoming,
|
|
|
+ outgoing: c.env.outgoing
|
|
|
+ });
|
|
|
+ } catch (e) {
|
|
|
+ if (vite) {
|
|
|
+ vite.ssrFixStacktrace(e);
|
|
|
+ }
|
|
|
+ console.error('开发环境请求处理错误:', e.stack);
|
|
|
+ return c.text('服务器内部错误', 500);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+// 生产环境:处理压缩中间件
|
|
|
+const handleCompression = async (c) => {
|
|
|
+ const req = c.env.incoming;
|
|
|
+ const res = c.env.outgoing;
|
|
|
+
|
|
|
+ const compressed = await new Promise((resolve) => {
|
|
|
+ compressionMiddleware(req, res, () => resolve(false));
|
|
|
+ });
|
|
|
+
|
|
|
+ return compressed ? c.body : null;
|
|
|
+};
|
|
|
+
|
|
|
+// 生产环境:处理静态文件
|
|
|
+const handleStaticFiles = async (c) => {
|
|
|
+ const req = c.env.incoming;
|
|
|
+ const res = c.env.outgoing;
|
|
|
+
|
|
|
+ const served = await new Promise((resolve) => {
|
|
|
+ sirvMiddleware(req, res, () => resolve(false));
|
|
|
+ });
|
|
|
+
|
|
|
+ return served ? c.body : null;
|
|
|
+};
|
|
|
+
|
|
|
+// 生产环境:处理SSR渲染
|
|
|
+const handleSsrRendering = async (c) => {
|
|
|
+ try {
|
|
|
+ const normalizedUrl = c.req.path.replace(baseUrl.pathname, '/') || '/';
|
|
|
+ return createSsrHandler(productionTemplate, productionApiModule.render, normalizedUrl)(c);
|
|
|
+ } catch (err) {
|
|
|
+ console.error('生产环境SSR渲染错误:', err);
|
|
|
+ return c.text('服务器内部错误', 500);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+// 生产环境请求处理
|
|
|
+const handleProduction = async (c) => {
|
|
|
+ try {
|
|
|
+ // 1. 处理压缩
|
|
|
+ const compressionResult = await handleCompression(c);
|
|
|
+ if (compressionResult) return compressionResult;
|
|
|
+
|
|
|
+ // 2. 处理静态文件
|
|
|
+ const staticResult = await handleStaticFiles(c);
|
|
|
+ if (staticResult) return staticResult;
|
|
|
+
|
|
|
+ // 3. 处理SSR渲染(作为最后手段)
|
|
|
+ return await handleSsrRendering(c);
|
|
|
+ } catch (err) {
|
|
|
+ console.error('生产环境请求处理错误:', err);
|
|
|
+ return c.text('服务器内部错误', 500);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+// 启动服务器
|
|
|
+console.log('准备启动服务器...');
|
|
|
+
|
|
|
+// 生产环境初始化
|
|
|
+if (isProduction) {
|
|
|
+ await initProduction();
|
|
|
+}else{
|
|
|
+ app.use(async (c) => {
|
|
|
+ return handleDevelopment(c);
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+parentServer.listen(port, () => {
|
|
|
+ console.log('========================================');
|
|
|
+ console.log(`服务器已成功启动!`);
|
|
|
+ console.log(`访问地址: http://localhost:${port}`);
|
|
|
+ console.log(`环境: ${isProduction ? '生产环境' : '开发环境'}`);
|
|
|
+ console.log('========================================');
|
|
|
+})
|
|
|
+
|
|
|
+// 统一的服务器关闭处理函数
|
|
|
+const shutdownServer = async () => {
|
|
|
+ console.log('正在关闭服务器...');
|
|
|
+
|
|
|
+ // 1. 先关闭 Vite 开发服务器(包括 HMR 服务)
|
|
|
+ if (!isProduction && vite) {
|
|
|
+ console.log('正在关闭 Vite 开发服务器(包括 HMR 服务)...');
|
|
|
+ try {
|
|
|
+ await vite.close();
|
|
|
+ console.log('Vite 开发服务器已关闭');
|
|
|
+ } catch (err) {
|
|
|
+ console.error('关闭 Vite 服务器时出错:', err);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 关闭主服务器
|
|
|
+ parentServer.close((err) => {
|
|
|
+ if (err) {
|
|
|
+ console.error('关闭主服务器时出错:', err);
|
|
|
+ process.exit(1);
|
|
|
+ }
|
|
|
+ console.log('主服务器已关闭');
|
|
|
+ process.exit(0);
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+// 处理进程终止信号
|
|
|
+process.on('SIGINT', shutdownServer);
|
|
|
+process.on('SIGTERM', shutdownServer);
|