Skip to content

Commit

Permalink
refactor: move router and related instances from BootStrap to `Modu…
Browse files Browse the repository at this point in the history
…les`
  • Loading branch information
yoshinorin committed Mar 20, 2024
1 parent 544c2a7 commit 17ef3a7
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 57 deletions.
55 changes: 1 addition & 54 deletions src/main/scala/net/yoshinorin/qualtet/BootStrap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,6 @@ import org.http4s.*
import org.http4s.ember.server.EmberServerBuilder
import org.http4s.server.Server
import com.comcast.ip4s.*
import net.yoshinorin.qualtet.http.{AuthProvider, CorsProvider}
import net.yoshinorin.qualtet.http.routes.HomeRoute
import net.yoshinorin.qualtet.http.routes.v1.{
ArchiveRoute => ArchiveRouteV1,
ArticleRoute => ArticleRouteV1,
AuthRoute => AuthRouteV1,
AuthorRoute => AuthorRouteV1,
CacheRoute => CacheRouteV1,
ContentRoute => ContentRouteV1,
ContentTypeRoute => ContentTypeRouteV1,
FeedRoute => FeedRouteV1,
SearchRoute => SearchRouteV1,
SeriesRoute => SeriesRouteV1,
SitemapRoute => SitemapRouteV1,
SystemRoute => SystemRouteV1,
TagRoute => TagRouteV1
}

import scala.concurrent.duration._

