2
0

app.tsx 9.4 KB

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