@@ -394,6 +394,17 @@ function buildForwardHeaders(req, user, extraHeaders = {}) {
394394 return headers ;
395395}
396396
397+ function buildInternalForwardHeaders ( req , user , extraHeaders = { } ) {
398+ const headers = buildForwardHeaders ( req , user , extraHeaders ) ;
399+ const forwardedUserId = normalizeOptionalString ( req . get ( 'x-user-id' ) ) ;
400+
401+ if ( ! headers [ 'x-user-id' ] && forwardedUserId ) {
402+ headers [ 'x-user-id' ] = forwardedUserId ;
403+ }
404+
405+ return headers ;
406+ }
407+
397408function pickPlayBotId ( body ) {
398409 const explicitBotId = normalizeOptionalString ( body ?. bot_id ) ;
399410 if ( explicitBotId ) {
@@ -871,6 +882,67 @@ function createExternalApiRouter({ env = process.env, fetchImpl = globalThis.fet
871882 return router ;
872883}
873884
885+ function createInternalApiRouter ( { serviceUrls, fetchImpl } ) {
886+ const router = express . Router ( ) ;
887+
888+ router . post ( '/v1/matchmaking/enqueue' , express . json ( ) , asyncRoute ( async ( req , res ) => {
889+ const user = await getOptionalUser ( req , serviceUrls , fetchImpl ) ;
890+ const result = await fetchJson (
891+ fetchImpl ,
892+ 'gamey service' ,
893+ `${ serviceUrls . gamey } /v1/matchmaking/enqueue` ,
894+ buildJsonInit ( 'POST' , req . body , buildInternalForwardHeaders ( req , user , {
895+ 'Content-Type' : 'application/json' ,
896+ } ) ) ,
897+ ) ;
898+
899+ sendPayload ( res , result . status , result . payload ) ;
900+ } ) ) ;
901+
902+ router . get ( '/v1/matchmaking/tickets/:ticketId' , asyncRoute ( async ( req , res ) => {
903+ const result = await fetchJson (
904+ fetchImpl ,
905+ 'gamey service' ,
906+ `${ serviceUrls . gamey } /v1/matchmaking/tickets/${ encodeURIComponent ( req . params . ticketId ) } ` ,
907+ { method : 'GET' } ,
908+ ) ;
909+
910+ sendPayload ( res , result . status , result . payload ) ;
911+ } ) ) ;
912+
913+ router . post ( '/v1/matchmaking/tickets/:ticketId/cancel' , asyncRoute ( async ( req , res ) => {
914+ const result = await fetchJson (
915+ fetchImpl ,
916+ 'gamey service' ,
917+ `${ serviceUrls . gamey } /v1/matchmaking/tickets/${ encodeURIComponent ( req . params . ticketId ) } /cancel` ,
918+ { method : 'POST' } ,
919+ ) ;
920+
921+ sendPayload ( res , result . status , result . payload ) ;
922+ } ) ) ;
923+
924+ router . use ( ( error , _req , res , _next ) => {
925+ if ( res . headersSent ) {
926+ return ;
927+ }
928+
929+ if ( error instanceof HttpResponseError ) {
930+ sendPayload ( res , error . status , error . payload ?? { message : error . message } ) ;
931+ return ;
932+ }
933+
934+ if ( error instanceof UpstreamConnectionError ) {
935+ res . status ( 502 ) . json ( { message : `Bad Gateway: ${ error . serviceName } unavailable` } ) ;
936+ return ;
937+ }
938+
939+ console . error ( error ) ;
940+ res . status ( 500 ) . json ( { message : error . message || 'Internal server error' } ) ;
941+ } ) ;
942+
943+ return router ;
944+ }
945+
874946function createApp ( {
875947 env = process . env ,
876948 proxyFactory = createProxyMiddleware ,
@@ -904,6 +976,7 @@ function createApp({
904976 }
905977 } ) ) ;
906978
979+ app . use ( '/api' , createInternalApiRouter ( { serviceUrls, fetchImpl } ) ) ;
907980
908981 for ( const route of proxyRoutes ) {
909982 if ( route . mountPath === '/api' ) {
0 commit comments