From 0750e8fa9415c8726ebc3f730bc5e39986c7a159 Mon Sep 17 00:00:00 2001 From: mmvpm Date: Sun, 3 Mar 2024 16:35:03 +0300 Subject: [PATCH] add ping --- .../main/scala/com/github/mmvpm/stub/Main.scala | 4 +++- .../com/github/mmvpm/stub/api/PingHandler.scala | 17 +++++++++++++++++ .../com/github/mmvpm/stub/api/StubHandler.scala | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 stub/src/main/scala/com/github/mmvpm/stub/api/PingHandler.scala diff --git a/stub/src/main/scala/com/github/mmvpm/stub/Main.scala b/stub/src/main/scala/com/github/mmvpm/stub/Main.scala index 002461d..966e87d 100644 --- a/stub/src/main/scala/com/github/mmvpm/stub/Main.scala +++ b/stub/src/main/scala/com/github/mmvpm/stub/Main.scala @@ -30,11 +30,13 @@ object Main extends IOApp { for { _ <- IO.pure(0) + pingHandler: PingHandler[IO] = new PingHandler[IO] + stubDao: StubDao[IO] = new StubDaoPostgresql[IO] stubService: StubService[IO] = new StubServiceImpl[IO](stubDao) stubHandler: StubHandler[IO] = new StubHandler[IO](stubService) - handlers = List(stubHandler) + handlers = List(pingHandler, stubHandler) endpoints <- IO.delay(handlers.flatMap(_.endpoints)) routes = Http4sServerInterpreter[IO].toRoutes(swaggerBy(endpoints) ++ endpoints) server <- serverBuilder(config, routes).value.rethrow diff --git a/stub/src/main/scala/com/github/mmvpm/stub/api/PingHandler.scala b/stub/src/main/scala/com/github/mmvpm/stub/api/PingHandler.scala new file mode 100644 index 0000000..0c1f9f5 --- /dev/null +++ b/stub/src/main/scala/com/github/mmvpm/stub/api/PingHandler.scala @@ -0,0 +1,17 @@ +package com.github.mmvpm.stub.api +import cats.Applicative +import sttp.tapir._ +import sttp.tapir.server.ServerEndpoint + +class PingHandler[F[_]: Applicative] extends Handler[F] { + + private val ping: ServerEndpoint[Any, F] = + endpoint.get + .summary("Ping") + .in("api" / "v1" / "ping") + .out(stringBody) + .serverLogic(_ => Applicative[F].pure(Right("pong"))) + + override def endpoints: List[ServerEndpoint[Any, F]] = + List(ping).map(_.withTag("util")) +} diff --git a/stub/src/main/scala/com/github/mmvpm/stub/api/StubHandler.scala b/stub/src/main/scala/com/github/mmvpm/stub/api/StubHandler.scala index 80de8dd..e0181da 100644 --- a/stub/src/main/scala/com/github/mmvpm/stub/api/StubHandler.scala +++ b/stub/src/main/scala/com/github/mmvpm/stub/api/StubHandler.scala @@ -48,5 +48,5 @@ class StubHandler[F[_]](stubService: StubService[F]) extends Handler[F] { .serverLogic((stubService.updateStub _).tupled(_).value) override def endpoints: List[ServerEndpoint[Any, F]] = - List(getStub, getStubs, createStub, updateStub) + List(getStub, getStubs, createStub, updateStub).map(_.withTag("stub")) }