Last active
May 25, 2024 08:39
-
-
Save dacr/a315f95ae852fd5b1bef819ab73bf4ec to your computer and use it in GitHub Desktop.
hello world http server (vertx based) using tapir with swagger documentation / published by https://github.com/dacr/code-examples-manager #859dd278-983f-4f5d-9d9c-0be7f624703c/da1e48cf53b2e79e3735c726f8d2beacf082954c
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 (vertx based) using tapir with swagger documentation | |
// keywords : scala, zio, tapir, http, vertx, 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 : 859dd278-983f-4f5d-9d9c-0be7f624703c | |
// created-on : 2021-11-22T13:02:43+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-core:1.6.0" | |
//> using dep "com.softwaremill.sttp.tapir::tapir-vertx-server:1.6.0" | |
//> using dep "com.softwaremill.sttp.tapir::tapir-redoc-bundle:1.6.0" | |
// --------------------- | |
import sttp.tapir._ | |
import sttp.tapir.server.vertx.VertxFutureServerInterpreter | |
import sttp.tapir.server.vertx.VertxFutureServerInterpreter.* | |
import io.vertx.core.Vertx | |
import io.vertx.ext.web.* | |
import scala.util.Either | |
import sttp.tapir.Endpoint | |
import sttp.apispec.openapi.Info | |
import sttp.tapir.redoc.bundle.RedocInterpreter | |
import scala.concurrent.Await | |
import scala.concurrent.duration.Duration | |
implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global | |
import scala.concurrent.Future | |
/* | |
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 Main { | |
// -------------------------------------------------- | |
def helloLogic = Future(Right("Hello world")) | |
val helloEndPoint = | |
endpoint | |
.description("Returns greeting") | |
.get | |
.in("hello") | |
.out(stringBody) | |
val helloRoute = | |
helloEndPoint.serverLogic(_ => helloLogic) | |
// -------------------------------------------------- | |
def hiLogic(name: String) = Future(Right(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.serverLogic(hiLogic) | |
// -------------------------------------------------- | |
val swaggerEndpoints = RedocInterpreter().fromEndpoints[Future]( | |
List(helloEndPoint, hiEndPoint), | |
Info(title = "Greeting API", version = "1.0", description = Some("Everything required to be polite")) | |
) | |
// -------------------------------------------------- | |
val endpoints = List(helloRoute, hiRoute) ++ swaggerEndpoints | |
val routes = endpoints.map(endpoint => VertxFutureServerInterpreter().route(endpoint)) | |
// -------------------------------------------------- | |
def main(): Unit = { | |
val vertx = Vertx.vertx() | |
val server = vertx.createHttpServer() | |
val router = Router.router(vertx) | |
routes.foreach(route => route(router)) | |
Await.result(server.requestHandler(router).listen(8080).asScala, Duration.Inf) | |
} | |
} | |
Main.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment