@@ -90,7 +90,32 @@ app.get("/health", (_req, res) => {
9090 } ) ;
9191} ) ;
9292
93- // Auth endpoint — creates a JWT token for the requesting user
93+ // Webhook endpoint — pluv.io sends server events here
94+ app . post ( "/api/pluv/webhook" , async ( req , res ) => {
95+ try {
96+ // Convert express req/res to a standard Request for ioServer.fetch
97+ const url = `${ req . protocol } ://${ req . get ( "host" ) } ${ req . originalUrl } ` ;
98+ const headers = new Headers ( ) ;
99+ for ( const [ key , value ] of Object . entries ( req . headers ) ) {
100+ if ( typeof value === "string" ) headers . set ( key , value ) ;
101+ }
102+
103+ const fetchReq = new Request ( url , {
104+ method : req . method ,
105+ headers,
106+ body : req . method !== "GET" ? JSON . stringify ( req . body ) : undefined ,
107+ } ) ;
108+
109+ const fetchRes = await ioServer . fetch ( fetchReq ) ;
110+ const body = await fetchRes . text ( ) ;
111+ res . status ( fetchRes . status ) . send ( body ) ;
112+ } catch ( err ) {
113+ console . error ( "[pluv] Webhook error:" , err ) ;
114+ res . status ( 500 ) . json ( { error : "Webhook handling failed" } ) ;
115+ }
116+ } ) ;
117+
118+ // Auth endpoint — creates a JWT token for the requesting user (must be after webhook route)
94119app . get ( "/api/auth/pluv" , async ( req , res ) => {
95120 try {
96121 const room = req . query . room ;
@@ -117,36 +142,11 @@ app.get("/api/auth/pluv", async (req, res) => {
117142 }
118143} ) ;
119144
120- // Webhook endpoint — pluv.io sends server events here
121- app . all ( "/api/pluv" , async ( req , res ) => {
122- try {
123- // Convert express req/res to a standard Request for ioServer.fetch
124- const url = `${ req . protocol } ://${ req . get ( "host" ) } ${ req . originalUrl } ` ;
125- const headers = new Headers ( ) ;
126- for ( const [ key , value ] of Object . entries ( req . headers ) ) {
127- if ( typeof value === "string" ) headers . set ( key , value ) ;
128- }
129-
130- const fetchReq = new Request ( url , {
131- method : req . method ,
132- headers,
133- body : req . method !== "GET" ? JSON . stringify ( req . body ) : undefined ,
134- } ) ;
135-
136- const fetchRes = await ioServer . fetch ( fetchReq ) ;
137- const body = await fetchRes . text ( ) ;
138- res . status ( fetchRes . status ) . send ( body ) ;
139- } catch ( err ) {
140- console . error ( "[pluv] Webhook error:" , err ) ;
141- res . status ( 500 ) . json ( { error : "Webhook handling failed" } ) ;
142- }
143- } ) ;
144-
145145// ── Start server ──────────────────────────────────────────────────────────
146146
147147app . listen ( PORT , HOST , ( ) => {
148148 console . log ( `\n Pluv Chat Server running on http://${ HOST } :${ PORT } ` ) ;
149149 console . log ( ` Auth endpoint: GET /api/auth/pluv?room=...&userId=...` ) ;
150- console . log ( ` Webhook endpoint: POST /api/pluv` ) ;
150+ console . log ( ` Webhook endpoint: POST /api/pluv/webhook ` ) ;
151151 console . log ( ` Health check: GET /health\n` ) ;
152152} ) ;
0 commit comments