@@ -8,83 +8,90 @@ function initAdjacentServersProxy(app, isDocker, ensureAdmin) {
88 // Proxies
99 //// STAC
1010 if ( process . env . WITH_STAC === "true" ) {
11+ const stacTarget = `http://${ isDocker ? "stac-fastapi" : "localhost" } :${
12+ process . env . STAC_PORT || 8881
13+ } `;
1114 app . use (
1215 `${ process . env . ROOT_PATH || "" } /stac` ,
1316 ensureAdmin ( false , false , true ) , // true to allow all GETs - others require admin auth
1417 createProxyMiddleware ( {
15- target : `http://${ isDocker ? "stac-fastapi" : "localhost" } :${
16- process . env . STAC_PORT || 8881
17- } `,
18+ target : stacTarget ,
1819 changeOrigin : true ,
1920 pathRewrite : {
2021 [ `^${ process . env . ROOT_PATH || "" } /stac` ] : "" ,
2122 } ,
2223 selfHandleResponse : true ,
2324 on : {
24- proxyRes : createSwaggerInterceptor ( "stac" ) ,
25+ proxyRes : createSwaggerInterceptor ( "stac" , stacTarget ) ,
2526 } ,
2627 } )
2728 ) ;
2829 }
2930
3031 //// Tipg
3132 if ( process . env . WITH_TIPG === "true" ) {
33+ const tipgTarget = `http://${ isDocker ? "tipg" : "localhost" } :${
34+ process . env . TIPG_PORT || 8882
35+ } `;
3236 app . use (
3337 `${ process . env . ROOT_PATH || "" } /tipg` ,
3438 ensureAdmin ( false , false , true ) , // true to allow all GETs - others require admin auth
3539 createProxyMiddleware ( {
36- target : `http://${ isDocker ? "tipg" : "localhost" } :${
37- process . env . TIPG_PORT || 8882
38- } `,
40+ target : tipgTarget ,
3941 changeOrigin : true ,
4042 pathRewrite : {
4143 [ `^${ process . env . ROOT_PATH || "" } /tipg` ] : "" ,
4244 } ,
4345 selfHandleResponse : true ,
4446 on : {
45- proxyRes : createSwaggerInterceptor ( "tipg" ) ,
47+ proxyRes : createSwaggerInterceptor ( "tipg" , tipgTarget ) ,
4648 } ,
4749 } )
4850 ) ;
4951 }
5052
5153 //// TiTiler
5254 if ( process . env . WITH_TITILER === "true" ) {
55+ const titilerTarget = `http://${ isDocker ? "titiler" : "localhost" } :${
56+ process . env . TITILER_PORT || 8883
57+ } `;
5358 app . use (
5459 `${ process . env . ROOT_PATH || "" } /titiler` ,
5560 ensureAdmin ( false , false , true , [ "/cog/stac" ] ) , // true to allow all GETs (except /cog/stac) - others require admin auth
5661 createProxyMiddleware ( {
57- target : `http://${ isDocker ? "titiler" : "localhost" } :${
58- process . env . TITILER_PORT || 8883
59- } `,
62+ target : titilerTarget ,
6063 changeOrigin : true ,
6164 pathRewrite : {
6265 [ `^${ process . env . ROOT_PATH || "" } /titiler` ] : "" ,
6366 } ,
6467 selfHandleResponse : true ,
6568 on : {
66- proxyRes : createSwaggerInterceptor ( "titiler" ) ,
69+ proxyRes : createSwaggerInterceptor ( "titiler" , titilerTarget ) ,
6770 } ,
6871 } )
6972 ) ;
7073 }
7174
7275 /// TiTiler-pgSTAC
7376 if ( process . env . WITH_TITILER_PGSTAC === "true" ) {
77+ const titilerpgstacTarget = `http://${
78+ isDocker ? "titiler-pgstac" : "localhost"
79+ } :${ process . env . TITILER_PGSTAC_PORT || 8884 } `;
7480 app . use (
7581 `${ process . env . ROOT_PATH || "" } /titilerpgstac` ,
7682 ensureAdmin ( false , false , true ) , // true to allow all GETs - others require admin auth
7783 createProxyMiddleware ( {
78- target : `http://${ isDocker ? "titiler-pgstac" : "localhost" } :${
79- process . env . TITILER_PGSTAC_PORT || 8884
80- } `,
84+ target : titilerpgstacTarget ,
8185 changeOrigin : true ,
8286 pathRewrite : {
8387 [ `^${ process . env . ROOT_PATH || "" } /titilerpgstac` ] : "" ,
8488 } ,
8589 selfHandleResponse : true ,
8690 on : {
87- proxyRes : createSwaggerInterceptor ( "titilerpgstac" ) ,
91+ proxyRes : createSwaggerInterceptor (
92+ "titilerpgstac" ,
93+ titilerpgstacTarget
94+ ) ,
8895 } ,
8996 } )
9097 ) ;
@@ -108,22 +115,26 @@ function initAdjacentServersProxy(app, isDocker, ensureAdmin) {
108115 }
109116}
110117
111- const createSwaggerInterceptor = ( path ) => {
118+ const createSwaggerInterceptor = ( path , target ) => {
112119 return responseInterceptor ( async ( responseBuffer , proxyRes , req , res ) => {
120+ let finalReturn = responseBuffer ;
121+ let newResponse ;
122+
113123 if ( req . originalUrl . endsWith ( `/${ path } /api` ) ) {
114- const response = JSON . parse ( responseBuffer . toString ( "utf8" ) ) ; // convert buffer to string
115- response . servers = [
124+ newResponse = newResponse || responseBuffer . toString ( "utf8" ) ;
125+ const responseJSON = JSON . parse ( newResponse ) ; // convert buffer to string
126+ responseJSON . servers = [
116127 {
117128 url : `${
118129 ( process . env . EXTERNAL_ROOT_PATH || "" ) +
119130 ( process . env . ROOT_PATH || "" )
120131 } /${ path } `,
121132 } ,
122133 ] ;
123- return JSON . stringify ( response ) ; // manipulate response and return the result
134+ newResponse = JSON . stringify ( responseJSON ) ; // manipulate response
124135 } else if ( req . originalUrl . endsWith ( `/${ path } /api.html` ) ) {
125- const response = responseBuffer . toString ( "utf8" ) ; // convert buffer to string
126- return response
136+ newResponse = newResponse || responseBuffer . toString ( "utf8" ) ;
137+ newResponse = newResponse
127138 . replace (
128139 "'/api'" ,
129140 `'${
@@ -137,9 +148,22 @@ const createSwaggerInterceptor = (path) => {
137148 ( process . env . EXTERNAL_ROOT_PATH || "" ) +
138149 ( process . env . ROOT_PATH || "" )
139150 } /${ path } /docs/oauth2-redirect'`
140- ) ; // manipulate response and return the result
151+ ) ; // manipulate response
152+ }
153+
154+ if (
155+ res . get ( "Content-Type" ) &&
156+ ( res . get ( "Content-Type" ) . includes ( "json" ) ||
157+ res . get ( "Content-Type" ) . includes ( "html" ) )
158+ ) {
159+ newResponse = newResponse || responseBuffer . toString ( "utf8" ) ;
160+ newResponse = newResponse . replaceAll (
161+ target ,
162+ `${ req . protocol } ://${ req . get ( "host" ) } /${ path } `
163+ ) ;
141164 }
142- return responseBuffer ;
165+
166+ return newResponse || finalReturn ;
143167 } ) ;
144168} ;
145169
0 commit comments