|
| 1 | +import { ObjectKernel, Plugin, IHttpServer } from '@objectstack/core'; |
| 2 | +import { HttpServer } from './http-server.js'; |
| 3 | +import { createApiRegistryPlugin, ApiRegistryConfig } from './api-registry-plugin.js'; |
| 4 | + |
| 5 | +export interface RuntimeConfig { |
| 6 | + /** |
| 7 | + * Optional existing server instance (e.g. Hono, Express app) |
| 8 | + * If provided, Runtime will use it as the 'http.server' service. |
| 9 | + * If not provided, Runtime expects a server plugin (like HonoServerPlugin) to be registered manually. |
| 10 | + */ |
| 11 | + server?: IHttpServer; |
| 12 | + |
| 13 | + /** |
| 14 | + * API Registry Configuration |
| 15 | + */ |
| 16 | + api?: ApiRegistryConfig; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * ObjectStack Runtime |
| 21 | + * |
| 22 | + * High-level entry point for bootstrapping an ObjectStack application. |
| 23 | + * Wraps ObjectKernel and provides standard orchestration for: |
| 24 | + * - HTTP Server binding |
| 25 | + * - API Registry (REST Routes) |
| 26 | + * - Plugin Management |
| 27 | + */ |
| 28 | +export class Runtime { |
| 29 | + readonly kernel: ObjectKernel; |
| 30 | + |
| 31 | + constructor(config: RuntimeConfig = {}) { |
| 32 | + this.kernel = new ObjectKernel(); |
| 33 | + |
| 34 | + // If external server provided, register it immediately |
| 35 | + if (config.server) { |
| 36 | + // If the provided server is not already an HttpServer wrapper, wrap it? |
| 37 | + // Since IHttpServer is the interface, we assume it complies. |
| 38 | + // But HttpServer class in runtime is an adapter. |
| 39 | + // If user passes raw Hono, it won't work unless they wrapped it. |
| 40 | + // We'll assume they pass a compliant IHttpServer. |
| 41 | + this.kernel.registerService('http.server', config.server); |
| 42 | + } |
| 43 | + |
| 44 | + // Register API Registry by default |
| 45 | + // This plugin is passive (wait for services) so it's safe to add early. |
| 46 | + this.kernel.use(createApiRegistryPlugin(config.api)); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Register a plugin |
| 51 | + */ |
| 52 | + use(plugin: Plugin) { |
| 53 | + this.kernel.use(plugin); |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Start the runtime |
| 59 | + * 1. Initializes all plugins (init phase) |
| 60 | + * 2. Starts all plugins (start phase) |
| 61 | + */ |
| 62 | + async start() { |
| 63 | + await this.kernel.bootstrap(); |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Get the kernel instance |
| 69 | + */ |
| 70 | + getKernel() { |
| 71 | + return this.kernel; |
| 72 | + } |
| 73 | +} |
0 commit comments