Created
April 28, 2021 22:57
-
-
Save ChristopherDavenport/c12a9ced430820f72aa2ab7aa4135889 to your computer and use it in GitHub Desktop.
Generic Json Http Server with Catscript
This file contains hidden or 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
| #!/usr/bin/env catscript | |
| // dependency: "org.http4s" %% "http4s-ember-server" % "1.0.0-M21" | |
| // dependency: "org.http4s" %% "http4s-circe" % "1.0.0-M21" | |
| // dependency: "org.http4s" %% "http4s-dsl" % "1.0.0-M21" | |
| // dependency: "ch.qos.logback" % "logback-classic" % "1.2.3" | |
| import org.http4s._ | |
| import org.http4s.circe._ | |
| import org.http4s.circe.middleware._ | |
| import org.http4s.dsl.io._ | |
| import org.http4s.ember.server._ | |
| import org.http4s.implicits._ | |
| import cats.syntax.all._ | |
| import cats.effect._ | |
| import cats.effect.std._ | |
| import com.comcast.ip4s._ | |
| def routes = HttpRoutes.of[IO]{ | |
| case req => | |
| for { | |
| json <- req.asJson | |
| _ <- Console[IO].println(json) | |
| out <- Ok() | |
| } yield out | |
| } | |
| def run = for { | |
| _ <- EmberServerBuilder.default[IO] | |
| .withHttpApp(routes.orNotFound) | |
| .withHost(host"0.0.0.0") | |
| .withPort(port"8080") | |
| .build | |
| .use(_ => IO.never) | |
| } yield () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment