@@ -16,6 +16,7 @@ import (
1616 "github.com/golang-jwt/jwt/v4"
1717 "github.com/gorilla/mux"
1818 "github.com/itchyny/gojq"
19+ "github.com/rs/cors"
1920
2021 "github.com/delta10/filter-proxy/internal/config"
2122 "github.com/delta10/filter-proxy/internal/route"
@@ -78,10 +79,10 @@ func main() {
7879
7980 resp , err := client .Do (r )
8081 if err != nil {
81- log .Printf ("%+v" , r .URL )
82- http .Error (w , "Server Error" , http .StatusInternalServerError )
83- log .Fatal ("ServeHTTP:" , err )
82+ writeError (w , http .StatusBadGateway , fmt .Sprintf ("could not fetch backend response: %s" , err ))
83+ return
8484 }
85+
8586 defer resp .Body .Close ()
8687
8788 utils .DelHopHeaders (resp .Header )
@@ -239,7 +240,7 @@ func main() {
239240
240241 proxyResp , err := client .Do (backendRequest )
241242 if err != nil {
242- writeError (w , http .StatusInternalServerError , fmt .Sprintf ("could not fetch backend response: %s" , err ))
243+ writeError (w , http .StatusBadGateway , fmt .Sprintf ("could not fetch backend response: %s" , err ))
243244 return
244245 }
245246
@@ -295,9 +296,23 @@ func main() {
295296 }
296297 }
297298
299+ var httpHandler http.Handler
300+ if len (config .Cors .AllowedOrigins ) > 0 {
301+ c := cors .New (cors.Options {
302+ AllowedOrigins : config .Cors .AllowedOrigins ,
303+ AllowedMethods : config .Cors .AllowedMethods ,
304+ AllowedHeaders : config .Cors .AllowedHeaders ,
305+ AllowCredentials : config .Cors .AllowCredentials ,
306+ })
307+
308+ httpHandler = c .Handler (router )
309+ } else {
310+ httpHandler = router
311+ }
312+
298313 s := & http.Server {
299314 Addr : config .ListenAddress ,
300- Handler : router ,
315+ Handler : httpHandler ,
301316 ReadTimeout : 10 * time .Second ,
302317 WriteTimeout : 10 * time .Second ,
303318 MaxHeaderBytes : 1 << 20 ,
@@ -403,7 +418,7 @@ func authorizeRequestWithService(config *config.Config, backend config.Backend,
403418 request .Header .Set ("X-Forwarded-For" , utils .ReadUserIP (r ))
404419
405420 client := & http.Client {
406- Timeout : 10 * time .Second ,
421+ Timeout : 25 * time .Second ,
407422 }
408423
409424 resp , err := client .Do (request )
0 commit comments