-
-
Save ChristopherDavenport/3eb2cf06806fb8b5b5733d526f8b3039 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.example.http4s | |
import cats.data.OptionT | |
import cats.effect.Sync | |
import org.http4s.dsl.impl.Path | |
import org.http4s.headers.Allow | |
import org.http4s.{AuthedRoutes, HttpRoutes, Method, Response, Status} | |
object Route { | |
def of[F[_]: Sync](pf: PartialFunction[Path, Map[Method, HttpRoutes[F]]]): HttpRoutes[F] = HttpRoutes[F] { req => | |
val path = Path(req.pathInfo) | |
if (pf.isDefinedAt(path)) { | |
val methods = pf(path) | |
val route = methods.getOrElse( | |
req.method, | |
HttpRoutes.pure( | |
Response[F](Status.MethodNotAllowed).putHeaders(Allow(methods.keySet)) | |
) | |
) | |
route(req) | |
} else OptionT.none | |
} | |
def apply[F[_]: Sync](path: Path, methods: (Method, HttpRoutes[F])*): HttpRoutes[F] = | |
of { case `path` => methods.toMap } | |
} | |
object AuthedRoute { | |
def of[A, F[_]: Sync](pf: PartialFunction[Path, Map[Method, AuthedRoutes[A, F]]]): AuthedRoutes[A, F] = | |
AuthedRoutes[A, F] { authed => | |
val path = Path(authed.req.pathInfo) | |
if (pf.isDefinedAt(path)) { | |
val methods = pf(path) | |
val route: AuthedRoutes[A, F] = methods.getOrElse( | |
authed.req.method, | |
AuthedRoutes( | |
_ => OptionT.some[F](Response[F](Status.MethodNotAllowed).putHeaders(Allow(methods.keySet))) | |
) | |
) | |
route(authed) | |
} else OptionT.none | |
} | |
def apply[A, F[_]: Sync](path: Path, methods: (Method, AuthedRoutes[A, F])*): AuthedRoutes[A, F] = | |
of { case `path` => methods.toMap } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment