Skip to content

Instantly share code, notes, and snippets.

@zetashift
Created June 10, 2022 16:09
Show Gist options
  • Save zetashift/bdefce3d692e6a626e6bf2ecaf218f48 to your computer and use it in GitHub Desktop.
Save zetashift/bdefce3d692e6a626e6bf2ecaf218f48 to your computer and use it in GitHub Desktop.
Http4s and scala-cli
//> using scala "3.1"
//> using lib "org.http4s::http4s-dsl:0.23.12"
//> using lib "org.http4s::http4s-ember-server:0.23.12"
//> using lib "org.http4s::http4s-ember-client:0.23.12"
import cats.effect._
import org.http4s._
import org.http4s.dsl.io._
import org.http4s.ember.server.EmberServerBuilder
import org.http4s.server.Router
import com.comcast.ip4s.{host, port}
object HelloApp extends IOApp:
def helloRoutes = HttpRoutes.of[IO] {
case GET -> Root => Ok("Hello World!")
case GET -> Root / "hello" / name => Ok(s"Hello, $name!")
}
.orNotFound
def run(args: List[String]): IO[ExitCode] =
val host = host"0.0.0.0"
val port = port"8080"
EmberServerBuilder
.default[IO]
.withHost(host)
.withPort(port)
.withHttpApp(helloRoutes)
.build
.use(_ => IO.never)
.as(ExitCode.Success)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment