Last active
February 3, 2026 20:26
-
-
Save dacr/2946747c334b9fb441ff2993cbd59c46 to your computer and use it in GitHub Desktop.
Simple http4s http server library example / published by https://github.com/dacr/code-examples-manager #43f85d6d-d575-49b9-9862-71e75c87788f/1e55ab629f15758826931959df5fc70b5d026f58
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
| // summary : Simple http4s http server library example | |
| // keywords : scala, http4s, cats, http-server, tapir, @testable, @exclusive | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 43f85d6d-d575-49b9-9862-71e75c87788f | |
| // created-on : 2024-06-13T14:14:06+02:00 | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : scala-cli $file | |
| // test-with : curl http://127.0.0.1:8080/hi?name=joe | |
| // --------------------- | |
| //> using scala "3.4.2" | |
| //> using dep "com.softwaremill.sttp.tapir::tapir-zio:1.10.9" | |
| //> using dep "com.softwaremill.sttp.tapir::tapir-http4s-server-zio:1.10.9" | |
| //> using dep "com.softwaremill.sttp.tapir::tapir-json-zio:1.10.9" | |
| //> using dep "com.softwaremill.sttp.tapir::tapir-swagger-ui-bundle:1.10.9" | |
| //> using dep "com.softwaremill.sttp.tapir::tapir-openapi-docs:1.10.9" | |
| //> using dep "org.http4s::http4s-blaze-server:0.23.16" | |
| //> using dep "org.slf4j:slf4j-simple:2.0.13" | |
| //> using options -Ykind-projector:underscores | |
| // --------------------- | |
| import cats.syntax.all.* | |
| import org.http4s.* | |
| import org.http4s.blaze.server.BlazeServerBuilder | |
| import org.http4s.server.Router | |
| import org.http4s.implicits.* | |
| import sttp.tapir.PublicEndpoint | |
| import sttp.tapir.generic.auto.* | |
| import sttp.tapir.swagger.bundle.SwaggerInterpreter | |
| import sttp.tapir.server.http4s.ztapir.ZHttp4sServerInterpreter | |
| import sttp.apispec.openapi.Info | |
| import sttp.tapir.ztapir.* | |
| import zio.interop.catz.* | |
| import zio.{Console, Task, ZIO, ZIOAppDefault} | |
| import zio.durationInt | |
| object Hello extends ZIOAppDefault { | |
| def hiLogic(givenName: Option[String]) = ZIO.succeed( | |
| givenName match { | |
| case None => s"Hi" | |
| case Some(name) => s"Hi $name" | |
| } | |
| ) | |
| val hiEndPoint = | |
| endpoint | |
| .description("Returns parameterized greeting") | |
| .get | |
| .in("hi") | |
| .in(query[Option[String]]("name").description("someone name")) | |
| .out(stringBody) | |
| val hiEndPointWired = hiEndPoint.zServerLogic[Any](hiLogic) | |
| val docEndPointWired = | |
| SwaggerInterpreter() | |
| .fromServerEndpoints( | |
| List(hiEndPointWired), | |
| Info(title = "Greeting API", version = "1.0", description = Some("Everything required to be polite")) | |
| ) | |
| val routes: HttpRoutes[Task] = ZHttp4sServerInterpreter() | |
| .from(hiEndPointWired :: docEndPointWired) | |
| .toRoutes | |
| def run = { | |
| ZIO.executor.flatMap(executor => | |
| BlazeServerBuilder[Task] | |
| .withExecutionContext(executor.asExecutionContext) | |
| .bindLocal(8080) | |
| .withHttpApp(Router("/" -> routes).orNotFound) | |
| .serve | |
| .compile | |
| .drain | |
| ).timeout(10.seconds) | |
| } | |
| } | |
| Hello.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment