-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: switch to vertx server backend #223
Draft
geminicaprograms
wants to merge
1
commit into
main
Choose a base branch
from
fix/vertx
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,23 @@ | ||
package com.softwaremill.adopttapir.http | ||
|
||
import cats.effect.std.Dispatcher | ||
import cats.effect.{IO, Resource} | ||
import com.softwaremill.adopttapir.infrastructure.CorrelationIdInterceptor | ||
import com.softwaremill.adopttapir.logging.FLogger | ||
import com.softwaremill.adopttapir.util.ServerEndpoints | ||
import com.typesafe.scalalogging.StrictLogging | ||
import org.http4s.HttpRoutes | ||
import org.http4s.blaze.server.BlazeServerBuilder | ||
import io.vertx.core.Vertx | ||
import io.vertx.core.http.HttpServer | ||
import io.vertx.ext.web.Router | ||
import sttp.capabilities.fs2.Fs2Streams | ||
import sttp.model.Method | ||
import sttp.tapir._ | ||
import sttp.tapir.server.ServerEndpoint | ||
import sttp.tapir.server.http4s.{Http4sServerInterpreter, Http4sServerOptions} | ||
import sttp.tapir.server.interceptor.cors.CORSInterceptor | ||
import sttp.tapir.server.interceptor.cors.CORSConfig.AllowedMethods | ||
import sttp.tapir.server.interceptor.cors.{CORSConfig, CORSInterceptor} | ||
import sttp.tapir.server.metrics.prometheus.PrometheusMetrics | ||
import sttp.tapir.server.model.ValuedEndpointOutput | ||
import sttp.tapir.server.vertx.cats.{VertxCatsServerInterpreter, VertxCatsServerOptions} | ||
import sttp.tapir.server.vertx.cats.VertxCatsServerInterpreter._ | ||
import sttp.tapir.static.ResourcesOptions | ||
import sttp.tapir.swagger.SwaggerUIOptions | ||
import sttp.tapir.swagger.bundle.SwaggerInterpreter | ||
|
@@ -35,27 +39,16 @@ class HttpApi( | |
) extends StrictLogging { | ||
private val apiContextPath = List("api", "v1") | ||
|
||
private val serverOptions: Http4sServerOptions[IO] = Http4sServerOptions | ||
.customiseInterceptors[IO] | ||
.prependInterceptor(CorrelationIdInterceptor) | ||
// all errors are formatted as json, and there are no other additional http4s routes | ||
.defaultHandlers(msg => ValuedEndpointOutput(http.jsonErrorOutOutput, Error_OUT(msg)), notFoundWhenRejected = true) | ||
.serverLog { | ||
// using a context-aware logger for http logging | ||
val flogger = new FLogger(logger) | ||
Http4sServerOptions | ||
.defaultServerLog[IO] | ||
.doLogWhenHandled((msg, e) => e.fold(flogger.debug[IO](msg))(flogger.debug(msg, _))) | ||
.doLogAllDecodeFailures((msg, e) => e.fold(flogger.debug[IO](msg))(flogger.debug(msg, _))) | ||
.doLogExceptions((msg, e) => flogger.error[IO](msg, e)) | ||
.doLogWhenReceived(msg => flogger.debug[IO](msg)) | ||
} | ||
.corsInterceptor(CORSInterceptor.default[IO]) | ||
.metricsInterceptor(prometheusMetrics.metricsInterceptor()) | ||
.options | ||
|
||
private lazy val publicRoutes: HttpRoutes[IO] = Http4sServerInterpreter(serverOptions).toRoutes(allPublicEndpoints) | ||
private lazy val adminRoutes: HttpRoutes[IO] = Http4sServerInterpreter(serverOptions).toRoutes(allAdminEndpoints) | ||
private def serverOptions(dispatcher: Dispatcher[IO]): VertxCatsServerOptions[IO] = | ||
VertxCatsServerOptions | ||
.customiseInterceptors[IO](dispatcher) | ||
.prependInterceptor(CorrelationIdInterceptor) | ||
// all errors are formatted as json, and there are no other additional http4s routes | ||
.defaultHandlers(msg => ValuedEndpointOutput(http.jsonErrorOutOutput, Error_OUT(msg)), notFoundWhenRejected = true) | ||
// TODO customise the serverLog when available | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in latest tapir |
||
.corsInterceptor(CORSInterceptor.default[IO]) | ||
.metricsInterceptor(prometheusMetrics.metricsInterceptor()) | ||
.options | ||
|
||
lazy val allPublicEndpoints: List[ServerEndpoint[Any with Fs2Streams[IO], IO]] = { | ||
// creating the documentation using `mainEndpoints` without the /api/v1 context path; instead, a server will be added | ||
|
@@ -84,16 +77,26 @@ class HttpApi( | |
(adminEndpoints ++ List(prometheusMetrics.metricsEndpoint)).toList | ||
|
||
/** The resource describing the HTTP server; binds when the resource is allocated. */ | ||
lazy val resource: Resource[IO, (org.http4s.server.Server, org.http4s.server.Server)] = { | ||
def resource(routes: HttpRoutes[IO], port: Int) = | ||
BlazeServerBuilder[IO] | ||
.bindHttp(port, config.host) | ||
.withHttpApp(routes.orNotFound) | ||
.resource | ||
def resource(dispatcher: Dispatcher[IO]): Resource[IO, (HttpServer, HttpServer)] = { | ||
def resource(dispatcher: Dispatcher[IO], endpoints: List[ServerEndpoint[Any with Fs2Streams[IO], IO]], port: Int) = { | ||
Resource.make( | ||
IO.delay { | ||
val vertx = Vertx.vertx() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should crate a single shared |
||
val server = vertx.createHttpServer() | ||
val router = Router.router(vertx) | ||
endpoints | ||
.map(endpoint => VertxCatsServerInterpreter[IO](serverOptions(dispatcher)).route(endpoint)) | ||
.foreach(attach => attach(router)) | ||
server.requestHandler(router).listen(port, config.host) | ||
}.flatMap(_.asF[IO]) | ||
)({ server => | ||
IO.delay(server.close).flatMap(_.asF[IO].void) | ||
}) | ||
} | ||
|
||
for { | ||
public <- resource(publicRoutes, config.port) | ||
admin <- resource(adminRoutes, config.adminPort) | ||
public <- resource(dispatcher, allPublicEndpoints, config.port) | ||
admin <- resource(dispatcher, allAdminEndpoints, config.adminPort) | ||
} yield (public, admin) | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current approach is based on Vert.x example
Alternatively
for
comprehension can be used but it is hard to tell if it is more readable ;)@adamw WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can make it more readable :)