Created
July 7, 2024 18:04
-
-
Save ghostbuster91/0b24d74bb93f4fc3317e9ff06eafeaf8 to your computer and use it in GitHub Desktop.
Simple http logging proxy written with scala & scala-cli
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
//> using scala "3.3.0" | |
//> using dep "org.http4s::http4s-ember-client:0.23.27" | |
//> using dep "org.http4s::http4s-ember-server:0.23.27" | |
//> using dep "org.http4s::http4s-dsl:0.23.27" | |
//> using dep "ch.qos.logback:logback-classic:1.5.6", | |
import org.http4s.HttpRoutes | |
import cats.effect.IO | |
import org.http4s.ember.server.EmberServerBuilder | |
import org.http4s.ember.client.EmberClientBuilder | |
import org.http4s.dsl.* | |
import com.comcast.ip4s.ipv4 | |
import com.comcast.ip4s.port | |
import org.http4s.HttpApp | |
import org.http4s.Response | |
import org.http4s.Request | |
import org.http4s.server.middleware.Logger | |
import org.http4s.Uri | |
import cats.effect.IOApp | |
import cats.effect.ExitCode | |
import org.http4s.Uri.Authority | |
object Main extends IOApp { | |
override def run(args: List[String]): IO[ExitCode] = (for { | |
c <- client | |
s <- server { req => | |
org.http4s.client.middleware | |
.Logger(true, true)(c) | |
.run( | |
req.withUri( | |
req.uri.copy(authority = | |
Some(Authority(userInfo = None, port = Some(7777))) | |
) | |
) | |
) | |
.use { response => | |
IO(response) | |
} | |
} | |
} yield ()).useForever | |
val client = EmberClientBuilder | |
.default[IO] | |
.build | |
def server(f: Request[IO] => IO[Response[IO]]) = EmberServerBuilder | |
.default[IO] | |
.withHost(ipv4"0.0.0.0") | |
.withPort(port"8080") | |
.withHttpApp(Logger.httpApp(true, true)(HttpApp(f))) | |
.build | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In order to run it just call
scala-cli https://gist.github.com/ghostbuster91/0b24d74bb93f4fc3317e9ff06eafeaf8