Created
June 15, 2024 07:58
-
-
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/4b3ee822d4182368e21d5e90f86b8c31dafc0e8e
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 NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// 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