Skip to content

Commit

Permalink
refactor(http): delete MethodNotAllowedSupport trait
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshinorin committed Jun 25, 2024
1 parent 073a206 commit 113ec55
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 70 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ import org.http4s.headers.{Allow, `Content-Type`}
import org.http4s.{HttpRoutes, MediaType, Response}
import org.http4s.dsl.io.*
import net.yoshinorin.qualtet.domains.archives.ArchiveService
import net.yoshinorin.qualtet.http.MethodNotAllowedSupport
import net.yoshinorin.qualtet.syntax.*

class ArchiveRoute[F[_]: Monad](
archiveService: ArchiveService[F]
) extends MethodNotAllowedSupport[IO] {
) {

private[http] def index: HttpRoutes[IO] = HttpRoutes.of[IO] { r =>
implicit val x = r
(r match {
case request @ GET -> Root => this.get
case request @ OPTIONS -> Root => NoContent() // TODO: return `Allow Header`
case request @ _ =>
methodNotAllowed(request, Allow(Set(GET)))
case request @ _ => MethodNotAllowed(Allow(Set(GET)))
}).handleErrorWith(_.logWithStackTrace[IO].andResponse)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import org.http4s.headers.{Allow, `Content-Type`}
import org.http4s.{HttpRoutes, MediaType, Response}
import org.http4s.dsl.io.*
import net.yoshinorin.qualtet.domains.articles.ArticleService
import net.yoshinorin.qualtet.http.{ArticlesQueryParameter, MethodNotAllowedSupport}
import net.yoshinorin.qualtet.http.ArticlesQueryParameter
import net.yoshinorin.qualtet.syntax.*

class ArticleRoute[F[_]: Monad](
articleService: ArticleService[F]
) extends MethodNotAllowedSupport[IO] {
) {

private[http] def index: HttpRoutes[IO] = HttpRoutes.of[IO] { implicit r =>
(r match {
Expand All @@ -20,7 +20,7 @@ class ArticleRoute[F[_]: Monad](
this.get(q.page, q.limit)
case request @ OPTIONS -> Root => NoContent() // TODO: return `Allow Header`
case request @ _ =>
methodNotAllowed(request, Allow(Set(GET)))
MethodNotAllowed(Allow(Set(GET)))
}).handleErrorWith(_.logWithStackTrace[IO].andResponse)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import org.http4s.headers.{Allow, `Content-Type`, `WWW-Authenticate`}
import org.http4s.{Challenge, HttpRoutes, MediaType, Request, Response}
import org.http4s.dsl.io.*
import net.yoshinorin.qualtet.auth.{AuthService, RequestToken}
import net.yoshinorin.qualtet.http.{MethodNotAllowedSupport, RequestDecoder}
import net.yoshinorin.qualtet.http.RequestDecoder
import net.yoshinorin.qualtet.syntax.*

class AuthRoute[F[_]: Monad](authService: AuthService[F]) extends RequestDecoder with MethodNotAllowedSupport[IO] {
class AuthRoute[F[_]: Monad](authService: AuthService[F]) extends RequestDecoder {

private[http] def index: HttpRoutes[IO] = HttpRoutes.of[IO] { implicit r =>
(r match {
case request @ POST -> Root => this.post(request)
case request @ OPTIONS -> Root => NoContent() // TODO: return `Allow Header`
case request @ _ =>
methodNotAllowed(request, Allow(Set(POST)))
case request @ _ => MethodNotAllowed(Allow(Set(POST)))
}).handleErrorWith(_.logWithStackTrace[IO].andResponse)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ import org.http4s.{HttpRoutes, MediaType, Response}
import org.http4s.dsl.io.*
import net.yoshinorin.qualtet.domains.authors.{AuthorName, AuthorService}
import net.yoshinorin.qualtet.syntax.*
import net.yoshinorin.qualtet.http.MethodNotAllowedSupport

class AuthorRoute[F[_]: Monad](
authorService: AuthorService[F]
) extends MethodNotAllowedSupport[IO] {
) {

private[http] def index: HttpRoutes[IO] = HttpRoutes.of[IO] { implicit r =>
(r match {
case request @ GET -> Root => this.get
case request @ GET -> Root / authorName => this.get(authorName)
case request @ OPTIONS -> Root => NoContent() // TODO: return `Allow Header`
case request @ _ =>
methodNotAllowed(request, Allow(Set(GET)))
case request @ _ => MethodNotAllowed(Allow(Set(GET)))
}).handleErrorWith(_.logWithStackTrace[IO].andResponse)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ import org.http4s.dsl.io.*
import org.http4s.ContextRequest
import net.yoshinorin.qualtet.domains.authors.ResponseAuthor
import net.yoshinorin.qualtet.cache.CacheService
import net.yoshinorin.qualtet.http.{AuthProvider, MethodNotAllowedSupport}
import net.yoshinorin.qualtet.http.AuthProvider

class CacheRoute[F[_]: Monad](
authProvider: AuthProvider[F],
cacheService: CacheService[F]
) extends MethodNotAllowedSupport[IO] {
) {

private[http] def index: HttpRoutes[IO] = authProvider.authenticate(AuthedRoutes.of { ctxRequest =>
(ctxRequest match
case ContextRequest(_, r) =>
r match
case request @ DELETE -> Root => this.delete(ctxRequest.context._1)
case request @ _ =>
methodNotAllowed(r, Allow(Set(DELETE)))
case request @ _ => MethodNotAllowed(Allow(Set(DELETE)))
)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ import net.yoshinorin.qualtet.domains.authors.ResponseAuthor
import net.yoshinorin.qualtet.domains.contents.{ContentId, ContentService, Path, RequestContent}
import net.yoshinorin.qualtet.http.{AuthProvider, RequestDecoder}
import net.yoshinorin.qualtet.syntax.*
import net.yoshinorin.qualtet.http.MethodNotAllowedSupport

class ContentRoute[F[_]: Monad](
authProvider: AuthProvider[F],
contentService: ContentService[F]
) extends RequestDecoder
with MethodNotAllowedSupport[IO] {
) extends RequestDecoder {

private val logger = LoggerFactory.getLogger(this.getClass)

Expand Down Expand Up @@ -48,8 +46,7 @@ class ContentRoute[F[_]: Monad](
r match {
case request @ POST -> Root => this.post(ctxRequest.context)
case request @ DELETE -> Root / id => this.delete(id)
case request @ _ =>
methodNotAllowed(r, Allow(Set(GET, POST, DELETE)))
case request @ _ => MethodNotAllowed(Allow(Set(GET, POST, DELETE)))
}
}).handleErrorWith(_.logWithStackTrace[IO].andResponse)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ import org.http4s.headers.{Allow, `Content-Type`}
import org.http4s.{HttpRoutes, MediaType, Response}
import org.http4s.dsl.io.*
import net.yoshinorin.qualtet.domains.contentTypes.ContentTypeService
import net.yoshinorin.qualtet.http.MethodNotAllowedSupport

import net.yoshinorin.qualtet.syntax.*

class ContentTypeRoute[F[_]: Monad](
contentTypeService: ContentTypeService[F]
) extends MethodNotAllowedSupport[IO] {
) {

private[http] def index: HttpRoutes[IO] = HttpRoutes.of[IO] { implicit r =>
(r match {
case request @ GET -> Root => this.get
case request @ GET -> Root / name => this.get(name)
case request @ OPTIONS -> Root => NoContent() // TODO: return `Allow Header`
case request @ _ =>
methodNotAllowed(request, Allow(Set(GET)))
case request @ _ => MethodNotAllowed(Allow(Set(GET)))
}).handleErrorWith(_.logWithStackTrace[IO].andResponse)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ import org.http4s.dsl.io.*
import net.yoshinorin.qualtet.http.ArticlesQueryParameter
import net.yoshinorin.qualtet.domains.feeds.FeedService
import net.yoshinorin.qualtet.syntax.*
import net.yoshinorin.qualtet.http.MethodNotAllowedSupport

class FeedRoute[F[_]: Monad](
feedService: FeedService[F]
) extends MethodNotAllowedSupport[IO] {
) {

private[http] def index: HttpRoutes[IO] = HttpRoutes.of[IO] { implicit r =>
(r match {
case request @ GET -> Root / name => this.get(name)
case request @ OPTIONS -> Root => NoContent() // TODO: return `Allow Header`
case request @ _ =>
methodNotAllowed(request, Allow(Set(GET)))
case request @ _ => MethodNotAllowed(Allow(Set(GET)))
}).handleErrorWith(_.logWithStackTrace[IO].andResponse)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@ import org.http4s.dsl.io.*
import org.slf4j.LoggerFactory
import net.yoshinorin.qualtet.domains.search.SearchService
import net.yoshinorin.qualtet.syntax.*
import net.yoshinorin.qualtet.http.MethodNotAllowedSupport

class SearchRoute[F[_]: Monad](
searchService: SearchService[F]
) extends MethodNotAllowedSupport[IO] {
) {

private val logger = LoggerFactory.getLogger(this.getClass)

private[http] def index: HttpRoutes[IO] = HttpRoutes.of[IO] { implicit r =>
(r match {
case request @ GET -> _ => this.search(request.uri.query.multiParams)
case request @ OPTIONS -> Root => NoContent() // TODO: return `Allow Header`
case request @ _ =>
methodNotAllowed(request, Allow(Set(GET)))
case request @ _ => MethodNotAllowed(Allow(Set(GET)))
}).handleErrorWith(_.logWithStackTrace[IO].andResponse)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import org.http4s.dsl.io.*
import org.http4s.ContextRequest
import net.yoshinorin.qualtet.domains.authors.ResponseAuthor
import net.yoshinorin.qualtet.domains.series.{RequestSeries, Series, SeriesName, SeriesService}
import net.yoshinorin.qualtet.http.{AuthProvider, MethodNotAllowedSupport, RequestDecoder}
import net.yoshinorin.qualtet.http.{AuthProvider, RequestDecoder}
import net.yoshinorin.qualtet.syntax.*

class SeriesRoute[F[_]: Monad](
authProvider: AuthProvider[F],
seriesService: SeriesService[F]
) extends RequestDecoder
with MethodNotAllowedSupport[IO] {
) extends RequestDecoder {

// NOTE: must be compose `auth route` after `Non auth route`.
private[http] def index: HttpRoutes[IO] =
Expand All @@ -38,8 +37,7 @@ class SeriesRoute[F[_]: Monad](
case ContextRequest(_, r) =>
r match {
case request @ POST -> Root => this.post(ctxRequest.context)
case request @ _ =>
methodNotAllowed(r, Allow(Set(GET, POST, DELETE)))
case request @ _ => MethodNotAllowed(Allow(Set(GET, POST, DELETE)))
}
}).handleErrorWith(_.logWithStackTrace[IO].andResponse)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import org.http4s.{HttpRoutes, MediaType, Response}
import org.http4s.dsl.io.*
import net.yoshinorin.qualtet.domains.sitemaps.SitemapService
import net.yoshinorin.qualtet.syntax.*
import net.yoshinorin.qualtet.http.MethodNotAllowedSupport

class SitemapRoute[F[_]: Monad](sitemapService: SitemapService[F]) extends MethodNotAllowedSupport[IO] {
class SitemapRoute[F[_]: Monad](sitemapService: SitemapService[F]) {

private[http] def index: HttpRoutes[IO] = HttpRoutes.of[IO] { implicit r =>
(r match {
case request @ GET -> Root => this.get
case request @ OPTIONS -> Root => NoContent() // TODO: return `Allow Header`
case request @ _ =>
methodNotAllowed(request, Allow(Set(GET)))
case request @ _ => MethodNotAllowed(Allow(Set(GET)))
}).handleErrorWith(_.logWithStackTrace[IO].andResponse)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ import cats.effect.IO
import org.http4s.headers.{Allow, `Content-Type`}
import org.http4s.{HttpRoutes, MediaType, Response}
import org.http4s.dsl.io.*
import net.yoshinorin.qualtet.http.MethodNotAllowedSupport

import net.yoshinorin.qualtet.ApplicationInfo
import net.yoshinorin.qualtet.syntax.*
import net.yoshinorin.qualtet.config.HttpSystemEndpointConfig

class SystemRoute(config: HttpSystemEndpointConfig) extends MethodNotAllowedSupport[IO] {
class SystemRoute(config: HttpSystemEndpointConfig) {

private[http] def index: HttpRoutes[IO] = HttpRoutes.of[IO] { implicit r =>
(r match {
case request @ GET -> Root / "health" => this.health
case request @ GET -> Root / "metadata" =>
if config.metadata.enabled then this.metadata else NotFound()
case request @ OPTIONS -> Root => NoContent() // TODO: return `Allow Header`
case request @ _ =>
methodNotAllowed(request, Allow(Set(GET)))
case request @ _ => MethodNotAllowed(Allow(Set(GET)))
}).handleErrorWith(_.logWithStackTrace[IO].andResponse)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import org.http4s.ContextRequest
import net.yoshinorin.qualtet.domains.articles.ArticleService
import net.yoshinorin.qualtet.domains.authors.ResponseAuthor
import net.yoshinorin.qualtet.domains.tags.{TagId, TagName, TagService}
import net.yoshinorin.qualtet.http.{ArticlesQueryParameter, AuthProvider, MethodNotAllowedSupport}
import net.yoshinorin.qualtet.http.{ArticlesQueryParameter, AuthProvider}
import net.yoshinorin.qualtet.syntax.*

class TagRoute[F[_]: Monad](
authProvider: AuthProvider[F],
tagService: TagService[F],
articleService: ArticleService[F]
) extends MethodNotAllowedSupport[IO] {
) {

private val logger = LoggerFactory.getLogger(this.getClass)

Expand All @@ -44,8 +44,7 @@ class TagRoute[F[_]: Monad](
case ContextRequest(_, r) =>
r match {
case request @ DELETE -> Root / nameOrId => this.delete(nameOrId)
case request @ _ =>
methodNotAllowed(r, Allow(Set(GET, DELETE)))
case request @ _ => MethodNotAllowed(Allow(Set(GET, DELETE)))
}
}).handleErrorWith(_.logWithStackTrace[IO].andResponse)
}
Expand Down

0 comments on commit 113ec55

Please sign in to comment.