Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active August 18, 2023 07:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/4a5d9e705493fe337e25d516003da910 to your computer and use it in GitHub Desktop.
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/8d9453e0de7b21f5d4f326ef3760e9033f33ceea
// 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.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 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