2026-05-19 16:05:09 +08:00
|
|
|
import index from '@/ui/index.html'
|
|
|
|
|
|
2026-06-01 23:16:22 +08:00
|
|
|
import type { Prisma } from '@shumai/db'
|
2026-05-19 16:05:09 +08:00
|
|
|
import { Hono } from 'hono'
|
|
|
|
|
import agentRoute from '@/api/agent'
|
|
|
|
|
import attachmentRoute from '@/api/attachment'
|
|
|
|
|
import authnRoute from '@/api/authn'
|
2026-05-25 17:29:45 +08:00
|
|
|
import collectionRoute from '@/api/collection'
|
2026-05-19 16:05:09 +08:00
|
|
|
import fileRoute from '@/api/file'
|
|
|
|
|
import folderRoute from '@/api/folder'
|
|
|
|
|
import inviteRoute from '@/api/invite'
|
|
|
|
|
import metadataRoute from '@/api/metadata'
|
|
|
|
|
import { authMiddleware } from '@/api/middleware/auth'
|
|
|
|
|
import notificationRoute from '@/api/notification'
|
|
|
|
|
import projectRoute from '@/api/project'
|
|
|
|
|
import providerRoute from '@/api/provider'
|
|
|
|
|
import publicInviteRoute from '@/api/public-invite'
|
|
|
|
|
import publicShareRoute from '@/api/public-share'
|
|
|
|
|
import s3Route from '@/api/s3'
|
|
|
|
|
import shareRoute from '@/api/share'
|
|
|
|
|
import skillRoute from '@/api/skill'
|
|
|
|
|
import teamRoute from '@/api/team'
|
|
|
|
|
import uploadRoute from '@/api/upload'
|
|
|
|
|
import versionStackRoute from '@/api/versionStack'
|
2026-06-01 23:16:22 +08:00
|
|
|
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
|
|
|
|
import { assetService } from '@shumai/core/src/asset/asset'
|
2026-05-19 16:05:09 +08:00
|
|
|
import { workflowService } from '@/workflow/workflow'
|
|
|
|
|
|
|
|
|
|
type User = Prisma.UserGetPayload<Record<string, never>>
|
|
|
|
|
|
|
|
|
|
const app = new Hono()
|
|
|
|
|
|
|
|
|
|
const apiRoute = new Hono<{ Variables: { user: User } }>()
|
|
|
|
|
// Public Routes
|
|
|
|
|
.route('/', authnRoute)
|
|
|
|
|
.route('/', publicInviteRoute)
|
|
|
|
|
.route('/', publicShareRoute)
|
|
|
|
|
|
|
|
|
|
// Protected Routes
|
|
|
|
|
.use('*', authMiddleware)
|
|
|
|
|
.route('/', inviteRoute)
|
|
|
|
|
.route('/', providerRoute)
|
|
|
|
|
.route('/', notificationRoute)
|
|
|
|
|
.route('/', metadataRoute)
|
|
|
|
|
.route('/', projectRoute)
|
2026-05-25 17:29:45 +08:00
|
|
|
.route('/', collectionRoute)
|
2026-05-19 16:05:09 +08:00
|
|
|
.route('/', teamRoute)
|
|
|
|
|
.route('/', versionStackRoute)
|
|
|
|
|
.route('/', fileRoute)
|
|
|
|
|
.route('/', folderRoute)
|
|
|
|
|
.route('/', agentRoute)
|
|
|
|
|
.route('/', attachmentRoute)
|
|
|
|
|
.route('/', uploadRoute)
|
|
|
|
|
.route('/', shareRoute)
|
|
|
|
|
.route('/', skillRoute)
|
|
|
|
|
|
|
|
|
|
app.route('/files', s3Route)
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
|
const apiAppRoute = app.route('/api', apiRoute)
|
|
|
|
|
|
|
|
|
|
export type AppType = typeof apiAppRoute
|
|
|
|
|
|
|
|
|
|
export async function run() {
|
|
|
|
|
// Start services
|
|
|
|
|
await metadataService.syncSystemFields().catch(console.error)
|
2026-05-23 18:09:30 +08:00
|
|
|
assetService.startCleanupJob()
|
2026-05-19 16:05:09 +08:00
|
|
|
workflowService.start()
|
|
|
|
|
if (process.env.WORKFLOW_EXECUTOR === 'temporal') {
|
|
|
|
|
const args = process.argv.slice(2)
|
|
|
|
|
let workersOption = ''
|
|
|
|
|
for (let i = 0; i < args.length; i++) {
|
|
|
|
|
if (args[i] === '--workers') {
|
|
|
|
|
workersOption = args[i + 1] || ''
|
|
|
|
|
} else if (args[i].startsWith('--workers=')) {
|
|
|
|
|
workersOption = args[i].split('=')[1]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const queuesToStart = ['db_queue']
|
|
|
|
|
if (workersOption === 'agent' || workersOption === 'ai') {
|
|
|
|
|
queuesToStart.push('agent_queue')
|
|
|
|
|
} else if (workersOption === 'transcode') {
|
|
|
|
|
queuesToStart.push('transcode_queue')
|
|
|
|
|
} else if (workersOption === 'all') {
|
|
|
|
|
queuesToStart.push('agent_queue', 'transcode_queue')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Promise.all(queuesToStart.map((q) => workflowService.startWorkers(q))).catch(console.error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const server = Bun.serve({
|
|
|
|
|
port: 3000,
|
|
|
|
|
fetch: app.fetch,
|
|
|
|
|
routes: {
|
|
|
|
|
// Serve index.html for root
|
|
|
|
|
'/': index,
|
|
|
|
|
|
|
|
|
|
// Proxy API requests to Hono
|
|
|
|
|
'/api/*': app.fetch,
|
|
|
|
|
'/files/*': app.fetch,
|
|
|
|
|
|
|
|
|
|
// Catch-all for SPA routing (fallback to index.html)
|
|
|
|
|
'/*': index,
|
|
|
|
|
},
|
|
|
|
|
development: process.env.NODE_ENV !== 'production',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
console.log(`🚀 Server running at ${server.url}`)
|
2026-05-24 00:03:21 +08:00
|
|
|
|
|
|
|
|
const shutdown = () => {
|
|
|
|
|
console.log('\nShutting down gracefully...')
|
|
|
|
|
assetService.stopCleanupJob()
|
|
|
|
|
server.stop(true)
|
|
|
|
|
process.exit(0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
process.on('SIGINT', shutdown)
|
|
|
|
|
process.on('SIGTERM', shutdown)
|
2026-05-19 16:05:09 +08:00
|
|
|
}
|