diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index 95fc1e5..ec9d7c6 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -1,11 +1,11 @@ - + postgresql true org.postgresql.Driver - jdbc:postgresql://localhost:5432/postgres + jdbc:postgresql://localhost:5101/yobble_db diff --git a/src/main/scala/org/yobble/scala_monolith/Main.scala b/src/main/scala/org/yobble/scala_monolith/Main.scala index 855037c..1172274 100644 --- a/src/main/scala/org/yobble/scala_monolith/Main.scala +++ b/src/main/scala/org/yobble/scala_monolith/Main.scala @@ -22,8 +22,12 @@ object Main extends IOApp { .withHttpApp(httpAppWithMiddleware) .build .use { server => - IO.println(s"Server running at http://localhost:${server.address.getPort}") *> - IO.never + // Здесь вручную выводим "localhost" в лог, хотя слушаем "0.0.0.0" + for { + _ <- IO.println(s"Server running at http://localhost:$port") + _ <- IO.println(s"Swagger UI available at http://localhost:$port/docs") + _ <- IO.never + } yield () } .as(ExitCode.Success) } diff --git a/src/main/scala/org/yobble/scala_monolith/api/endpoint/ErrorsEndpoint.scala b/src/main/scala/org/yobble/scala_monolith/api/endpoint/ErrorsEndpoint.scala index 5e1c85b..a54f0be 100644 --- a/src/main/scala/org/yobble/scala_monolith/api/endpoint/ErrorsEndpoint.scala +++ b/src/main/scala/org/yobble/scala_monolith/api/endpoint/ErrorsEndpoint.scala @@ -27,5 +27,9 @@ object ErrorsEndpoint { oneOfVariant(StatusCode.ServiceUnavailable, jsonBody[ErrorResponse].description("Service Unavailable").example(ErrorExamples.unavailable)) ) ) - .out(jsonBody[BaseResponse]) + .out( + jsonBody[BaseResponse] + .description("Successful Response") + .example(BaseResponse(message = "errors")) + ) } diff --git a/src/main/scala/org/yobble/scala_monolith/api/endpoint/PingEndpoint.scala b/src/main/scala/org/yobble/scala_monolith/api/endpoint/PingEndpoint.scala index 2b3a4b3..e27e1f8 100644 --- a/src/main/scala/org/yobble/scala_monolith/api/endpoint/PingEndpoint.scala +++ b/src/main/scala/org/yobble/scala_monolith/api/endpoint/PingEndpoint.scala @@ -1,19 +1,27 @@ package org.yobble.scala_monolith.api.endpoint -import sttp.tapir._ +import sttp.tapir.* import sttp.tapir.json.circe.jsonBody -import sttp.tapir.generic.auto._ -import org.yobble.scala_monolith.api.response.ErrorResponse -import io.circe.generic.auto._ +import sttp.tapir.generic.auto.* +import org.yobble.scala_monolith.api.response.{BaseResponse, ErrorResponse} +import io.circe.generic.auto.* +import org.yobble.scala_monolith.api.util.ErrorExamples +import sttp.model.StatusCode object PingEndpoint { - case class Pong(status: String, message: String) - val pingEndpoint = endpoint .get .in("ping") .tags(List("Info")) - .out(jsonBody[Pong]) - .errorOut(jsonBody[ErrorResponse]) + .errorOut( + oneOf[ErrorResponse]( + oneOfVariant(StatusCode.InternalServerError, jsonBody[ErrorResponse].description("Internal Server Error").example(ErrorExamples.internal)) + ) + ) .description("Check if the server is running") + .out( + jsonBody[BaseResponse] + .description("Successful Response") + .example(BaseResponse(message = "pong")) + ) } \ No newline at end of file diff --git a/src/main/scala/org/yobble/scala_monolith/api/response/BaseResponse.scala b/src/main/scala/org/yobble/scala_monolith/api/response/BaseResponse.scala index 5e1f679..3b3795c 100644 --- a/src/main/scala/org/yobble/scala_monolith/api/response/BaseResponse.scala +++ b/src/main/scala/org/yobble/scala_monolith/api/response/BaseResponse.scala @@ -1,3 +1,6 @@ package org.yobble.scala_monolith.api.response -case class BaseResponse(status: String, message: String) +case class BaseResponse( + status: String = "fine", + message: String + ) \ No newline at end of file diff --git a/src/main/scala/org/yobble/scala_monolith/api/route/AllServerEndpoints.scala b/src/main/scala/org/yobble/scala_monolith/api/route/AllServerEndpoints.scala index dc93044..84cba4e 100644 --- a/src/main/scala/org/yobble/scala_monolith/api/route/AllServerEndpoints.scala +++ b/src/main/scala/org/yobble/scala_monolith/api/route/AllServerEndpoints.scala @@ -3,13 +3,12 @@ package org.yobble.scala_monolith.api.route import cats.effect.IO import sttp.tapir.server.ServerEndpoint import org.yobble.scala_monolith.api.endpoint.PingEndpoint -import org.yobble.scala_monolith.api.endpoint.PingEndpoint.Pong import org.yobble.scala_monolith.api.endpoint.ErrorsEndpoint import org.yobble.scala_monolith.api.response.BaseResponse object AllServerEndpoints { val all: List[ServerEndpoint[Any, IO]] = List( - PingEndpoint.pingEndpoint.serverLogicSuccess(_ => IO.pure(Pong("ok", "pong"))), - ErrorsEndpoint.errorsEndpoint.serverLogicSuccess(_ => IO.pure(BaseResponse("fine", "errors"))) + PingEndpoint.pingEndpoint.serverLogicSuccess(_ => IO.pure(BaseResponse(message = "pong"))), + ErrorsEndpoint.errorsEndpoint.serverLogicSuccess(_ => IO.pure(BaseResponse(message = "errors"))) ) } \ No newline at end of file diff --git a/src/main/scala/org/yobble/scala_monolith/api/util/ErrorUtils.scala b/src/main/scala/org/yobble/scala_monolith/api/util/ErrorUtils.scala index c238ed5..d0d8443 100644 --- a/src/main/scala/org/yobble/scala_monolith/api/util/ErrorUtils.scala +++ b/src/main/scala/org/yobble/scala_monolith/api/util/ErrorUtils.scala @@ -17,7 +17,7 @@ object ErrorUtils { ErrorResponse(code = 404, errors = List(ErrorDetail("resource", message))) def methodNotAllowed(message: String = "Method not allowed"): ErrorResponse = - ErrorResponse(code = 405, errors = List(ErrorDetail("method", message))) + ErrorResponse(code = 405, errors = List(ErrorDetail("request", message))) def conflict(message: String = "Conflict"): ErrorResponse = ErrorResponse(code = 409, errors = List(ErrorDetail("conflict", message))) @@ -32,7 +32,7 @@ object ErrorUtils { ErrorResponse(code = 500, errors = List(ErrorDetail("server", message))) def serviceUnavailableError(message: String = "Service unavailable"): ErrorResponse = - ErrorResponse(code = 503, errors = List(ErrorDetail("request", message))) + ErrorResponse(code = 503, errors = List(ErrorDetail("proxy", message))) } object ErrorExamples { @@ -60,7 +60,7 @@ object ErrorExamples { val notAllowed: ErrorResponse = ErrorResponse( code = 405, - errors = List(ErrorDetail("resource", "Method not allowed on this endpoint")) + errors = List(ErrorDetail("request", "Method not allowed on this endpoint")) ) val conflict: ErrorResponse = ErrorResponse( @@ -75,7 +75,7 @@ object ErrorExamples { val validation: ErrorResponse = ErrorResponse( code = 422, - errors = List(ErrorDetail("login", "Login must not contain whitespace characters")) + errors = List(ErrorDetail("example login", "example Login must not contain whitespace characters")) ) val internal: ErrorResponse = ErrorResponse( @@ -85,7 +85,7 @@ object ErrorExamples { val unavailable: ErrorResponse = ErrorResponse( code = 503, - errors = List(ErrorDetail("request", "Service unavailable.")) + errors = List(ErrorDetail("proxy", "Service unavailable.")) ) }