Last active
May 25, 2024 08:39
-
-
Save dacr/4a5d9e705493fe337e25d516003da910 to your computer and use it in GitHub Desktop.
hello world http server (zhttp zio based) using tapir with swagger documentation / published by https://github.com/dacr/code-examples-manager #6352725e-e1c3-4647-9f82-75fa0f80c9f3/a00fade7f887d208421775e7b488a738d1a85640
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 : hello world http server (zhttp zio based) using tapir with swagger 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 : 6352725e-e1c3-4647-9f82-75fa0f80c9f3 | |
// created-on : 2021-11-21T08:39:28+01: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.4.2" | |
//> 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 lib "com.softwaremill.sttp.tapir::tapir-swagger-ui-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.swagger.bundle.SwaggerInterpreter | |
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 = | |
SwaggerInterpreter() | |
.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