11const http = require ( 'http' ) ;
2+ const httpProxy = require ( 'http-proxy' ) ;
23const fs = require ( 'fs' ) ;
34const path = require ( 'path' ) ;
45
56const PORT = 8080 ;
67
8+ // Create proxy instances
9+ const chatProxy = httpProxy . createProxyServer ( { target : 'http://localhost:3000' } ) ;
10+ const terminalProxy = httpProxy . createProxyServer ( { target : 'http://localhost:7681' , ws : true } ) ;
11+ const gatewayProxy = httpProxy . createProxyServer ( { target : 'http://localhost:8001' } ) ;
12+
713const server = http . createServer ( ( req , res ) => {
14+ // Route based on path
815 if ( req . url === '/' || req . url === '/index.html' ) {
16+ // Serve the main UI
917 fs . readFile ( path . join ( __dirname , 'index.html' ) , ( err , data ) => {
1018 if ( err ) {
1119 res . writeHead ( 500 ) ;
@@ -15,12 +23,52 @@ const server = http.createServer((req, res) => {
1523 res . writeHead ( 200 , { 'Content-Type' : 'text/html' } ) ;
1624 res . end ( data ) ;
1725 } ) ;
26+ } else if ( req . url . startsWith ( '/chat' ) ) {
27+ // Proxy to Anse chat UI
28+ chatProxy . web ( req , res , { } , ( err ) => {
29+ console . error ( 'Chat proxy error:' , err ) ;
30+ res . writeHead ( 502 ) ;
31+ res . end ( 'Chat service unavailable' ) ;
32+ } ) ;
33+ } else if ( req . url . startsWith ( '/terminal' ) ) {
34+ // Proxy to ttyd terminal
35+ terminalProxy . web ( req , res , { } , ( err ) => {
36+ console . error ( 'Terminal proxy error:' , err ) ;
37+ res . writeHead ( 502 ) ;
38+ res . end ( 'Terminal service unavailable' ) ;
39+ } ) ;
40+ } else if ( req . url . startsWith ( '/v1/' ) ) {
41+ // Proxy to gateway API
42+ gatewayProxy . web ( req , res , { } , ( err ) => {
43+ console . error ( 'Gateway proxy error:' , err ) ;
44+ res . writeHead ( 502 ) ;
45+ res . end ( 'Gateway service unavailable' ) ;
46+ } ) ;
1847 } else {
1948 res . writeHead ( 404 ) ;
2049 res . end ( 'Not found' ) ;
2150 }
2251} ) ;
2352
53+ // Handle WebSocket upgrade for terminal
54+ server . on ( 'upgrade' , ( req , socket , head ) => {
55+ if ( req . url . startsWith ( '/terminal' ) ) {
56+ terminalProxy . ws ( req , socket , head , { } , ( err ) => {
57+ console . error ( 'Terminal WebSocket error:' , err ) ;
58+ socket . destroy ( ) ;
59+ } ) ;
60+ }
61+ } ) ;
62+
63+ // Error handling
64+ chatProxy . on ( 'error' , ( err ) => console . error ( 'Chat proxy error:' , err ) ) ;
65+ terminalProxy . on ( 'error' , ( err ) => console . error ( 'Terminal proxy error:' , err ) ) ;
66+ gatewayProxy . on ( 'error' , ( err ) => console . error ( 'Gateway proxy error:' , err ) ) ;
67+
2468server . listen ( PORT , '0.0.0.0' , ( ) => {
2569 console . log ( `Responsive UI server running on port ${ PORT } ` ) ;
70+ console . log ( 'Proxying:' ) ;
71+ console . log ( ' /chat -> localhost:3000 (Anse)' ) ;
72+ console . log ( ' /terminal -> localhost:7681 (ttyd)' ) ;
73+ console . log ( ' /v1/* -> localhost:8001 (Gateway)' ) ;
2674} ) ;
0 commit comments