Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/bundlejs-api.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107// mod.ts/** * Bundle Service: Main Entry Point * * Creates Hono app with: * - Global middleware (security, cors, logging, timing) * - Rate limiting per-tier * - All endpoints registered from definitions * - Legacy path compatibility */
import type { EndpointHandlerModule } from '@utils/endpoint/types'import { createServer } from '@utils/server'import { showRoutes } from 'hono/dev'
// Import endpoint handlersimport * as BundleRunHandler from './endpoints/bundle/run/handler.ts'import * as BundleFileHandler from './endpoints/bundle/file/handler.ts'import * as BundleMetafileHandler from './endpoints/bundle/metafile/handler.ts'import * as BundleAnalysisHandler from './endpoints/bundle/analysis/handler.ts'import * as BundleWarningsHandler from './endpoints/bundle/warnings/handler.ts'import * as BundleRawHandler from './endpoints/bundle/raw/handler.ts'
import * as BadgeSvgHandler from './endpoints/badge/svg/handler.ts'import * as BadgeRasterHandler from './endpoints/badge/raster/handler.ts'
import * as CachePurgeHandler from './endpoints/cache/purge/handler.ts'import * as CacheClearHandler from './endpoints/cache/clear/handler.ts'
import * as StaticHandler from './endpoints/static/handler.ts'
import { EndpointDefinitions } from './index.ts'import { registerLegacyRoutes } from './legacy.ts'
// ============================================================================// Handler Registry// ============================================================================
export const EndpointHandlers = { // Bundle endpoints [EndpointDefinitions.BundleRun.Name]: BundleRunHandler, [EndpointDefinitions.BundleFile.Name]: BundleFileHandler, [EndpointDefinitions.BundleMetafile.Name]: BundleMetafileHandler, [EndpointDefinitions.BundleAnalysis.Name]: BundleAnalysisHandler, [EndpointDefinitions.BundleWarnings.Name]: BundleWarningsHandler, [EndpointDefinitions.BundleRaw.Name]: BundleRawHandler, // Badge endpoints [EndpointDefinitions.BadgeSvg.Name]: BadgeSvgHandler, [EndpointDefinitions.BadgeRaster.Name]: BadgeRasterHandler, // Cache admin endpoints [EndpointDefinitions.CachePurge.Name]: CachePurgeHandler, [EndpointDefinitions.CacheClear.Name]: CacheClearHandler, // Static endpoints [EndpointDefinitions.Static.Name]: StaticHandler,} as const satisfies Record<string, EndpointHandlerModule>
// ============================================================================// Create App// ============================================================================
const app = createServer('', { serviceName: 'bundle-service', cors: { origin: '*', allowMethods: ['GET', 'POST', 'DELETE', 'OPTIONS'], allowHeaders: ['Content-Type', 'Authorization'], exposeHeaders: ['X-Request-ID', 'X-Cache', 'X-Bundle-Duration-ms'], },})
// ============================================================================// Register Endpoints// ============================================================================
Object.values(EndpointDefinitions).forEach((endpoint) => { const route = endpoint.Route const methods = Array.from(new Set(endpoint.Methods)) as Array< 'GET' | 'POST' | 'DELETE' | 'PUT' | 'PATCH' >
const handlerModule = EndpointHandlers[endpoint.Name] as EndpointHandlerModule const middleware = handlerModule.Middleware ?? [] const handler = handlerModule.default
if (!handler) { console.warn(`No handler found for endpoint: ${endpoint.Name}`) return }
app.on(methods, [route], ...middleware, handler)})
// Register legacy routes for backward compatibilityregisterLegacyRoutes(app, EndpointHandlers)
// Show registered routesshowRoutes(app, { verbose: true })
// ============================================================================// Export// ============================================================================
export { app }export default app