-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from Quafadas/proxyWork
Proxy fixes
- Loading branch information
Showing
10 changed files
with
139 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import com.comcast.ip4s.* | ||
|
||
import cats.data.NonEmptyList | ||
|
||
import cats.syntax.all.* | ||
|
||
object ProxyConfig: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,86 @@ | ||
import org.http4s.HttpRoutes | ||
import org.http4s.client.Client | ||
import org.http4s.server.Router | ||
import org.http4s.server.middleware.Logger | ||
|
||
import com.comcast.ip4s.Host | ||
import com.comcast.ip4s.Port | ||
|
||
import scribe.Scribe | ||
|
||
import cats.data.NonEmptyList | ||
import cats.effect.IO | ||
import cats.effect.kernel.Resource | ||
import cats.effect.std.Random | ||
import cats.syntax.all.* | ||
|
||
import ProxyConfig.Equilibrium | ||
import ProxyConfig.LocationMatcher | ||
import ProxyConfig.Server | ||
|
||
def makeProxyRoutes( | ||
client: Client[IO], | ||
pathPrefix: Option[String], | ||
proxyConfig: Resource[IO, Option[Equilibrium]] | ||
)(logger: Scribe[IO]): Resource[IO, HttpRoutes[IO]] = | ||
proxyConfig.flatMap { | ||
case Some(pc) => | ||
{ | ||
given R: Random[IO] = Random.javaUtilConcurrentThreadLocalRandom[IO] | ||
logger.debug("setup proxy server") >> | ||
IO(HttpProxy.servers[IO](pc, client, pathPrefix.getOrElse(???)).head._2) | ||
}.toResource | ||
proxyConfig: Option[(Equilibrium, String)] | ||
)(logger: Scribe[IO]): HttpRoutes[IO] = | ||
proxyConfig match | ||
case Some((pc, pathPrefix)) => | ||
given R: Random[IO] = Random.javaUtilConcurrentThreadLocalRandom[IO] | ||
Logger.httpRoutes[IO]( | ||
logHeaders = true, | ||
logBody = true, | ||
redactHeadersWhen = _ => false, | ||
logAction = Some((msg: String) => logger.trace(msg)) | ||
)( | ||
Router( | ||
pathPrefix -> HttpProxy.servers[IO](pc, client, pathPrefix).head._2 | ||
) | ||
) | ||
|
||
case None => | ||
( | ||
logger.debug("no proxy set") >> | ||
IO(HttpRoutes.empty[IO]) | ||
).toResource | ||
} | ||
HttpRoutes.empty[IO] | ||
|
||
end makeProxyRoutes | ||
|
||
def proxyConf(proxyTarget: Option[Port], pathPrefix: Option[String]): Resource[IO, Option[(Equilibrium, String)]] = | ||
proxyTarget | ||
.zip(pathPrefix) | ||
.traverse { | ||
(pt, prfx) => | ||
IO( | ||
( | ||
Equilibrium( | ||
ProxyConfig.HttpProxyConfig( | ||
servers = NonEmptyList( | ||
Server( | ||
listen = pt, | ||
serverNames = List("localhost"), | ||
locations = List( | ||
ProxyConfig.Location( | ||
matcher = LocationMatcher.Prefix(prfx), | ||
proxyPass = "http://$backend" | ||
) | ||
) | ||
), | ||
List() | ||
), | ||
upstreams = List( | ||
ProxyConfig.Upstream( | ||
name = "backend", | ||
servers = NonEmptyList( | ||
ProxyConfig.UpstreamServer( | ||
host = Host.fromString("localhost").get, | ||
port = pt, | ||
weight = 5 | ||
), | ||
List() | ||
) | ||
) | ||
) | ||
) | ||
), | ||
prfx | ||
) | ||
) | ||
|
||
} | ||
.toResource |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.