Last active
August 18, 2023 07:29
-
-
Save dacr/db4538d83b565652ec7cc449d73f6324 to your computer and use it in GitHub Desktop.
hello world http server (zhttp zio based) using tapir with redoc documentation / published by https://github.com/dacr/code-examples-manager #eb44baa6-35c5-45f4-9002-1de290a653c4/f2651bbac590110fa192dc607a52f61198c98606
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
// summary : hello world http server (zhttp zio based) using tapir with redoc documentation | |
// keywords : scala, zio, tapir, http, zhttp, swagger, endpoints, @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 : eb44baa6-35c5-45f4-9002-1de290a653c4 | |
// created-on : 2023-05-05T17:18:35+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/hello | |
// --------------------- | |
//> using scala "3.3.0" | |
//> using dep "com.softwaremill.sttp.tapir::tapir-zio:1.6.0" | |
//> using dep "com.softwaremill.sttp.tapir::tapir-zio-http-server:1.6.0" | |
//> using dep "com.softwaremill.sttp.tapir::tapir-redoc-bundle:1.6.0" | |
// --------------------- | |
/* | |
other examples : | |
- https://github.com/softwaremill/tapir/blob/master/examples/src/main/scala/sttp/tapir/examples/ZioExampleZioHttpServer.scala | |
*/ | |
import sttp.tapir.ztapir.* | |
import sttp.tapir.server.ziohttp.ZioHttpInterpreter | |
import sttp.apispec.openapi.Info | |
import sttp.tapir.redoc.bundle.RedocInterpreter | |
import zio.*, zio.http.Server | |
/* | |
curl -L http://127.0.0.1:8080/docs | |
curl http://127.0.0.1:8080/hello | |
curl http://127.0.0.1:8080/hi?name=david | |
*/ | |
object WebApp extends ZIOAppDefault { | |
// -------------------------------------------------- | |
def helloLogic = ZIO.succeed("Hello world") | |
val helloEndPoint = | |
endpoint | |
.description("Returns greeting") | |
.get | |
.in("hello") | |
.out(stringBody) | |
val helloRoute = helloEndPoint.zServerLogic[Any](_ => helloLogic) | |
// -------------------------------------------------- | |
def hiLogic(name: String) = ZIO.succeed(s"Hi $name") | |
val hiEndPoint = | |
endpoint | |
.description("Returns parameterized greeting") | |
.get | |
.in("hi") | |
.in(query[String]("name").description("someone name")) | |
.out(stringBody) | |
val hiRoute = hiEndPoint.zServerLogic[Any](hiLogic) | |
// -------------------------------------------------- | |
val swaggerEndpoints = | |
RedocInterpreter() | |
.fromServerEndpoints( | |
List(helloRoute, hiRoute), | |
Info(title = "Greeting API", version = "1.0", description = Some("Everything required to be polite")) | |
) | |
// -------------------------------------------------- | |
val routes = | |
ZioHttpInterpreter() | |
.toHttp(List(helloRoute, hiRoute) ++ swaggerEndpoints) // zioHttpInterpreter(s) can also be merged using <> (orElse) | |
// -------------------------------------------------- | |
override def run = Server.serve(routes.withDefaultErrorResponse).provide(Server.default) | |
} | |
WebApp.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment