Skip to content

Commit

Permalink
Introduce ServerEndpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Mar 25, 2019
1 parent b569b34 commit 12b931a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
3 changes: 3 additions & 0 deletions core/src/main/scala/tapir/Endpoint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tapir

import tapir.EndpointInput.RequestMethod
import tapir.model.Method
import tapir.server.ServerEndpoint
import tapir.typelevel.{FnComponents, ParamConcat, ParamsAsArgs}

/**
Expand Down Expand Up @@ -70,6 +71,8 @@ case class Endpoint[I, E, O, +S](input: EndpointInput[I], errorOutput: EndpointO
def show: String = {
s"Endpoint${info.name.map("[" + _ + "]").getOrElse("")}(in: ${input.show}, errout: ${errorOutput.show}, out: ${output.show})"
}

def serverLogic[F[_]](f: I => F[Either[E, O]]): ServerEndpoint[I, E, O, S, F] = ServerEndpoint(this, f)
}

case class EndpointInfo(name: Option[String], summary: Option[String], description: Option[String], tags: Vector[String]) {
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/scala/tapir/server/ServerEndpoint.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package tapir.server
import tapir.Endpoint

case class ServerEndpoint[I, E, O, +S, F[_]](endpoint: Endpoint[I, E, O, S], logic: I => F[Either[E, O]])
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tapir.server.akkahttp

import akka.http.scaladsl.server.{Directive, Route}
import tapir.Endpoint
import tapir.server.ServerEndpoint
import tapir.typelevel.{ParamsToTuple, ReplaceFirstInTuple}

import scala.concurrent.{ExecutionContext, Future}
Expand Down Expand Up @@ -31,6 +32,14 @@ trait TapirAkkaHttpServer {
}
}

implicit class RichAkkaHttpServerEndpoint[I, E, O](se: ServerEndpoint[I, E, O, AkkaStream, Future]) {
def toDirective[T](implicit paramsToTuple: ParamsToTuple.Aux[I, T], akkaHttpOptions: AkkaHttpServerOptions): Directive[T] =
new EndpointToAkkaServer(akkaHttpOptions).toDirective(se.endpoint)

def toRoute(implicit serverOptions: AkkaHttpServerOptions): Route =
new EndpointToAkkaServer(serverOptions).toRoute(se.endpoint)(se.logic)
}

implicit class RichToFutureFunction[T, U](a: T => Future[U])(implicit ec: ExecutionContext) {
def andThenFirst[U_TUPLE, T_TUPLE, O](l: U_TUPLE => Future[O])(
implicit replaceFirst: ReplaceFirstInTuple[T, U, T_TUPLE, U_TUPLE]): T_TUPLE => Future[O] = { tTuple =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package tapir.server.http4s

import cats.Monad
import cats.implicits._
import cats.effect.{ContextShift, Sync}
import cats.implicits._
import org.http4s.{EntityBody, HttpRoutes}
import tapir.Endpoint
import tapir.internal.{ParamsToSeq, SeqToParams}
import tapir.typelevel.{ParamsAsArgs, ReplaceFirstInFn, ReplaceFirstInTuple}
import tapir.server.ServerEndpoint
import tapir.typelevel.ReplaceFirstInTuple

import scala.reflect.ClassTag

Expand All @@ -32,6 +32,11 @@ trait TapirHttp4sServer {
}
}

implicit class RichAkkaHttpServerEndpoint[I, E, O, F[_]](se: ServerEndpoint[I, E, O, EntityBody[F], F]) {
def toRoute(implicit serverOptions: Http4sServerOptions[F], fs: Sync[F], fcs: ContextShift[F]): HttpRoutes[F] =
new EndpointToHttp4sServer(serverOptions).toRoutes(se.endpoint)(se.logic)
}

implicit class RichToMonadFunction[T, U, F[_]: Monad](a: T => F[U]) {
def andThenFirst[U_TUPLE, T_TUPLE, O](l: U_TUPLE => F[O])(
implicit replaceFirst: ReplaceFirstInTuple[T, U, T_TUPLE, U_TUPLE]): T_TUPLE => F[O] = { tTuple =>
Expand Down

0 comments on commit 12b931a

Please sign in to comment.