@@ -7,15 +7,14 @@ import { HonoYjsMemAdapter } from '@kerebron/extension-server-hono/HonoYjsMemAda
77
88const __dirname = import . meta. dirname ;
99
10- const app = new Hono ( ) ;
11-
1210const yjsAdapter = new HonoYjsMemAdapter ( ) ;
1311
1412export class Server {
1513 public app ;
1614 public fetch ;
1715
18- constructor ( private opts : { devProxyUrl ?: string } = { } ) {
16+ constructor ( private opts : { devProxyUrls : Record < string , string > } = { devProxyUrls : { } } ) {
17+ const app = new Hono ( ) ;
1918 this . app = app ;
2019 this . fetch = app . fetch ;
2120
@@ -31,29 +30,41 @@ export class Server {
3130 } ) ,
3231 ) ;
3332
34- if ( opts . devProxyUrl ) {
35- this . app . get ( '*' , ( c ) => {
33+ for ( const path in this . opts . devProxyUrls ) {
34+ const devProxyUrl = this . opts . devProxyUrls [ path ] ;
35+ console . log ( `Proxy: ${ path } => ${ devProxyUrl } ` ) ;
36+ this . app . all ( path + '/*' , ( c ) => {
3637 const queryString = c . req . url
3738 . split ( '?' )
3839 . map ( ( e : string , idx : number ) => {
3940 return idx > 0 ? e : '' ;
4041 } )
4142 . join ( '?' ) ;
42- return proxy ( `${ opts . devProxyUrl } ${ c . req . path } ${ queryString } ` ) ;
43- } ) ;
44- } else {
45- this . app . notFound ( ( c ) => {
46- const file = Deno . readFileSync (
47- __dirname + '/../../example-vue/dist/index.html' ,
48- ) ;
49- return c . html ( new TextDecoder ( ) . decode ( file ) ) ;
43+
44+ const subPath = c . req . path ;
45+ return proxy ( `${ devProxyUrl } ${ subPath } ${ queryString } ` , {
46+ ...c . req , // optional, specify only when forwarding all the request data (including credentials) is necessary.
47+ headers : {
48+ ...c . req . header ( ) ,
49+ 'X-Forwarded-For' : '127.0.0.1' ,
50+ 'X-Forwarded-Host' : c . req . header ( 'host' ) ,
51+ Authorization : undefined , // do not propagate request headers contained in c.req.header('Authorization')
52+ } ,
53+ } ) ;
5054 } ) ;
51- this . app . use (
52- '*' ,
53- serveStatic ( { root : __dirname + '/../../example-vue/dist' } ) ,
54- ) ;
5555 }
5656
57+ this . app . notFound ( ( c ) => {
58+ const file = Deno . readFileSync (
59+ __dirname + '/../public/index.html' ,
60+ ) ;
61+ return c . html ( new TextDecoder ( ) . decode ( file ) ) ;
62+ } ) ;
63+ this . app . use (
64+ '*' ,
65+ serveStatic ( { root : __dirname + '/../public' } ) ,
66+ ) ;
67+
5768 this . app . use ( '/*' , cors ( ) ) ;
5869 }
5970}
0 commit comments