app.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 { Auth } from '@d8d-appcontainer/auth';
  8. import debug from "debug"
  9. import dayjs from 'dayjs';
  10. import utc from 'dayjs/plugin/utc';
  11. import type { GlobalConfig } from '../client/share/types.ts';
  12. import { OssType, MapMode } from '../client/share/types.ts';
  13. import { createRouter } from './router.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. // 初始化学员有效期检查服务
  44. import { initUserExpiryCheck } from './services/userExpiryCheck.ts'
  45. initUserExpiryCheck()
  46. interface EsmScriptConfig {
  47. src: string
  48. href: string
  49. denoJson: string
  50. refresh: boolean
  51. prodPath?: string
  52. prodSrc?: string
  53. }
  54. // 定义模块参数接口
  55. interface ModuleParams {
  56. apiClient: APIClient,
  57. auth: Auth,
  58. app: Hono
  59. moduleDir: string
  60. }
  61. export default function({ apiClient, app, moduleDir , auth}: ModuleParams) {
  62. const honoApp = app
  63. // 创建路由
  64. const router = createRouter(apiClient, moduleDir, auth)
  65. honoApp.route('/', router)
  66. // 首页路由 - SSR
  67. honoApp.get('/', async (c: HonoContext) => {
  68. const systemName = GLOBAL_CONFIG.APP_NAME
  69. return c.html(
  70. <html>
  71. <head>
  72. <title>{systemName}</title>
  73. <meta charset="UTF-8" />
  74. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  75. <script src="https://cdn.tailwindcss.com"></script>
  76. </head>
  77. <body>
  78. <div className="min-h-screen bg-gray-50 flex items-center justify-center py-12 px-4 sm:px-6 lg:px-8">
  79. <div className="max-w-md w-full space-y-8">
  80. {/* 系统介绍区域 */}
  81. <div className="text-center">
  82. <h1 className="text-4xl font-bold text-gray-900 mb-4">
  83. {systemName}
  84. </h1>
  85. <p className="text-lg text-gray-600 mb-8">
  86. 全功能应用Starter
  87. </p>
  88. <p className="text-base text-gray-500 mb-8">
  89. 这是一个基于Hono和React的应用Starter,提供了用户认证、文件管理、图表分析、地图集成和主题切换等常用功能。
  90. </p>
  91. </div>
  92. {/* 管理入口按钮 */}
  93. <div className="space-y-4">
  94. <a
  95. href="/admin"
  96. 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"
  97. >
  98. 进入管理后台
  99. </a>
  100. {/* 移动端入口按钮 */}
  101. <a
  102. href="/mobile"
  103. 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"
  104. >
  105. 进入移动端
  106. </a>
  107. {/* 迁移管理入口按钮 */}
  108. <a
  109. href="/migrations"
  110. 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"
  111. >
  112. 数据库迁移
  113. </a>
  114. </div>
  115. </div>
  116. </div>
  117. </body>
  118. </html>
  119. )
  120. })
  121. // 创建一个函数,用于生成包含全局配置的HTML页面
  122. const createHtmlWithConfig = (scriptConfig: EsmScriptConfig, title = '应用Starter') => {
  123. return (c: HonoContext) => {
  124. const isProd = GLOBAL_CONFIG.ENV === 'production';
  125. const isLocalDeploy = Deno.env.get('IS_LOCAL_DEPLOY') === 'true';
  126. return c.html(
  127. <html lang="zh-CN">
  128. <head>
  129. <meta charset="UTF-8" />
  130. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  131. {isProd && <meta name="version" content={Deno.env.get('VERSION') || '0.1.0'} />}
  132. <title>{title}</title>
  133. {isLocalDeploy ? (
  134. <script type="module" src={scriptConfig.prodSrc || `/client_dist/${scriptConfig.prodPath}`}></script>
  135. ) : (
  136. <script src={scriptConfig.src} href={scriptConfig.href} deno-json={scriptConfig.denoJson}
  137. {...isProd ? {}:{ refresh: true }}
  138. ></script>
  139. )}
  140. {isLocalDeploy ? (<script src="/tailwindcss@3.4.16/index.js"></script>) : (<script src="https://cdn.tailwindcss.com"></script>)}
  141. <script dangerouslySetInnerHTML={{ __html: `window.CONFIG = ${JSON.stringify(GLOBAL_CONFIG)};` }} />
  142. {isProd && (
  143. <>
  144. <script src="https://ai-oss.d8d.fun/umd/vconsole.3.15.1.min.js"></script>
  145. <script dangerouslySetInnerHTML={{ __html: `
  146. const urlParams = new URLSearchParams(window.location.search);
  147. if (urlParams.has('vconsole')) {
  148. var vConsole = new VConsole({
  149. theme: urlParams.get('vconsole_theme') || 'light',
  150. onReady: function() {
  151. console.log('vConsole is ready');
  152. }
  153. });
  154. }
  155. `}} />
  156. </>
  157. )}
  158. {!isProd && (
  159. <>
  160. <script src="https://ai-oss.d8d.fun/umd/vconsole.3.15.1.min.js"></script>
  161. <script dangerouslySetInnerHTML={{ __html: `
  162. const urlParams = new URLSearchParams(window.location.search);
  163. var vConsole = new VConsole({
  164. theme: urlParams.get('vconsole_theme') || 'light',
  165. onReady: function() {
  166. console.log('vConsole is ready');
  167. }
  168. });
  169. `}} />
  170. </>
  171. )}
  172. <script src="https://g.alicdn.com/apsara-media-box/imp-interaction/1.6.1/alivc-im.iife.js"></script>
  173. </head>
  174. <body className="bg-gray-50">
  175. <div id="root"></div>
  176. </body>
  177. </html>
  178. )
  179. }
  180. }
  181. // 后台管理路由
  182. honoApp.get('/admin', createHtmlWithConfig({
  183. src: "https://esm.d8d.fun/xb",
  184. href: "/client/admin/web_app.tsx",
  185. denoJson: "/deno.json",
  186. refresh: true,
  187. prodPath: "admin/web_app.js"
  188. }, GLOBAL_CONFIG.APP_NAME))
  189. honoApp.get('/admin/*', createHtmlWithConfig({
  190. src: "https://esm.d8d.fun/xb",
  191. href: "/client/admin/web_app.tsx",
  192. denoJson: "/deno.json",
  193. refresh: true,
  194. prodPath: "admin/web_app.js"
  195. }, GLOBAL_CONFIG.APP_NAME))
  196. // 移动端路由
  197. honoApp.get('/mobile', createHtmlWithConfig({
  198. src: "https://esm.d8d.fun/xb",
  199. href: "/client/mobile/mobile_app.tsx",
  200. denoJson: "/deno.json",
  201. refresh: true,
  202. prodPath: "mobile/mobile_app.js"
  203. }, GLOBAL_CONFIG.APP_NAME))
  204. honoApp.get('/mobile/*', createHtmlWithConfig({
  205. src: "https://esm.d8d.fun/xb",
  206. href: "/client/mobile/mobile_app.tsx",
  207. denoJson: "/deno.json",
  208. refresh: true,
  209. prodPath: "mobile/mobile_app.js"
  210. }, GLOBAL_CONFIG.APP_NAME))
  211. // 迁移管理路由
  212. honoApp.get('/migrations', createHtmlWithConfig({
  213. src: "https://esm.d8d.fun/xb",
  214. href: "/client/migrations/migrations_app.tsx",
  215. denoJson: "/deno.json",
  216. refresh: true,
  217. prodPath: "migrations/migrations_app.js"
  218. }, GLOBAL_CONFIG.APP_NAME))
  219. honoApp.get('/migrations/*', createHtmlWithConfig({
  220. src: "https://esm.d8d.fun/xb",
  221. href: "/client/migrations/migrations_app.tsx",
  222. denoJson: "/deno.json",
  223. refresh: true,
  224. prodPath: "migrations/migrations_app.js"
  225. }, GLOBAL_CONFIG.APP_NAME))
  226. honoApp.get('/stock/*', createHtmlWithConfig({
  227. src: "https://esm.d8d.fun/xb",
  228. href: "/client/stock/stock_app.tsx",
  229. denoJson: "/deno.json",
  230. refresh: true,
  231. prodPath: "stock/stock_app.js"
  232. }, GLOBAL_CONFIG.APP_NAME))
  233. const staticRoutes = serveStatic({
  234. root: moduleDir,
  235. onFound: async (path: string, c: HonoContext) => {
  236. const fileExt = path.split('.').pop()?.toLowerCase()
  237. if (fileExt === 'tsx' || fileExt === 'ts') {
  238. c.header('Content-Type', 'text/typescript; charset=utf-8')
  239. } else if (fileExt === 'js' || fileExt === 'mjs') {
  240. c.header('Content-Type', 'application/javascript; charset=utf-8')
  241. } else if (fileExt === 'json') {
  242. c.header('Content-Type', 'application/json; charset=utf-8')
  243. } else if (fileExt === 'html') {
  244. c.header('Content-Type', 'text/html; charset=utf-8')
  245. } else if (fileExt === 'css') {
  246. c.header('Content-Type', 'text/css; charset=utf-8')
  247. } else if (['jpg', 'jpeg', 'png', 'gif', 'webp'].includes(fileExt || '')) {
  248. c.header('Content-Type', `image/${fileExt}`)
  249. }
  250. const fileInfo = await Deno.stat(path)
  251. c.header('Last-Modified', fileInfo.mtime?.toUTCString() ?? new Date().toUTCString())
  252. },
  253. })
  254. // 静态资源路由
  255. honoApp.get('/deno.json', staticRoutes)
  256. honoApp.get('/client/*', staticRoutes)
  257. honoApp.get('/amap/*', staticRoutes)
  258. honoApp.get('/tailwindcss@3.4.16/*', staticRoutes)
  259. honoApp.get('/client_dist/*', staticRoutes)
  260. return honoApp
  261. }