|
| 1 | +import { serve } from '@hono/node-server'; |
| 2 | +import { serveStatic } from '@hono/node-server/serve-static'; |
| 3 | +import { Hono } from 'hono'; |
| 4 | +import { cors } from 'hono/cors'; |
| 5 | +import { logger } from 'hono/logger'; |
| 6 | +import { RuntimePlugin, RuntimeContext, ObjectStackRuntimeProtocol } from '@objectstack/runtime'; |
| 7 | + |
| 8 | +export interface HonoPluginOptions { |
| 9 | + port?: number; |
| 10 | + staticRoot?: string; |
| 11 | +} |
| 12 | + |
| 13 | +export class HonoServerPlugin implements RuntimePlugin { |
| 14 | + name = 'hono-server'; |
| 15 | + private options: HonoPluginOptions; |
| 16 | + private app: Hono; |
| 17 | + |
| 18 | + constructor(options: HonoPluginOptions = {}) { |
| 19 | + this.options = { |
| 20 | + port: 3000, |
| 21 | + ...options |
| 22 | + }; |
| 23 | + this.app = new Hono(); |
| 24 | + } |
| 25 | + |
| 26 | + install(ctx: RuntimeContext) { |
| 27 | + const { engine } = ctx; |
| 28 | + const protocol = new ObjectStackRuntimeProtocol(engine); |
| 29 | + |
| 30 | + // Middleware |
| 31 | + this.app.use('*', logger()); |
| 32 | + this.app.use('*', cors()); |
| 33 | + |
| 34 | + // --- Bind Protocol to Hono --- |
| 35 | + |
| 36 | + // 1. Discovery |
| 37 | + this.app.get('/api/v1', (c) => c.json(protocol.getDiscovery())); |
| 38 | + |
| 39 | + // 2. Meta |
| 40 | + this.app.get('/api/v1/meta', (c) => c.json(protocol.getMetaTypes())); |
| 41 | + this.app.get('/api/v1/meta/:type', (c) => c.json(protocol.getMetaItems(c.req.param('type')))); |
| 42 | + this.app.get('/api/v1/meta/:type/:name', (c) => { |
| 43 | + try { |
| 44 | + return c.json(protocol.getMetaItem(c.req.param('type'), c.req.param('name'))); |
| 45 | + } catch (e: any) { |
| 46 | + return c.json({ error: e.message }, 404); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + // 3. Data |
| 51 | + this.app.get('/api/v1/data/:object', async (c) => { |
| 52 | + try { |
| 53 | + const result = await protocol.findData(c.req.param('object'), c.req.query()); |
| 54 | + return c.json(result); |
| 55 | + } catch (e: any) { |
| 56 | + return c.json({ error: e.message }, 404); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + this.app.get('/api/v1/data/:object/:id', async (c) => { |
| 61 | + try { |
| 62 | + const result = await protocol.getData(c.req.param('object'), c.req.param('id')); |
| 63 | + return c.json(result); |
| 64 | + } catch (e: any) { |
| 65 | + return c.json({ error: e.message }, 404); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + this.app.post('/api/v1/data/:object', async (c) => { |
| 70 | + try { |
| 71 | + const body = await c.req.json(); |
| 72 | + const result = await protocol.createData(c.req.param('object'), body); |
| 73 | + return c.json(result, 201); |
| 74 | + } catch (e: any) { |
| 75 | + return c.json({ error: e.message }, 400); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + this.app.patch('/api/v1/data/:object/:id', async (c) => { |
| 80 | + try { |
| 81 | + const body = await c.req.json(); |
| 82 | + const result = await protocol.updateData(c.req.param('object'), c.req.param('id'), body); |
| 83 | + return c.json(result); |
| 84 | + } catch (e: any) { |
| 85 | + return c.json({ error: e.message }, 400); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + this.app.delete('/api/v1/data/:object/:id', async (c) => { |
| 90 | + try { |
| 91 | + const result = await protocol.deleteData(c.req.param('object'), c.req.param('id')); |
| 92 | + return c.json(result); |
| 93 | + } catch (e: any) { |
| 94 | + return c.json({ error: e.message }, 400); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + // 4. UI Protocol |
| 99 | + this.app.get('/api/v1/ui/view/:object', (c) => { |
| 100 | + try { |
| 101 | + // @ts-ignore |
| 102 | + const view = protocol.getUiView(c.req.param('object'), c.req.query('type') || 'list'); |
| 103 | + return c.json(view); |
| 104 | + } catch (e: any) { |
| 105 | + return c.json({ error: e.message }, 404); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + // Static Files |
| 110 | + if (this.options.staticRoot) { |
| 111 | + this.app.get('/', serveStatic({ root: this.options.staticRoot, path: 'index.html' })); |
| 112 | + this.app.get('/*', serveStatic({ root: this.options.staticRoot })); |
| 113 | + } |
| 114 | + |
| 115 | + console.log(`[HonoPlugin] Installed routes and middleware.`); |
| 116 | + } |
| 117 | + |
| 118 | + async onStart(ctx: RuntimeContext) { |
| 119 | + const port = this.options.port; |
| 120 | + console.log(`[HonoPlugin] Starting server...`); |
| 121 | + console.log(`✅ Server is running on http://localhost:${port}`); |
| 122 | + |
| 123 | + serve({ |
| 124 | + fetch: this.app.fetch, |
| 125 | + port |
| 126 | + }); |
| 127 | + } |
| 128 | +} |
0 commit comments