Last active
August 18, 2023 07:29
-
-
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/bd2787d0721adf79c7136ce3dd06af8b8672c948
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 (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.3.0" | |
//> 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