@@ -5,58 +5,57 @@ import { cors } from 'hono/cors';
55import { logger } from 'hono/logger' ;
66import { DataEngine , ObjectStackRuntimeProtocol , RuntimePlugin } from '@objectstack/runtime' ;
77
8- export interface DevServerOptions {
8+ export interface HonoServerOptions {
99 port ?: number ;
1010 staticRoot ?: string ;
1111 cors ?: boolean ;
12+ logger ?: boolean ;
1213}
1314
1415/**
15- * ObjectStack Development Server Plugin
16+ * Hono Server Runtime Plugin
1617 *
17- * Drop-in plugin to expose Kernel via HTTP for UI development .
18- * NOT for production use .
18+ * Exposes the ObjectStack Kernel via standard HTTP Protocol using Hono .
19+ * Can be used for Production (Standalone) or Development .
1920 */
20- export class DevServerPlugin implements RuntimePlugin {
21- name = 'dev-server' ;
22- private port : number ;
23- private staticRoot : string ;
24- private enableCors : boolean ;
21+ export class HonoServerPlugin implements RuntimePlugin {
22+ name = 'com.objectstack.server.hono' ;
23+
24+ private options : HonoServerOptions ;
2525
26- constructor ( options : DevServerOptions = { } ) {
27- this . port = options . port || 3004 ;
28- this . staticRoot = options . staticRoot || './public' ;
29- this . enableCors = options . cors !== false ;
26+ constructor ( options : HonoServerOptions = { } ) {
27+ this . options = {
28+ port : 3000 ,
29+ cors : true ,
30+ logger : true ,
31+ ...options
32+ } ;
3033 }
3134
3235 async onStart ( ctx : { engine : DataEngine } ) {
3336 const app = new Hono ( ) ;
3437 const protocol = new ObjectStackRuntimeProtocol ( ctx . engine ) ;
3538
36- // 1. Dev Middlewares
37- app . use ( '*' , logger ( ) ) ;
38- if ( this . enableCors ) {
39- app . use ( '*' , cors ( ) ) ;
40- }
39+ // 1. Middlewares
40+ if ( this . options . logger ) app . use ( '*' , logger ( ) ) ;
41+ if ( this . options . cors ) app . use ( '*' , cors ( ) ) ;
4142
4243 // 2. Wiring Protocol (Automatic)
4344 // Discovery
4445 app . get ( '/api/v1' , ( c ) => c . json ( protocol . getDiscovery ( ) ) ) ;
4546
46- // Meta (Types)
47+ // Meta Protocol
4748 app . get ( '/api/v1/meta' , ( c ) => c . json ( protocol . getMetaTypes ( ) ) ) ;
48- // Meta (List)
4949 app . get ( '/api/v1/meta/:type' , ( c ) => c . json ( protocol . getMetaItems ( c . req . param ( 'type' ) ) ) ) ;
50- // Meta (Item)
51- app . get ( '/api/v1/meta/:type/:name' , ( c ) => {
50+ app . get ( '/api/v1/meta/:type/:name' , ( c ) => {
5251 try {
53- return c . json ( protocol . getMetaItem ( c . req . param ( 'type' ) , c . req . param ( 'name' ) ) )
52+ return c . json ( protocol . getMetaItem ( c . req . param ( 'type' ) , c . req . param ( 'name' ) ) ) ;
5453 } catch ( e :any ) {
5554 return c . json ( { error : e . message } , 404 ) ;
5655 }
5756 } ) ;
58-
59- // Data (Read)
57+
58+ // Data Protocol
6059 app . get ( '/api/v1/data/:object' , async ( c ) => {
6160 try { return c . json ( await protocol . findData ( c . req . param ( 'object' ) , c . req . query ( ) ) ) ; }
6261 catch ( e :any ) { return c . json ( { error :e . message } , 400 ) ; }
@@ -65,8 +64,6 @@ export class DevServerPlugin implements RuntimePlugin {
6564 try { return c . json ( await protocol . getData ( c . req . param ( 'object' ) , c . req . param ( 'id' ) ) ) ; }
6665 catch ( e :any ) { return c . json ( { error :e . message } , 404 ) ; }
6766 } ) ;
68-
69- // Data (Write)
7067 app . post ( '/api/v1/data/:object' , async ( c ) => {
7168 try { return c . json ( await protocol . createData ( c . req . param ( 'object' ) , await c . req . json ( ) ) , 201 ) ; }
7269 catch ( e :any ) { return c . json ( { error :e . message } , 400 ) ; }
@@ -80,7 +77,7 @@ export class DevServerPlugin implements RuntimePlugin {
8077 catch ( e :any ) { return c . json ( { error :e . message } , 400 ) ; }
8178 } ) ;
8279
83- // UI View
80+ // UI Protocol
8481 // @ts -ignore
8582 app . get ( '/api/v1/ui/view/:object' , ( c ) => {
8683 try {
@@ -90,18 +87,16 @@ export class DevServerPlugin implements RuntimePlugin {
9087 catch ( e :any ) { return c . json ( { error :e . message } , 404 ) ; }
9188 } ) ;
9289
93- // 3. Static Files (UI Hosting )
94- if ( this . staticRoot ) {
95- app . get ( '/' , serveStatic ( { root : this . staticRoot , path : 'index.html' } ) ) ;
96- app . get ( '/*' , serveStatic ( { root : this . staticRoot } ) ) ;
90+ // 3. Static Files (Optional )
91+ if ( this . options . staticRoot ) {
92+ app . get ( '/' , serveStatic ( { root : this . options . staticRoot , path : 'index.html' } ) ) ;
93+ app . get ( '/*' , serveStatic ( { root : this . options . staticRoot } ) ) ;
9794 }
9895
9996 console . log ( '' ) ;
100- console . log ( `📦 ObjectStack Dev Server running at: http://localhost:${ this . port } ` ) ;
101- console . log ( `➜ API Discovery: http://localhost:${ this . port } /api/v1` ) ;
102- console . log ( `➜ UI Playground: http://localhost:${ this . port } /index.html` ) ;
97+ console . log ( `🌍 ObjectStack Server (Hono) running at: http://localhost:${ this . options . port } ` ) ;
10398 console . log ( '' ) ;
104-
105- serve ( { fetch : app . fetch , port : this . port } ) ;
99+
100+ serve ( { fetch : app . fetch , port : this . options . port } ) ;
106101 }
107102}
0 commit comments