11import  *  as  express  from  'express' ; 
22import  *  as  dotenv  from  'dotenv' ; 
3- import  feedbackRoutes  from  './routes/feedback.routes' ; 
4- import  healthRoutes  from  './routes/health.routes' ; 
53import  *  as  swaggerUi  from  'swagger-ui-express' ; 
64import  *  as  YAML  from  'yamljs' ; 
75import  *  as  path  from  'path' ; 
6+ import  *  as  match  from  'path-to-regexp' ; 
7+ import  feedbackRoutes  from  './routes/feedback.routes' ; 
8+ import  healthRoutes  from  './routes/health.routes' ; 
89
910
1011dotenv . config ( ) ; 
1112const  swaggerDocument  =  YAML . load ( path . join ( __dirname ,  './docs/openapi.yaml' ) ) ; 
1213
14+ // Parse swaggerDocument to find paths with security defined 
15+ const  securedPaths : string [ ]  =  [ ] ; 
16+ if  ( swaggerDocument  &&  swaggerDocument . paths )  { 
17+     for  ( const  [ route ,  methods ]  of  Object . entries ( swaggerDocument . paths ) )  { 
18+         // @ts -ignore 
19+         for  ( const  [ method ,  details ]  of  Object . entries < any > ( methods ) )  { 
20+             if  ( details  &&  details . security  &&  details . security . length  >  0 )  { 
21+                 if  ( ! securedPaths . includes ( route ) )  { 
22+                     securedPaths . push ( route ) ; 
23+                 } 
24+             } 
25+         } 
26+     } 
27+ } 
28+ console . debug ( 'Secured paths:' ,  securedPaths ) ; 
29+ 
30+ 
1331const  app  =  express ( ) ; 
32+ 
1433// Middleware 
34+ // JSON body parser 
1535app . use ( express . json ( ) ) ; 
36+ // Authentication middleware 
37+ // @ts -ignore 
38+ app . use ( ( req ,  res ,  next )  =>  { 
39+     const  isSecured  =  securedPaths . some ( pathPattern  =>  { 
40+         // Convert OpenAPI path patterns (e.g., /feedbacks/{id}) to path-to-regexp style (e.g., /feedbacks/:id) 
41+         const  openApiPattern  =  pathPattern . replace ( / { ( [ ^ } ] + ) } / g,  ':$1' ) ; 
42+         const  matcher  =  match . match ( openApiPattern ,  {  decode : decodeURIComponent ,  end : true  } ) ; 
43+         return  matcher ( req . path )  !==  false ; 
44+     } ) ; 
45+     console . debug ( 'Request path:' ,  req . path ,  'isSecured:' ,  isSecured ) ; 
46+     if  ( isSecured )  { 
47+         const  token  =  req . headers [ 'x-api-key' ] ; 
48+         if  ( ! token  ||  token  !==  process . env . AUTH_TOKEN )  { 
49+             console . warn ( 'Unauthorized access attempt:' ,  req . path ) ; 
50+             return  res . status ( 401 ) . json ( {  message : 'Unauthorized'  } ) ; 
51+         } 
52+     }  
53+     next ( ) ; 
54+ } ) ; 
1655
1756// Routes 
1857// @ts -ignore 
1958app . get ( '/' ,  ( req ,  res )  =>  res . redirect ( '/api-docs' ) ) ; 
2059app . use ( '/api-docs' ,  swaggerUi . serve ,  swaggerUi . setup ( swaggerDocument ) ) ; 
2160app . use ( '/feedbacks' ,  feedbackRoutes ) ; 
22- app . use ( '/health' ,  healthRoutes ) 
61+ app . use ( '/health' ,  healthRoutes ) ; 
2362
2463export  default  app ; 
0 commit comments