app.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /** @jsxImportSource https://esm.d8d.fun/hono@4.7.4/jsx */
  2. import { Hono } from 'hono'
  3. import React from 'hono/jsx'
  4. import type { Context as HonoContext } from 'hono'
  5. import { serveStatic } from 'hono/deno'
  6. import { APIClient } from '@d8d-appcontainer/api'
  7. import debug from "debug"
  8. import dayjs from 'dayjs';
  9. import utc from 'dayjs/plugin/utc';
  10. import type { GlobalConfig } from '../client/share/types.ts';
  11. import { OssType, MapMode } from '../client/share/types.ts';
  12. import { createRouter } from './router.ts'
  13. import { startMonitoring } from "./utils/monitor.ts";
  14. dayjs.extend(utc)
  15. // 初始化debug实例
  16. const log = {
  17. app: debug('app:server'),
  18. auth: debug('auth:server'),
  19. api: debug('api:server'),
  20. debug: debug('debug:server')
  21. }
  22. const GLOBAL_CONFIG: GlobalConfig = {
  23. OSS_BASE_URL: Deno.env.get('OSS_BASE_URL') || 'https://d8d-appcontainer-user.oss-cn-beijing.aliyuncs.com',
  24. OSS_TYPE: Deno.env.get('OSS_TYPE') === OssType.MINIO ? OssType.MINIO : OssType.ALIYUN,
  25. API_BASE_URL: '/api',
  26. APP_NAME: Deno.env.get('APP_NAME') || '应用Starter',
  27. ENV: Deno.env.get('ENV') || 'development',
  28. DEFAULT_THEME: 'light', // 默认主题
  29. MAP_CONFIG: {
  30. KEY: Deno.env.get('AMAP_KEY') || '您的地图API密钥',
  31. VERSION: '2.0',
  32. PLUGINS: ['AMap.ToolBar', 'AMap.Scale', 'AMap.HawkEye', 'AMap.MapType', 'AMap.Geolocation'],
  33. MAP_MODE: Deno.env.get('MAP_MODE') === MapMode.OFFLINE ? MapMode.OFFLINE : MapMode.ONLINE,
  34. },
  35. CHART_THEME: 'default', // 图表主题
  36. ENABLE_THEME_CONFIG: false, // 主题配置开关
  37. THEME: null
  38. };
  39. log.app.enabled = true
  40. log.auth.enabled = true
  41. log.api.enabled = true
  42. log.debug.enabled = true
  43. interface EsmScriptConfig {
  44. src: string
  45. href: string
  46. denoJson: string
  47. refresh: boolean
  48. prodPath?: string
  49. prodSrc?: string
  50. }
  51. // 定义模块参数接口
  52. interface ModuleParams {
  53. apiClient: APIClient
  54. app: Hono
  55. moduleDir: string
  56. }
  57. export default function({ apiClient, app, moduleDir }: ModuleParams) {
  58. const honoApp = app
  59. // 创建路由
  60. const router = createRouter(apiClient, moduleDir)
  61. honoApp.route('/', router)
  62. // 首页路由 - SSR
  63. honoApp.get('/', async (c: HonoContext) => {
  64. const systemName = GLOBAL_CONFIG.APP_NAME
  65. return c.html(
  66. <html>
  67. <head>
  68. <title>{systemName}</title>
  69. <meta charset="UTF-8" />
  70. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  71. <script src="https://cdn.tailwindcss.com"></script>
  72. </head>
  73. <body>
  74. <div className="min-h-screen bg-gray-50 flex items-center justify-center py-12 px-4 sm:px-6 lg:px-8">
  75. <div className="max-w-md w-full space-y-8">
  76. {/* 系统介绍区域 */}
  77. <div className="text-center">
  78. <h1 className="text-4xl font-bold text-gray-900 mb-4">
  79. {systemName}
  80. </h1>
  81. <p className="text-lg text-gray-600 mb-8">
  82. 全功能应用Starter
  83. </p>
  84. <p className="text-base text-gray-500 mb-8">
  85. 这是一个基于Hono和React的应用Starter,提供了用户认证、文件管理、图表分析、地图集成和主题切换等常用功能。
  86. </p>
  87. </div>
  88. {/* 管理入口按钮 */}
  89. <div className="space-y-4">
  90. <a
  91. href="/admin"
  92. className="w-full flex justify-center py-3 px-4 border border-transparent rounded-md shadow-sm text-lg font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
  93. >
  94. 进入管理后台
  95. </a>
  96. {/* 移动端入口按钮 */}
  97. <a
  98. href="/mobile"
  99. className="w-full flex justify-center py-3 px-4 border border-blue-600 rounded-md shadow-sm text-lg font-medium text-blue-600 bg-white hover:bg-blue-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
  100. >
  101. 进入移动端
  102. </a>
  103. {/* 迁移管理入口按钮 */}
  104. <a
  105. href="/migrations"
  106. className="w-full flex justify-center py-3 px-4 border border-red-600 rounded-md shadow-sm text-lg font-medium text-red-600 bg-white hover:bg-red-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500"
  107. >
  108. 数据库迁移
  109. </a>
  110. </div>
  111. </div>
  112. </div>
  113. </body>
  114. </html>
  115. )
  116. })
  117. // 创建一个函数,用于生成包含全局配置的HTML页面
  118. const createHtmlWithConfig = (scriptConfig: EsmScriptConfig, title = '应用Starter') => {
  119. return (c: HonoContext) => {
  120. const isProd = GLOBAL_CONFIG.ENV === 'production';
  121. const isLocalDeploy = Deno.env.get('IS_LOCAL_DEPLOY') === 'true';
  122. return c.html(
  123. <html lang="zh-CN">
  124. <head>
  125. <meta charset="UTF-8" />
  126. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  127. {isProd && <meta name="version" content={Deno.env.get('VERSION') || '0.1.0'} />}
  128. <title>{title}</title>
  129. {isLocalDeploy ? (
  130. <script type="module" src={scriptConfig.prodSrc || `/client_dist/${scriptConfig.prodPath}`}></script>
  131. ) : (
  132. <script src={scriptConfig.src} href={scriptConfig.href} deno-json={scriptConfig.denoJson}
  133. {...isProd ? {}:{ refresh: true }}
  134. ></script>
  135. )}
  136. {isLocalDeploy ? (<script src="/tailwindcss@3.4.16/index.js"></script>) : (<script src="https://cdn.tailwindcss.com"></script>)}
  137. <script dangerouslySetInnerHTML={{ __html: `window.CONFIG = ${JSON.stringify(GLOBAL_CONFIG)};` }} />
  138. {isProd && (
  139. <>
  140. <script src="https://ai-oss.d8d.fun/umd/vconsole.3.15.1.min.js"></script>
  141. <script dangerouslySetInnerHTML={{ __html: `
  142. const urlParams = new URLSearchParams(window.location.search);
  143. if (urlParams.has('vconsole')) {
  144. var vConsole = new VConsole({
  145. theme: urlParams.get('vconsole_theme') || 'light',
  146. onReady: function() {
  147. console.log('vConsole is ready');
  148. }
  149. });
  150. }
  151. `}} />
  152. </>
  153. )}
  154. {!isProd && (
  155. <>
  156. <script src="https://ai-oss.d8d.fun/umd/vconsole.3.15.1.min.js"></script>
  157. <script dangerouslySetInnerHTML={{ __html: `
  158. const urlParams = new URLSearchParams(window.location.search);
  159. var vConsole = new VConsole({
  160. theme: urlParams.get('vconsole_theme') || 'light',
  161. onReady: function() {
  162. console.log('vConsole is ready');
  163. }
  164. });
  165. `}} />
  166. </>
  167. )}
  168. </head>
  169. <body className="bg-gray-50">
  170. <div id="root"></div>
  171. </body>
  172. </html>
  173. )
  174. }
  175. }
  176. // 后台管理路由
  177. honoApp.get('/admin', createHtmlWithConfig({
  178. src: "https://esm.d8d.fun/xb",
  179. href: "/client/admin/web_app.tsx",
  180. denoJson: "/deno.json",
  181. refresh: true,
  182. prodPath: "admin/web_app.js"
  183. }, GLOBAL_CONFIG.APP_NAME))
  184. honoApp.get('/admin/*', createHtmlWithConfig({
  185. src: "https://esm.d8d.fun/xb",
  186. href: "/client/admin/web_app.tsx",
  187. denoJson: "/deno.json",
  188. refresh: true,
  189. prodPath: "admin/web_app.js"
  190. }, GLOBAL_CONFIG.APP_NAME))
  191. // 移动端路由
  192. honoApp.get('/mobile', createHtmlWithConfig({
  193. src: "https://esm.d8d.fun/xb",
  194. href: "/client/mobile/mobile_app.tsx",
  195. denoJson: "/deno.json",
  196. refresh: true,
  197. prodPath: "mobile/mobile_app.js"
  198. }, GLOBAL_CONFIG.APP_NAME))
  199. honoApp.get('/mobile/*', createHtmlWithConfig({
  200. src: "https://esm.d8d.fun/xb",
  201. href: "/client/mobile/mobile_app.tsx",
  202. denoJson: "/deno.json",
  203. refresh: true,
  204. prodPath: "mobile/mobile_app.js"
  205. }, GLOBAL_CONFIG.APP_NAME))
  206. // 迁移管理路由
  207. honoApp.get('/migrations', createHtmlWithConfig({
  208. src: "https://esm.d8d.fun/xb",
  209. href: "/client/migrations/migrations_app.tsx",
  210. denoJson: "/deno.json",
  211. refresh: true,
  212. prodPath: "migrations/migrations_app.js"
  213. }, GLOBAL_CONFIG.APP_NAME))
  214. honoApp.get('/migrations/*', createHtmlWithConfig({
  215. src: "https://esm.d8d.fun/xb",
  216. href: "/client/migrations/migrations_app.tsx",
  217. denoJson: "/deno.json",
  218. refresh: true,
  219. prodPath: "migrations/migrations_app.js"
  220. }, GLOBAL_CONFIG.APP_NAME))
  221. // 大屏路由 - 使用生成的HTML模板
  222. honoApp.get('/big', createHtmlWithConfig({
  223. src: "https://esm.d8d.fun/xb",
  224. href: "/client/big/client.tsx",
  225. denoJson: "/deno.json",
  226. refresh: true,
  227. prodPath: "big/client.js"
  228. }, GLOBAL_CONFIG.APP_NAME))
  229. const staticRoutes = serveStatic({
  230. root: moduleDir,
  231. onFound: async (path: string, c: HonoContext) => {
  232. const fileExt = path.split('.').pop()?.toLowerCase()
  233. if (fileExt === 'tsx' || fileExt === 'ts') {
  234. c.header('Content-Type', 'text/typescript; charset=utf-8')
  235. } else if (fileExt === 'js' || fileExt === 'mjs') {
  236. c.header('Content-Type', 'application/javascript; charset=utf-8')
  237. } else if (fileExt === 'json') {
  238. c.header('Content-Type', 'application/json; charset=utf-8')
  239. } else if (fileExt === 'html') {
  240. c.header('Content-Type', 'text/html; charset=utf-8')
  241. } else if (fileExt === 'css') {
  242. c.header('Content-Type', 'text/css; charset=utf-8')
  243. } else if (['jpg', 'jpeg', 'png', 'gif', 'webp'].includes(fileExt || '')) {
  244. c.header('Content-Type', `image/${fileExt}`)
  245. }
  246. const fileInfo = await Deno.stat(path)
  247. c.header('Last-Modified', fileInfo.mtime?.toUTCString() ?? new Date().toUTCString())
  248. },
  249. })
  250. // 静态资源路由
  251. honoApp.get('/deno.json', staticRoutes)
  252. honoApp.get('/client/*', staticRoutes)
  253. honoApp.get('/amap/*', staticRoutes)
  254. honoApp.get('/tailwindcss@3.4.16/*', staticRoutes)
  255. honoApp.get('/client_dist/*', staticRoutes)
  256. startMonitoring(apiClient)
  257. return honoApp
  258. }