Skip to content

Instantly share code, notes, and snippets.

@ASRagab
Last active April 21, 2023 03:44
Show Gist options
  • Save ASRagab/d74409042bc080bb8ec88972a3eaade3 to your computer and use it in GitHub Desktop.
Save ASRagab/d74409042bc080bb8ec88972a3eaade3 to your computer and use it in GitHub Desktop.
ZIO Tapir Issue Reproduction
//> using scala "2.13.10"
//> using dep "dev.zio::zio:2.0.13"
//> using dep "dev.zio::zio-http:0.0.5"
//> using dep "com.softwaremill.sttp.tapir::tapir-zio-http-server:1.2.12"
//> using dep "com.softwaremill.sttp.tapir::tapir-core:1.2.12"
//> using dep "com.softwaremill.sttp.tapir::tapir-swagger-ui-bundle:1.2.12"
/**
* This is a simple example of using ZIO Tapir to create an HTTP server.
* To run this example, use the following command: `scala-cli run https://gist.github.com/ASRagab/d74409042bc080bb8ec88972a3eaade3`
* Then access the swagger/openapi documentation at http://localhost:8080/docs
* The server will run until you press Ctrl-C
*
* TO REPRODUCE THE ISSUE use 3.0.0-RC1 of zio-http instead of 0.0.5
* The server will not startup, you cannot reach any of the endpoints and no errors are reported
*/
import sttp.tapir.PublicEndpoint
import sttp.tapir.server.ServerEndpoint
import sttp.tapir.server.ziohttp.ZioHttpInterpreter
import sttp.tapir.swagger.bundle.SwaggerInterpreter
import sttp.tapir.ztapir._
import zio._
import zio.http.{HttpApp, Server}
object Simple extends ZIOAppDefault {
val PORT = 8080
val helloEndpoint: PublicEndpoint[Unit, String, String, Any] =
endpoint
.in("api" / "v1" / "hello")
.get
.out(stringBody)
.errorOut(stringBody)
val lengthEndpoint: PublicEndpoint[String, String, Int, Any] =
endpoint
.in("api" / "v1" / "length")
.post
.in(stringBody)
.out(plainBody[Int])
.errorOut(stringBody)
val swaggerEndpoints: List[ServerEndpoint[Any, Task]] =
SwaggerInterpreter()
.fromEndpoints(
List(helloEndpoint, lengthEndpoint),
"ZIO Tapir OpenAPI Explorer",
"1.0"
)
val serverEndpoints: List[ServerEndpoint[Any, Task]] =
List(
helloEndpoint.zServerLogic(_ => ZIO.succeed("Hello, world!")),
lengthEndpoint.zServerLogic(input => ZIO.succeed(input.length))
)
val httpApp: HttpApp[Any & Any, Throwable] = ZioHttpInterpreter().toHttp(serverEndpoints ++ swaggerEndpoints)
override def run: ZIO[Any, Any, Any] =
Console.printLine(s"Starting server at http://localhost:${PORT}/docs") *>
Server.serve(httpApp.withDefaultErrorResponse)
.provide(Server.defaultWithPort(PORT))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment