Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cats.Monad
- import cats.effect.{IOApp, IO, ExitCode, Sync, ContextShift, ConcurrentEffect, Timer}
- import fs2.Stream
- import io.circe.generic.auto._
- import org.http4s.HttpApp
- import org.http4s.server.Router
- import org.http4s.server.blaze.BlazeServerBuilder
- import org.http4s.server.middleware.Logger
- import sttp.tapir.json.circe._
- object Main extends IOApp {
- type Id = Long
- type Name = String
- type Status = Int
- type Token = String
- case class User(id: Id, name: Name)
- case class UserInfo(user: User, info: String)
- override def run(args: List[String]): IO[ExitCode] = {
- import cats.implicits._
- val app: HttpApp[IO] = createApp[IO]
- val loggedApp: HttpApp[IO] = Logger.httpApp(logHeaders = true, logBody = true)(app)
- val server: Stream[IO, ExitCode] = createServer(app = loggedApp)
- server.compile.drain.as(ExitCode.Success)
- }
- def createApp[F[_]: Sync: ContextShift]: HttpApp[F] = {
- import sttp.tapir._
- import sttp.tapir.server.http4s._
- import org.http4s.implicits._
- import cats.syntax.applicative.catsSyntaxApplicativeId
- import cats.syntax.either.catsSyntaxEitherId
- // GET localhost:8080/users/555/tommy?status=2
- val myEndpoint: Endpoint[(User, Status, Token), Unit, UserInfo, Nothing] = endpoint
- .get
- .in(("users" / path[Id]("id") / path[Name]("name")).mapTo(User))
- .in(query[Status]("status"))
- .in(header[Token]("X-Auth"))
- //.errorOutput(stringBody)
- .out(jsonBody[UserInfo])
- def logic(u: User, s: Status, t: Token): F[Either[Unit, UserInfo]] = implicitly[Monad[F]].pure {
- Right[Unit, UserInfo](UserInfo(u, s"Status is $s, token is $t"))
- }
- val route1 = myEndpoint.toRoutes((logic _).tupled)
- // val route2 = otherEndpoint.toRoutes(_ => "hello".asRight[Throwable].pure)
- val routes = route1 // <+> route2
- Router("/" -> routes).orNotFound
- }
- def createServer[F[_]: ConcurrentEffect: Timer](host: String = "0.0.0.0", port: Int = 8080, app: HttpApp[F]): Stream[F, ExitCode] = {
- BlazeServerBuilder[F].bindHttp(port, host).withHttpApp(app).serve
- }
- }
Add Comment
Please, Sign In to add comment