@@ -8,25 +8,78 @@ import cats.effect.kernel.Resource
88import cats .effect .std .Random
99
1010import ProxyConfig .Equilibrium
11+ import org .http4s .server .Router
12+ import org .http4s .server .middleware .Logger
13+ import com .comcast .ip4s .Port
14+ import cats .data .NonEmptyList
15+ import ProxyConfig .Server
16+ import ProxyConfig .LocationMatcher
17+ import com .comcast .ip4s .Host
18+ import cats .syntax .all .*
1119
1220def makeProxyRoutes (
1321 client : Client [IO ],
14- pathPrefix : Option [String ],
15- proxyConfig : Resource [IO , Option [Equilibrium ]]
16- )(logger : Scribe [IO ]): Resource [IO , HttpRoutes [IO ]] =
17- proxyConfig.flatMap {
18- case Some (pc) =>
19- {
20- given R : Random [IO ] = Random .javaUtilConcurrentThreadLocalRandom[IO ]
21- logger.debug(" setup proxy server" ) >>
22- IO (HttpProxy .servers[IO ](pc, client, pathPrefix.getOrElse(??? )).head._2)
23- }.toResource
22+ proxyConfig : Option [(Equilibrium , String )]
23+ )(logger : Scribe [IO ]): HttpRoutes [IO ] =
24+ proxyConfig match
25+ case Some ((pc, pathPrefix)) =>
26+ given R : Random [IO ] = Random .javaUtilConcurrentThreadLocalRandom[IO ]
27+ Logger .httpRoutes[IO ](
28+ logHeaders = true ,
29+ logBody = true ,
30+ redactHeadersWhen = _ => false ,
31+ logAction = Some ((msg : String ) => logger.trace(msg))
32+ )(
33+ Router (
34+ pathPrefix -> HttpProxy .servers[IO ](pc, client, pathPrefix).head._2
35+ )
36+ )
2437
2538 case None =>
26- (
27- logger.debug(" no proxy set" ) >>
28- IO (HttpRoutes .empty[IO ])
29- ).toResource
30- }
39+ HttpRoutes .empty[IO ]
3140
3241end makeProxyRoutes
42+
43+ def proxyConf (proxyTarget : Option [Port ], pathPrefix : Option [String ]): Resource [IO , Option [(Equilibrium , String )]] =
44+ proxyTarget
45+ .zip(pathPrefix)
46+ .traverse {
47+ (pt, prfx) =>
48+ IO (
49+ (
50+ Equilibrium (
51+ ProxyConfig .HttpProxyConfig (
52+ servers = NonEmptyList (
53+ Server (
54+ listen = pt,
55+ serverNames = List (" localhost" ),
56+ locations = List (
57+ ProxyConfig .Location (
58+ matcher = LocationMatcher .Prefix (prfx),
59+ proxyPass = s " http:// $$ backend "
60+ )
61+ )
62+ ),
63+ List ()
64+ ),
65+ upstreams = List (
66+ ProxyConfig .Upstream (
67+ name = " backend" ,
68+ servers = NonEmptyList (
69+ ProxyConfig .UpstreamServer (
70+ host = Host .fromString(" localhost" ).get,
71+ port = pt,
72+ weight = 5
73+ ),
74+ List ()
75+ )
76+ )
77+ )
78+ )
79+ ),
80+ prfx
81+ )
82+ )
83+
84+ }
85+ .toResource
0 commit comments