0% found this document useful (0 votes)
77 views2 pages

Advanced Scala

This document defines an HTTP API endpoint for requesting OTPs. It takes a UserService as a dependency and defines a POST route at /otp that accepts OtpRequest objects, calls the requestOtp method on the UserService, and returns a 201 Created response with a Location header pointing to the OTP resource. It also defines a Server object that wires this endpoint together with an in-memory repository to run an HTTP server on port 9000.

Uploaded by

Rajan Jaiprakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views2 pages

Advanced Scala

This document defines an HTTP API endpoint for requesting OTPs. It takes a UserService as a dependency and defines a POST route at /otp that accepts OtpRequest objects, calls the requestOtp method on the UserService, and returns a 201 Created response with a Location header pointing to the OTP resource. It also defines a Server object that wires this endpoint together with an in-memory repository to run an HTTP server on port 9000.

Uploaded by

Rajan Jaiprakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package driver

import cats.effect._
import cats.implicits._
import fruitfal.auth.domain.{OtpRequest, UserService}
import io.circe.generic.auto._
import org.http4s.circe._
import org.http4s.dsl.Http4sDsl
import org.http4s.{EntityDecoder, HttpRoutes, Uri, headers}

import scala.language.higherKinds

class OtpRequestEndpoint[F[_]: Sync] extends Http4sDsl[F] {

implicit val otpReqDecoder: EntityDecoder[F, OtpRequest] = jsonOf

private def OtpReqEndpoint(userService: UserService[F]): HttpRoutes[F] =


HttpRoutes.of[F] {
case req @ POST -> Root / "otp" =>
for {
otpRequest <- req.as[OtpRequest]
otp <- userService.requestOtp(otpRequest)
response <- Created(
headers.Location(Uri.unsafeFromString(s"/otp/${otp.id.get}")))
} yield response
}
}

object OtpRequestEndpoint {
def OtpReqEndpoint[F[_]: Effect](userService: UserService[F]): HttpRoutes[F] =
new OtpRequestEndpoint[F].OtpReqEndpoint(userService)
}

----------------------------------------------------------------------

package driver

import cats.effect._
import cats.implicits._
import fruitfal.auth.domain._
import fruitfal.auth.repository.inMemory._
import org.http4s.implicits._
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.server.middleware.Logger
import org.http4s.server.{Router, Server => H4Server}

object Server extends IOApp {


def createServer[F[_]: ContextShift: ConcurrentEffect: Timer]
: Resource[F, H4Server[F]] =
for {
otpRepo <- Resource.liftF(new MemOtpRepoInterpreter[F].pure[F])
userService = new UserService[F](otpRepo)
services = OtpRequestEndpoint.OtpReqEndpoint[F](userService)
httpApp = Router("/" -> services).orNotFound
httpAppWithLogger = Logger(false, true)(httpApp)
server <- BlazeServerBuilder[F]
.bindHttp(9000, "192.168.0.18")
.withHttpApp(httpAppWithLogger)
.resource
} yield server

def run(args: List[String]): IO[ExitCode] =


createServer.use(_ => IO.never).as(ExitCode.Success)
}

You might also like