|
| 1 | +import path from 'path'; |
| 2 | + |
| 3 | +import { connectDatabase } from '@admin/db/connect'; |
| 4 | +import { createDatabaseModels } from '@admin/db/models'; |
| 5 | +import { parseEnvironment } from '@admin/env'; |
| 6 | +import { routeApiRequest } from '@admin/http/router'; |
| 7 | +import { type ServiceContext } from '@admin/services/context'; |
| 8 | +import { createS3StorageClient } from '@admin/storage/s3-client'; |
| 9 | + |
| 10 | +const env = parseEnvironment(process.env); |
| 11 | + |
| 12 | +await connectDatabase(env.MONGO_URL); |
| 13 | + |
| 14 | +const models = createDatabaseModels(); |
| 15 | +const storage = createS3StorageClient(env); |
| 16 | +await storage.verifyBuckets(); |
| 17 | + |
| 18 | +const context: ServiceContext = { |
| 19 | + env, |
| 20 | + models, |
| 21 | + storage, |
| 22 | + startedAt: Date.now(), |
| 23 | +}; |
| 24 | + |
| 25 | +async function resolvePublicRoot() { |
| 26 | + const candidates = [ |
| 27 | + path.resolve(import.meta.dir, '../public'), |
| 28 | + path.resolve(import.meta.dir, '../dist/public'), |
| 29 | + ]; |
| 30 | + |
| 31 | + for (const candidate of candidates) { |
| 32 | + if (await Bun.file(path.join(candidate, 'index.html')).exists()) { |
| 33 | + return candidate; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return candidates[0]; |
| 38 | +} |
| 39 | + |
| 40 | +const publicRoot = await resolvePublicRoot(); |
| 41 | + |
| 42 | +function safeResolvePublicPath(urlPath: string) { |
| 43 | + const normalized = path.normalize(urlPath).replace(/^(\.\.(\/|\\|$))+/, ''); |
| 44 | + const fullPath = path.resolve(publicRoot, `.${normalized}`); |
| 45 | + |
| 46 | + if (!fullPath.startsWith(publicRoot)) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + return fullPath; |
| 51 | +} |
| 52 | + |
| 53 | +async function serveStatic(request: Request) { |
| 54 | + const url = new URL(request.url); |
| 55 | + const pathname = url.pathname === '/' ? '/index.html' : url.pathname; |
| 56 | + const filePath = safeResolvePublicPath(pathname); |
| 57 | + |
| 58 | + if (filePath) { |
| 59 | + const file = Bun.file(filePath); |
| 60 | + if (await file.exists()) { |
| 61 | + return new Response(file); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + const spaFallback = Bun.file(path.join(publicRoot, 'index.html')); |
| 66 | + if (await spaFallback.exists()) { |
| 67 | + return new Response(spaFallback); |
| 68 | + } |
| 69 | + |
| 70 | + return new Response('index.html was not found. Run `bun run build` first.', { |
| 71 | + status: 500, |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +Bun.serve({ |
| 76 | + hostname: env.ADMIN_APP_HOST, |
| 77 | + port: env.ADMIN_APP_PORT, |
| 78 | + async fetch(request) { |
| 79 | + const url = new URL(request.url); |
| 80 | + |
| 81 | + if (url.pathname.startsWith('/api/')) { |
| 82 | + return routeApiRequest(request, context); |
| 83 | + } |
| 84 | + |
| 85 | + return serveStatic(request); |
| 86 | + }, |
| 87 | +}); |
| 88 | + |
| 89 | +process.stdout.write( |
| 90 | + `@nbw/admin listening on http://${env.ADMIN_APP_HOST}:${env.ADMIN_APP_PORT}\n`, |
| 91 | +); |
0 commit comments