1+ // Fixed server.js with better error handling and logging
2+ const http = require ( 'http' ) ;
3+ const fs = require ( 'fs' ) ;
4+ const path = require ( 'path' ) ;
5+
6+ const PORT = 8080 ;
7+
8+ // Simple proxy function with better error handling
9+ function proxy ( req , res , targetHost , targetPort , targetPath ) {
10+ console . log ( `Proxying ${ req . url } to ${ targetHost } :${ targetPort } ${ targetPath } ` ) ;
11+
12+ const options = {
13+ hostname : targetHost ,
14+ port : targetPort ,
15+ path : targetPath || req . url ,
16+ method : req . method ,
17+ headers : { ...req . headers , host : `${ targetHost } :${ targetPort } ` }
18+ } ;
19+
20+ const proxyReq = http . request ( options , ( proxyRes ) => {
21+ console . log ( `Proxy response: ${ proxyRes . statusCode } ` ) ;
22+ res . writeHead ( proxyRes . statusCode , proxyRes . headers ) ;
23+ proxyRes . pipe ( res ) ;
24+ } ) ;
25+
26+ proxyReq . on ( 'error' , ( err ) => {
27+ console . error ( `Proxy error to ${ targetHost } :${ targetPort } :` , err . message ) ;
28+ res . writeHead ( 502 , { 'Content-Type' : 'application/json' } ) ;
29+ res . end ( JSON . stringify ( { error : 'Gateway error' , details : err . message } ) ) ;
30+ } ) ;
31+
32+ req . pipe ( proxyReq ) ;
33+ }
34+
35+ const server = http . createServer ( ( req , res ) => {
36+ console . log ( `Request: ${ req . method } ${ req . url } ` ) ;
37+
38+ // Add CORS headers for all responses
39+ res . setHeader ( 'Access-Control-Allow-Origin' , '*' ) ;
40+ res . setHeader ( 'Access-Control-Allow-Methods' , 'GET, POST, OPTIONS' ) ;
41+ res . setHeader ( 'Access-Control-Allow-Headers' , 'Content-Type' ) ;
42+
43+ // Handle OPTIONS for CORS
44+ if ( req . method === 'OPTIONS' ) {
45+ res . writeHead ( 200 ) ;
46+ res . end ( ) ;
47+ return ;
48+ }
49+
50+ // Route based on path
51+ if ( req . url === '/' || req . url === '/index.html' ) {
52+ // Serve the main UI
53+ fs . readFile ( path . join ( __dirname , 'index.html' ) , ( err , data ) => {
54+ if ( err ) {
55+ console . error ( 'Error reading index.html:' , err ) ;
56+ res . writeHead ( 500 ) ;
57+ res . end ( 'Error loading page' ) ;
58+ return ;
59+ }
60+ res . writeHead ( 200 , { 'Content-Type' : 'text/html' } ) ;
61+ res . end ( data ) ;
62+ } ) ;
63+ } else if ( req . url . startsWith ( '/chat' ) ) {
64+ // Proxy to chat UI
65+ const targetPath = req . url . substring ( 5 ) || '/' ;
66+ proxy ( req , res , 'localhost' , 3000 , targetPath ) ;
67+ } else if ( req . url . startsWith ( '/terminal' ) ) {
68+ // Proxy to ttyd
69+ const targetPath = req . url . substring ( 9 ) || '/' ;
70+ proxy ( req , res , 'localhost' , 7681 , targetPath ) ;
71+ } else if ( req . url . startsWith ( '/v1/' ) ) {
72+ // Proxy to gateway API - THIS IS THE IMPORTANT ONE
73+ proxy ( req , res , 'localhost' , 8001 , req . url ) ;
74+ } else {
75+ res . writeHead ( 404 ) ;
76+ res . end ( `Not found: ${ req . url } ` ) ;
77+ }
78+ } ) ;
79+
80+ // Handle WebSocket upgrades
81+ server . on ( 'upgrade' , ( req , socket , head ) => {
82+ console . log ( `WebSocket upgrade request: ${ req . url } ` ) ;
83+
84+ if ( req . url . startsWith ( '/terminal' ) ) {
85+ // Proxy WebSocket to ttyd
86+ const net = require ( 'net' ) ;
87+ const upstream = net . connect ( 7681 , 'localhost' ) ;
88+
89+ upstream . on ( 'connect' , ( ) => {
90+ console . log ( 'Connected to ttyd WebSocket' ) ;
91+ const targetPath = req . url . substring ( 9 ) || '/' ;
92+ upstream . write ( `GET ${ targetPath } HTTP/1.1\r\n` ) ;
93+ for ( const [ key , value ] of Object . entries ( req . headers ) ) {
94+ if ( key . toLowerCase ( ) !== 'host' ) {
95+ upstream . write ( `${ key } : ${ value } \r\n` ) ;
96+ }
97+ }
98+ upstream . write ( 'Host: localhost:7681\r\n' ) ;
99+ upstream . write ( '\r\n' ) ;
100+ upstream . write ( head ) ;
101+
102+ socket . pipe ( upstream ) ;
103+ upstream . pipe ( socket ) ;
104+ } ) ;
105+
106+ upstream . on ( 'error' , ( err ) => {
107+ console . error ( 'WebSocket upstream error:' , err ) ;
108+ socket . destroy ( ) ;
109+ } ) ;
110+
111+ socket . on ( 'error' , ( err ) => {
112+ console . error ( 'WebSocket socket error:' , err ) ;
113+ upstream . destroy ( ) ;
114+ } ) ;
115+ } else {
116+ socket . destroy ( ) ;
117+ }
118+ } ) ;
119+
120+ server . listen ( PORT , '0.0.0.0' , ( ) => {
121+ console . log ( `UI server running on port ${ PORT } ` ) ;
122+ console . log ( 'Routes:' ) ;
123+ console . log ( ' / -> UI (this server)' ) ;
124+ console . log ( ' /chat/* -> localhost:3000' ) ;
125+ console . log ( ' /terminal/* -> localhost:7681' ) ;
126+ console . log ( ' /v1/* -> localhost:8001 (Gateway API)' ) ;
127+
128+ // Test gateway connectivity on startup
129+ setTimeout ( ( ) => {
130+ const testReq = http . get ( 'http://localhost:8001/health' , ( res ) => {
131+ console . log ( `Gateway health check: ${ res . statusCode } ` ) ;
132+ } ) ;
133+ testReq . on ( 'error' , ( err ) => {
134+ console . error ( 'Gateway not responding:' , err . message ) ;
135+ } ) ;
136+ } , 1000 ) ;
137+ } ) ;
0 commit comments