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