mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 14:04:50 +00:00
refactor(web): dynamically register production HTML bundle routes
This commit is contained in:
+58
-59
@@ -1,15 +1,15 @@
|
|||||||
import { join } from 'node:path'
|
|
||||||
import { loadEnvConfig } from '@shumai/core/src/env-loader'
|
import { loadEnvConfig } from '@shumai/core/src/env-loader'
|
||||||
|
import { join } from 'node:path'
|
||||||
loadEnvConfig(process.cwd(), process.env.NODE_ENV !== 'production')
|
loadEnvConfig(process.cwd(), process.env.NODE_ENV !== 'production')
|
||||||
|
|
||||||
import index from '@shumai/webui/index.html'
|
import index from '@shumai/webui/index.html'
|
||||||
|
|
||||||
import { initAgentWorkflows } from '@shumai/agent'
|
import { initAgentWorkflows } from '@shumai/agent'
|
||||||
|
import { app } from '@shumai/api'
|
||||||
import { assetService } from '@shumai/core/src/asset/asset'
|
import { assetService } from '@shumai/core/src/asset/asset'
|
||||||
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
||||||
import { initTranscodeWorkflows } from '@shumai/transcode'
|
import { initTranscodeWorkflows } from '@shumai/transcode'
|
||||||
import { workflowService } from '@shumai/workflow-core'
|
import { workflowService } from '@shumai/workflow-core'
|
||||||
import { app } from '@shumai/api'
|
|
||||||
|
|
||||||
import { handleDaemonCommands } from '@shumai/core/src/utils/daemon'
|
import { handleDaemonCommands } from '@shumai/core/src/utils/daemon'
|
||||||
|
|
||||||
@@ -54,69 +54,68 @@ async function run() {
|
|||||||
|
|
||||||
const port = process.env.SHUMAI_SERVER_PORT ? parseInt(process.env.SHUMAI_SERVER_PORT) : 3000
|
const port = process.env.SHUMAI_SERVER_PORT ? parseInt(process.env.SHUMAI_SERVER_PORT) : 3000
|
||||||
|
|
||||||
const server = Bun.serve(
|
interface HtmlBundleFile {
|
||||||
isProd
|
input: string
|
||||||
? {
|
path: string
|
||||||
port,
|
loader: string
|
||||||
maxRequestBodySize: process.env.MAX_REQUEST_BODY_SIZE
|
isEntry: boolean
|
||||||
? parseInt(process.env.MAX_REQUEST_BODY_SIZE)
|
headers: Record<string, string>
|
||||||
: 1024 * 1024 * 1024 * 10, // Default 10GB
|
}
|
||||||
development: false,
|
|
||||||
async fetch(req) {
|
|
||||||
const url = new URL(req.url)
|
|
||||||
|
|
||||||
if (!url.pathname.startsWith('/api/') && !url.pathname.startsWith('/files/')) {
|
interface HtmlBundle {
|
||||||
const filepath = join(import.meta.dir, url.pathname)
|
index: string
|
||||||
const file = Bun.file(filepath)
|
files: HtmlBundleFile[]
|
||||||
if (await file.exists()) {
|
}
|
||||||
try {
|
|
||||||
const stat = await file.stat()
|
|
||||||
if (stat.isFile()) {
|
|
||||||
return new Response(file)
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// ignore stat errors
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const htmlFile = Bun.file(join(import.meta.dir, 'index.html'))
|
// Bun's native Serve config routes require specific handler types that are hard
|
||||||
if (await htmlFile.exists()) {
|
// to dynamically construct without using 'any'.
|
||||||
return new Response(htmlFile, {
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
headers: { 'Content-Type': 'text/html' },
|
const routes: Record<string, any> = {
|
||||||
})
|
'/api/*': app.fetch,
|
||||||
}
|
'/files/*': app.fetch,
|
||||||
|
}
|
||||||
|
|
||||||
const htmlFileNpm = Bun.file(join(import.meta.dir, 'shumai-app.html'))
|
if (isProd) {
|
||||||
if (await htmlFileNpm.exists()) {
|
const bundle = index as unknown as HtmlBundle
|
||||||
return new Response(htmlFileNpm, {
|
if (bundle && typeof bundle === 'object' && 'files' in bundle && Array.isArray(bundle.files)) {
|
||||||
headers: { 'Content-Type': 'text/html' },
|
for (const file of bundle.files) {
|
||||||
})
|
const routePath = file.path.startsWith('.') ? file.path.slice(1) : file.path
|
||||||
}
|
const filePath = join(import.meta.dir, file.path)
|
||||||
}
|
routes[routePath] = () => new Response(Bun.file(filePath), { headers: file.headers })
|
||||||
|
}
|
||||||
|
|
||||||
return app.fetch(req)
|
const mainHtmlPath = join(import.meta.dir, bundle.index)
|
||||||
},
|
const mainHtmlFile = Bun.file(mainHtmlPath)
|
||||||
}
|
const mainHtmlHeaders = bundle.files.find((f) => f.path === bundle.index)?.headers || {
|
||||||
: {
|
'content-type': 'text/html;charset=utf-8',
|
||||||
port,
|
}
|
||||||
maxRequestBodySize: process.env.MAX_REQUEST_BODY_SIZE
|
const serveMainHtml = () => new Response(mainHtmlFile, { headers: mainHtmlHeaders })
|
||||||
? parseInt(process.env.MAX_REQUEST_BODY_SIZE)
|
|
||||||
: 1024 * 1024 * 1024 * 10, // Default 10GB
|
|
||||||
development: true,
|
|
||||||
fetch: app.fetch,
|
|
||||||
routes: {
|
|
||||||
// Serve index.html for root
|
|
||||||
'/': index,
|
|
||||||
|
|
||||||
// Proxy API requests to Hono
|
routes['/'] = serveMainHtml
|
||||||
'/api/*': app.fetch,
|
routes['/*'] = serveMainHtml
|
||||||
'/files/*': app.fetch,
|
} else {
|
||||||
|
const fallbackHtmlPath = join(import.meta.dir, 'shumai-app.html')
|
||||||
|
const serveFallbackHtml = () =>
|
||||||
|
new Response(Bun.file(fallbackHtmlPath), {
|
||||||
|
headers: { 'content-type': 'text/html;charset=utf-8' },
|
||||||
|
})
|
||||||
|
routes['/'] = serveFallbackHtml
|
||||||
|
routes['/*'] = serveFallbackHtml
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
routes['/'] = index
|
||||||
|
routes['/*'] = index
|
||||||
|
}
|
||||||
|
|
||||||
// Catch-all for SPA routing (fallback to index.html)
|
const server = Bun.serve({
|
||||||
'/*': index,
|
port,
|
||||||
},
|
maxRequestBodySize: process.env.MAX_REQUEST_BODY_SIZE
|
||||||
},
|
? parseInt(process.env.MAX_REQUEST_BODY_SIZE)
|
||||||
)
|
: 1024 * 1024 * 1024 * 10, // Default 10GB
|
||||||
|
development: !isProd,
|
||||||
|
fetch: app.fetch,
|
||||||
|
routes,
|
||||||
|
})
|
||||||
|
|
||||||
console.log(`🚀 Server running at ${server.url}`)
|
console.log(`🚀 Server running at ${server.url}`)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user