11import { Request , Response } from 'express' ;
22import { BackgroundTaskQueue , Region , Variant } from '@tf2qs/core' ;
33import { CreateServerForClientTaskData } from '@tf2qs/providers' ;
4+ import { logger } from '@tf2qs/telemetry' ;
45
56export function createCreateServerHandler ( backgroundTaskQueue : BackgroundTaskQueue ) {
67 /**
@@ -37,6 +38,11 @@ export function createCreateServerHandler(backgroundTaskQueue: BackgroundTaskQue
3738 return async ( req : Request , res : Response ) : Promise < void > => {
3839 const clientId = req . auth ?. payload . azp || req . auth ?. payload . sub ;
3940 if ( ! clientId ) {
41+ logger . emit ( {
42+ severityText : 'WARN' ,
43+ body : 'Create server request with no client ID in token' ,
44+ attributes : { path : req . path , method : req . method } ,
45+ } ) ;
4046 res . status ( 401 ) . json ( { error : 'Unauthorized' , message : 'No client ID found in token' } ) ;
4147 return ;
4248 }
@@ -48,32 +54,74 @@ export function createCreateServerHandler(backgroundTaskQueue: BackgroundTaskQue
4854 firstMap ?: unknown ;
4955 } ;
5056
57+ logger . emit ( {
58+ severityText : 'INFO' ,
59+ body : 'Create server request received' ,
60+ attributes : {
61+ clientId : clientId as string ,
62+ region : region as string ,
63+ variantName : variantName as string ,
64+ firstMap : firstMap as string | undefined ,
65+ hasExtraEnvs : String ( extraEnvs !== undefined ) ,
66+ } ,
67+ } ) ;
68+
5169 if ( ! region || typeof region !== 'string' ) {
70+ logger . emit ( {
71+ severityText : 'WARN' ,
72+ body : 'Create server validation failed: invalid region' ,
73+ attributes : { clientId : String ( clientId ) , region : String ( region ) , reason : 'not a string or missing' } ,
74+ } ) ;
5275 res . status ( 400 ) . json ( { error : 'Bad Request' , message : 'region is required and must be a string' } ) ;
5376 return ;
5477 }
5578 if ( ! Object . values ( Region ) . includes ( region as Region ) ) {
79+ logger . emit ( {
80+ severityText : 'WARN' ,
81+ body : 'Create server validation failed: unknown region' ,
82+ attributes : { clientId : String ( clientId ) , region : String ( region ) , reason : 'not a valid Region enum value' } ,
83+ } ) ;
5684 res . status ( 400 ) . json ( { error : 'Bad Request' , message : 'region is not a valid region' } ) ;
5785 return ;
5886 }
5987 if ( ! variantName || typeof variantName !== 'string' ) {
88+ logger . emit ( {
89+ severityText : 'WARN' ,
90+ body : 'Create server validation failed: invalid variantName' ,
91+ attributes : { clientId : String ( clientId ) , region : String ( region ) , variantName : String ( variantName ) , reason : 'not a string or missing' } ,
92+ } ) ;
6093 res . status ( 400 ) . json ( { error : 'Bad Request' , message : 'variantName is required and must be a string' } ) ;
6194 return ;
6295 }
6396 if ( firstMap !== undefined && ( typeof firstMap !== 'string' || ! / ^ \w + $ / . test ( firstMap ) ) ) {
97+ logger . emit ( {
98+ severityText : 'WARN' ,
99+ body : 'Create server validation failed: invalid map' ,
100+ attributes : { clientId : String ( clientId ) , region : String ( region ) , variantName : String ( variantName ) , firstMap : String ( firstMap ) , reason : 'map name contains invalid characters' } ,
101+ } ) ;
64102 res . status ( 400 ) . json ( { error : 'Bad Request' , message : 'Invalid map' } ) ;
65103 return ;
66104 }
67105
68106 let sanitizedExtraEnvs : Record < string , string > | undefined ;
69107 if ( extraEnvs !== undefined ) {
70108 if ( typeof extraEnvs !== 'object' || Array . isArray ( extraEnvs ) || extraEnvs === null ) {
109+ logger . emit ( {
110+ severityText : 'WARN' ,
111+ body : 'Create server validation failed: invalid extraEnvs type' ,
112+ attributes : { clientId : String ( clientId ) , region : String ( region ) , variantName : String ( variantName ) , reason : 'not a key-value object' } ,
113+ } ) ;
71114 res . status ( 400 ) . json ( { error : 'Bad Request' , message : 'extraEnvs must be a key-value object if provided' } ) ;
72115 return ;
73116 }
74117 const result : Record < string , string > = { } ;
75118 for ( const [ key , value ] of Object . entries ( extraEnvs as Record < string , unknown > ) ) {
76119 if ( typeof value !== 'string' ) {
120+ logger . emit ( {
121+ severityText : 'WARN' ,
122+ body : 'Create server validation failed: extraEnvs non-string value' ,
123+ attributes : { clientId : String ( clientId ) , region : String ( region ) , variantName : String ( variantName ) , extraEnvKey : key } ,
124+ } ) ;
77125 res . status ( 400 ) . json ( { error : 'Bad Request' , message : 'extraEnvs values must be strings' } ) ;
78126 return ;
79127 }
@@ -87,11 +135,23 @@ export function createCreateServerHandler(backgroundTaskQueue: BackgroundTaskQue
87135 variantName : variantName as Variant ,
88136 clientId : clientId as string ,
89137 extraEnvs : sanitizedExtraEnvs ,
90- firstMap :firstMap as string | undefined ,
138+ firstMap : firstMap as string | undefined ,
91139 } ;
92140
93141 const taskId = await backgroundTaskQueue . enqueue ( 'create-server-for-client' , taskData , undefined , undefined , { ownerId : clientId as string } ) ;
94142
143+ logger . emit ( {
144+ severityText : 'INFO' ,
145+ body : 'Create server task enqueued' ,
146+ attributes : {
147+ clientId : clientId as string ,
148+ region : region as string ,
149+ variantName : variantName as string ,
150+ taskId,
151+ firstMap : firstMap as string | undefined ,
152+ } ,
153+ } ) ;
154+
95155 res . status ( 202 ) . json ( { taskId } ) ;
96156 } ;
97157}
0 commit comments