Expand All @@ -35,42 +18,6 @@ object BootStrap extends IOApp {

val logger: SelfAwareStructuredLogger[IO] = Log4CatsLoggerFactory[IO].getLoggerFromClass(this.getClass)

val authProvider = new AuthProvider(Modules.authService)
val corsProvider = new CorsProvider(Modules.config.cors)

val archiveRouteV1 = new ArchiveRouteV1(Modules.archiveService)
val articleRouteV1 = new ArticleRouteV1(Modules.articleService)
val authorRouteV1 = new AuthorRouteV1(Modules.authorService)
val authRouteV1 = new AuthRouteV1(Modules.authService)
val cacheRouteV1 = new CacheRouteV1(authProvider, Modules.cacheService)
val contentTypeRouteV1 = new ContentTypeRouteV1(Modules.contentTypeService)
val contentRouteV1 = new ContentRouteV1(authProvider, Modules.contentService)
val feedRouteV1 = new FeedRouteV1(Modules.feedService)
val homeRoute: HomeRoute = new HomeRoute()
val searchRouteV1 = new SearchRouteV1(Modules.searchService)
val seriesRouteV1 = new SeriesRouteV1(authProvider, Modules.seriesService)
val sitemapRouteV1 = new SitemapRouteV1(Modules.sitemapService)
val systemRouteV1 = new SystemRouteV1(Modules.config.http.endpoints.system)
val tagRouteV1 = new TagRouteV1(authProvider, Modules.tagService, Modules.articleService)

val router = new net.yoshinorin.qualtet.http.Router(
corsProvider,
archiveRouteV1,
articleRouteV1,
authorRouteV1,
authRouteV1,
cacheRouteV1,
contentRouteV1,
contentTypeRouteV1,
feedRouteV1,
homeRoute,
searchRouteV1,
seriesRouteV1,
sitemapRouteV1,
systemRouteV1,
tagRouteV1
)

private def server(host: Ipv4Address, port: Port, httpApp: HttpApp[IO]): Resource[IO, Server] = {
EmberServerBuilder
.default[IO]
Expand All @@ -89,7 +36,7 @@ object BootStrap extends IOApp {
(for {
_ <- logger.info(ApplicationInfo.asJson)
_ <- IO(Modules.migrator.migrate(Modules.contentTypeService))
routes <- router.withCors.map[Kleisli[IO, Request[IO], Response[IO]]](x => x.orNotFound)
routes <- Modules.router.withCors.map[Kleisli[IO, Request[IO], Response[IO]]](x => x.orNotFound)
httpApp <- IO(new HttpAppBuilder(routes).build)
server <- IO(
server(host, port, httpApp)
Expand Down
54 changes: 54 additions & 0 deletions src/main/scala/net/yoshinorin/qualtet/Modules.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ import net.yoshinorin.qualtet.auth.Signature
import net.yoshinorin.qualtet.domains.feeds.FeedService
import net.yoshinorin.qualtet.cache.CacheService
import net.yoshinorin.qualtet.domains.articles.ResponseArticleWithCount
import net.yoshinorin.qualtet.http.{AuthProvider, CorsProvider}
import net.yoshinorin.qualtet.http.routes.HomeRoute
import net.yoshinorin.qualtet.http.routes.v1.{
ArchiveRoute => ArchiveRouteV1,
ArticleRoute => ArticleRouteV1,
AuthRoute => AuthRouteV1,
AuthorRoute => AuthorRouteV1,
CacheRoute => CacheRouteV1,
ContentRoute => ContentRouteV1,
ContentTypeRoute => ContentTypeRouteV1,
FeedRoute => FeedRouteV1,
SearchRoute => SearchRouteV1,
SeriesRoute => SeriesRouteV1,
SitemapRoute => SitemapRouteV1,
SystemRoute => SystemRouteV1,
TagRoute => TagRouteV1
}
import net.yoshinorin.qualtet.infrastructure.db.Migrator
import net.yoshinorin.qualtet.infrastructure.db.doobie.DoobieTransactor

Expand Down Expand Up @@ -115,4 +132,41 @@ object Modules {
contentTypeService,
feedService
)

val authProvider = new AuthProvider(Modules.authService)
val corsProvider = new CorsProvider(Modules.config.cors)

val archiveRouteV1 = new ArchiveRouteV1(Modules.archiveService)
val articleRouteV1 = new ArticleRouteV1(Modules.articleService)
val authorRouteV1 = new AuthorRouteV1(Modules.authorService)
val authRouteV1 = new AuthRouteV1(Modules.authService)
val cacheRouteV1 = new CacheRouteV1(authProvider, Modules.cacheService)
val contentTypeRouteV1 = new ContentTypeRouteV1(Modules.contentTypeService)
val contentRouteV1 = new ContentRouteV1(authProvider, Modules.contentService)
val feedRouteV1 = new FeedRouteV1(Modules.feedService)
val homeRoute: HomeRoute = new HomeRoute()
val searchRouteV1 = new SearchRouteV1(Modules.searchService)
val seriesRouteV1 = new SeriesRouteV1(authProvider, Modules.seriesService)
val sitemapRouteV1 = new SitemapRouteV1(Modules.sitemapService)
val systemRouteV1 = new SystemRouteV1(Modules.config.http.endpoints.system)
val tagRouteV1 = new TagRouteV1(authProvider, Modules.tagService, Modules.articleService)

val router = new net.yoshinorin.qualtet.http.Router(
corsProvider,
archiveRouteV1,
articleRouteV1,
authorRouteV1,
authRouteV1,
cacheRouteV1,
contentRouteV1,
contentTypeRouteV1,
feedRouteV1,
homeRoute,
searchRouteV1,
seriesRouteV1,
sitemapRouteV1,
systemRouteV1,
tagRouteV1
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import net.yoshinorin.qualtet.domains.contents.{Content, ContentId, Path, Reques
import net.yoshinorin.qualtet.domains.robots.Attributes
import net.yoshinorin.qualtet.message.ProblemDetails
import net.yoshinorin.qualtet.fixture.Fixture.*
import net.yoshinorin.qualtet.fixture.Fixture.{router => fixtureRouter}
import net.yoshinorin.qualtet.Modules.*
import org.scalatest.wordspec.AnyWordSpec

Expand All @@ -25,7 +26,7 @@ class ContentRouteV1Spec extends AnyWordSpec {

val validAuthor: ResponseAuthor = authorService.findByName(author.name).unsafeRunSync().get
val validToken: String = authService.generateToken(RequestToken(validAuthor.id, "pass")).unsafeRunSync().token
val client: Client[IO] = Client.fromHttpApp(router.routes.orNotFound)
val client: Client[IO] = Client.fromHttpApp(fixtureRouter.routes.orNotFound)

"ContentRoute" should {
"create a content (HTTP Header Bearer is UpperCase)" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import net.yoshinorin.qualtet.domains.authors.ResponseAuthor
import net.yoshinorin.qualtet.domains.series.{RequestSeries, Series, SeriesName}
import net.yoshinorin.qualtet.message.ProblemDetails
import net.yoshinorin.qualtet.fixture.Fixture.*
import net.yoshinorin.qualtet.fixture.Fixture.{authProvider => fixtureAuthProvider}
import net.yoshinorin.qualtet.Modules.*
import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.BeforeAndAfterAll
Expand Down Expand Up @@ -48,7 +49,7 @@ class SeriesRouteV1Spec extends AnyWordSpec with BeforeAndAfterAll {

val validAuthor: ResponseAuthor = authorService.findByName(author.name).unsafeRunSync().get
val validToken: String = authService.generateToken(RequestToken(validAuthor.id, "pass")).unsafeRunSync().token
val seriesRouteV1 = new SeriesRoute(authProvider, seriesService)
val seriesRouteV1 = new SeriesRoute(fixtureAuthProvider, seriesService)
val client: Client[IO] = Client.fromHttpApp(makeRouter(seriesRouteV1 = seriesRouteV1).routes.orNotFound)

"SeriesRoute" should {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import net.yoshinorin.qualtet.domains.authors.ResponseAuthor
import net.yoshinorin.qualtet.domains.tags.{ResponseTag, TagId}
import net.yoshinorin.qualtet.message.ProblemDetails
import net.yoshinorin.qualtet.fixture.Fixture.*
import net.yoshinorin.qualtet.fixture.Fixture.{authProvider => fixtureAuthProvider}
import net.yoshinorin.qualtet.Modules.*
import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.BeforeAndAfterAll
Expand All @@ -35,7 +36,7 @@ class TagRouteV1Spec extends AnyWordSpec with BeforeAndAfterAll {

val validAuthor: ResponseAuthor = authorService.findByName(author.name).unsafeRunSync().get
val validToken: String = authService.generateToken(RequestToken(validAuthor.id, "pass")).unsafeRunSync().token
val tagRouteV1 = new TagRoute(authProvider, tagService, articleService)
val tagRouteV1 = new TagRoute(fixtureAuthProvider, tagService, articleService)
val client: Client[IO] = Client.fromHttpApp(makeRouter(tagRouteV1 = tagRouteV1).routes.orNotFound)

"TagRoute" should {
Expand Down

0 comments on commit 17ef3a7

Please sign in to comment.