Skip to content

Commit

Permalink
Add dump request route
Browse files Browse the repository at this point in the history
  • Loading branch information
H1rono committed Feb 23, 2024
1 parent b381f91 commit 991eab4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/main/scala/h1rono/BotRoutes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cats.effect.Sync
import cats.implicits._
import org.http4s.HttpRoutes
import org.http4s.dsl.Http4sDsl
import org.http4s.Response

object BotRoutes {
def helloWorldRoutes[F[_]: Sync](H: HelloWorld[F]): HttpRoutes[F] = {
Expand All @@ -16,4 +17,14 @@ object BotRoutes {
} yield resp
}
}

def dumpRequestRoutes[F[_]: Sync](D: DumpReq[F]): HttpRoutes[F] = {
val dsl = new Http4sDsl[F] {}
import dsl._
HttpRoutes.of[F] { case req @ _ -> Root / "dump" =>
for {
status <- D.dump(DumpReq.Req(req))
} yield Response(status = status)
}
}
}
8 changes: 6 additions & 2 deletions src/main/scala/h1rono/BotServer.scala
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package h1rono

import cats.effect.Async
import cats.syntax.all._
import com.comcast.ip4s._
import fs2.io.net.Network
import org.http4s.ember.server.EmberServerBuilder
import org.http4s.implicits._
import cats.effect.std.Console

object BotServer {
def run[F[_]: Async: Network]: F[Nothing] = {
def run[F[_]: Async: Network: Console]: F[Nothing] = {
val helloWorldAlg = HelloWorld.impl[F]
val dumpReqAlg = DumpReq.impl[F]

// Combine Service Routes into an HttpApp.
// Can also be done via a Router if you
// want to extract segments not checked
// in the underlying routes.
val httpApp = (
BotRoutes.helloWorldRoutes[F](helloWorldAlg)
BotRoutes.helloWorldRoutes[F](helloWorldAlg) <+>
BotRoutes.dumpRequestRoutes[F](dumpReqAlg)
).orNotFound

for {
Expand Down
23 changes: 23 additions & 0 deletions src/main/scala/h1rono/DumpReq.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package h1rono

import org.http4s.Request
import cats.effect.std._
import cats.syntax.all._
import cats.effect.kernel.Async
import _root_.org.http4s.Status

trait DumpReq[F[_]] {
def dump(req: DumpReq.Req[F]): F[Status]
}

object DumpReq {
final case class Req[F[_]](req: Request[F]) extends AnyVal

def impl[F[_]: Async: Console]: DumpReq[F] = new DumpReq[F] {
def dump(req: Req[F]): F[Status] = for {
_ <- Console[F].println(req.req)
body <- req.req.as[String]
_ <- Console[F].println(body)
} yield Status.NoContent
}
}

0 comments on commit 991eab4

Please sign in to comment.