Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jaguililla committed Jan 24, 2024
1 parent bed7377 commit 354c693
Show file tree
Hide file tree
Showing 19 changed files with 50 additions and 48 deletions.
7 changes: 4 additions & 3 deletions backend/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.hexagonkt.logging.slf4j.jul.Slf4jJulLoggingAdapter
import com.hexagonkt.realworld.domain.model.Article
import com.hexagonkt.realworld.domain.model.Comment
import com.hexagonkt.realworld.domain.model.User
import com.hexagonkt.realworld.rest.RestApi
import com.hexagonkt.serialization.SerializationManager
import com.hexagonkt.serialization.jackson.json.Json
import com.hexagonkt.store.Store
Expand All @@ -14,20 +15,20 @@ import com.mongodb.client.model.IndexOptions
import com.mongodb.client.model.Indexes
import java.net.URI

val application by lazy {
val restApi by lazy {
val settings = Settings()
val jwt = createJwt(settings)
val userStore = createUserStore(settings)
val articleStore = createArticleStore(settings)

Application(settings, jwt, userStore, articleStore)
RestApi(settings, jwt, userStore, articleStore)
}

internal fun main() {
LoggingManager.adapter = Slf4jJulLoggingAdapter()
SerializationManager.defaultFormat = Json

application.start()
restApi.start()
}

internal fun createJwt(settings: Settings): Jwt =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.hexagonkt.realworld.rest.messages.ErrorResponseRoot
import com.hexagonkt.rest.SerializeResponseCallback
import kotlin.text.Charsets.UTF_8

internal data class Routes(
internal data class ApiController(
private val userRouter: HttpHandler,
private val usersRouter: HttpHandler,
private val profilesRouter: HttpHandler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME

internal data class ArticlesRouter(
internal data class ArticlesController(
private val jwt: Jwt,
private val users: Store<User, String>,
private val articles: Store<Article, String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.hexagonkt.realworld.rest.messages.CommentResponse
import com.hexagonkt.realworld.rest.messages.OkResponse
import com.hexagonkt.store.Store

internal data class CommentsRouter(
internal data class CommentsController(
private val jwt: Jwt,
private val users: Store<User, String>,
private val articles: Store<Article, String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.hexagonkt.realworld.rest.messages.ProfileResponseRoot
import com.hexagonkt.realworld.domain.model.User
import com.hexagonkt.store.Store

internal data class ProfilesRouter(
internal data class ProfilesController(
private val users: Store<User, String>,
private val contentType: ContentType,
private val authenticator: HttpHandler,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.hexagonkt.realworld
package com.hexagonkt.realworld.rest

import com.hexagonkt.core.media.APPLICATION_JSON
import com.hexagonkt.http.handlers.FilterHandler
import com.hexagonkt.http.handlers.HttpHandler
import com.hexagonkt.http.model.ContentType
import com.hexagonkt.http.server.*
import com.hexagonkt.http.server.netty.NettyServerAdapter
import com.hexagonkt.realworld.Jwt
import com.hexagonkt.realworld.Settings
import com.hexagonkt.realworld.domain.UsersService
import com.hexagonkt.realworld.rest.*
import com.hexagonkt.realworld.domain.model.Article
import com.hexagonkt.realworld.domain.model.User
import com.hexagonkt.store.Store
import kotlin.text.Charsets.UTF_8

data class Application(
data class RestApi(
private val settings: Settings = Settings(),
private val jwt: Jwt,
private val users: Store<User, String>,
Expand All @@ -30,13 +31,13 @@ data class Application(
private val serverSettings = HttpServerSettings(settings.bindAddress, settings.bindPort, "/api")
private val serverAdapter = NettyServerAdapter()
private val usersService = UsersService(users)
private val userRouter = UserRouter(jwt, usersService, contentType, authenticator)
private val usersRouter = UsersRouter(jwt, users, contentType)
private val profilesRouter = ProfilesRouter(users, contentType, authenticator)
private val commentsRouter = CommentsRouter(jwt, users, articles, contentType)
private val articlesRouter = ArticlesRouter(jwt, users, articles, contentType, authenticator, commentsRouter)
private val tagsRouter = TagsRouter(articles, contentType)
private val router: HttpHandler by lazy { Routes(userRouter, usersRouter, profilesRouter, articlesRouter, tagsRouter) }
private val userController = UserController(jwt, usersService, contentType, authenticator)
private val usersController = UsersController(jwt, users, contentType)
private val profilesController = ProfilesController(users, contentType, authenticator)
private val commentsController = CommentsController(jwt, users, articles, contentType)
private val articlesController = ArticlesController(jwt, users, articles, contentType, authenticator, commentsController)
private val tagsController = TagsController(articles, contentType)
private val router: HttpHandler by lazy { ApiController(userController, usersController, profilesController, articlesController, tagsController) }
internal val server: HttpServer by lazy { HttpServer(serverAdapter, router, serverSettings) }

fun start() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.hexagonkt.realworld.rest.messages.TagsResponseRoot
import com.hexagonkt.realworld.domain.model.Article
import com.hexagonkt.store.Store

internal data class TagsRouter(
internal data class TagsController(
private val articles: Store<Article, String>,
private val contentType: ContentType,
) : HttpController {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.hexagonkt.realworld.Jwt
import com.hexagonkt.realworld.domain.UsersService
import com.hexagonkt.rest.bodyMap

internal data class UserRouter(
internal data class UserController(
private val jwt: Jwt,
private val users: UsersService,
private val contentType: ContentType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.hexagonkt.realworld.rest.messages.*
import com.hexagonkt.rest.bodyMap
import com.hexagonkt.store.Store

internal data class UsersRouter(
internal data class UsersController(
private val jwt: Jwt,
private val users: Store<User, String>,
private val contentType: ContentType,
Expand Down
2 changes: 1 addition & 1 deletion backend/src/test/kotlin/ArchTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlin.test.Test
internal class ArchTest {

companion object {
private val APPLICATION_PACKAGE: String = Application::class.java.`package`.name
private val APPLICATION_PACKAGE: String = Settings::class.java.`package`.name
private val DOMAIN_PACKAGE: String = "$APPLICATION_PACKAGE.domain"
private val DOMAIN_MODEL_PACKAGE: String = "$DOMAIN_PACKAGE.model"
private val ADAPTER_PACKAGE: String = "$APPLICATION_PACKAGE.adapter"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.hexagonkt.realworld.rest.it

import com.hexagonkt.http.model.NOT_FOUND_404
import com.hexagonkt.realworld.RealWorldClient
import com.hexagonkt.realworld.application
import com.hexagonkt.realworld.restApi
import com.hexagonkt.realworld.domain.model.User
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable
Expand All @@ -13,7 +13,7 @@ import kotlin.test.assertEquals
* TODO Test bad requests (invalid JSON, bad field formats, etc.)
*/
@DisabledIfEnvironmentVariable(named = "DOCKER_BUILD", matches = "true")
internal class RoutesIT : ITBase() {
internal class ApiControllerIT : ITBase() {

private val jake = User(
username = "jake",
Expand All @@ -24,7 +24,7 @@ internal class RoutesIT : ITBase() {
)

@Test fun `Non existing route returns a 404`() {
val client = RealWorldClient("http://localhost:${application.server.runtimePort}/api")
val client = RealWorldClient("http://localhost:${restApi.server.runtimePort}/api")
val jakeClient = client.initializeUser(jake)

jakeClient.client.get("/404").apply { assertEquals(NOT_FOUND_404, status) }
Expand Down
10 changes: 5 additions & 5 deletions backend/src/test/kotlin/rest/it/ArticlesIT.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.hexagonkt.realworld.rest.it

import com.hexagonkt.realworld.RealWorldClient
import com.hexagonkt.realworld.application
import com.hexagonkt.realworld.restApi
import com.hexagonkt.realworld.rest.messages.PutArticleRequest
import com.hexagonkt.realworld.domain.model.Article
import com.hexagonkt.realworld.domain.model.User
Expand Down Expand Up @@ -48,7 +48,7 @@ internal class ArticlesIT : ITBase() {
)

@Test fun `Delete, create update and get an article`() {
val client = RealWorldClient("http://localhost:${application.server.runtimePort}/api")
val client = RealWorldClient("http://localhost:${restApi.server.runtimePort}/api")
val jakeClient = client.initializeUser(jake)

jakeClient.deleteArticle(trainDragon.slug)
Expand All @@ -59,7 +59,7 @@ internal class ArticlesIT : ITBase() {
}

@Test fun `Favorite and un-favorite articles`() {
val client = RealWorldClient("http://localhost:${application.server.runtimePort}/api")
val client = RealWorldClient("http://localhost:${restApi.server.runtimePort}/api")
val user = jake.username

val jakeClient = client.initializeUser(jake)
Expand All @@ -86,7 +86,7 @@ internal class ArticlesIT : ITBase() {
}

@Test fun `Find articles filters correctly`() {
val client = RealWorldClient("http://localhost:${application.server.runtimePort}/api")
val client = RealWorldClient("http://localhost:${restApi.server.runtimePort}/api")
val jakeClient = client.initializeUser(jake)
val janeClient = client.initializeUser(jane)

Expand Down Expand Up @@ -121,7 +121,7 @@ internal class ArticlesIT : ITBase() {
}

@Test fun `Get user feed`() {
val client = RealWorldClient("http://localhost:${application.server.runtimePort}/api")
val client = RealWorldClient("http://localhost:${restApi.server.runtimePort}/api")
val jakeClient = client.initializeUser(jake)
val janeClient = client.initializeUser(jane)

Expand Down
8 changes: 4 additions & 4 deletions backend/src/test/kotlin/rest/it/CommentsIT.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.hexagonkt.realworld.rest.it

import com.hexagonkt.realworld.RealWorldClient
import com.hexagonkt.realworld.application
import com.hexagonkt.realworld.restApi
import com.hexagonkt.realworld.rest.messages.CommentRequest
import com.hexagonkt.realworld.domain.model.Article
import com.hexagonkt.realworld.domain.model.User
Expand Down Expand Up @@ -30,7 +30,7 @@ internal class CommentsIT : ITBase() {
)

@Test fun `Delete, create and get article's comments`() {
val client = RealWorldClient("http://localhost:${application.server.runtimePort}/api")
val client = RealWorldClient("http://localhost:${restApi.server.runtimePort}/api")
val jakeClient = client.initializeUser(jake)

jakeClient.deleteArticle(trainDragon.slug)
Expand All @@ -42,7 +42,7 @@ internal class CommentsIT : ITBase() {
}

@Test fun `Get article's comments without login`() {
val client = RealWorldClient("http://localhost:${application.server.runtimePort}/api")
val client = RealWorldClient("http://localhost:${restApi.server.runtimePort}/api")
val jakeClient = client.initializeUser(jake)

jakeClient.deleteArticle(trainDragon.slug)
Expand All @@ -55,7 +55,7 @@ internal class CommentsIT : ITBase() {
}

@Test fun `Post comment to a not created article`() {
val client = RealWorldClient("http://localhost:${application.server.runtimePort}/api")
val client = RealWorldClient("http://localhost:${restApi.server.runtimePort}/api")
val jakeClient = client.initializeUser(jake)

jakeClient.createComment("non_existing_article", CommentRequest("Nice film"))
Expand Down
4 changes: 2 additions & 2 deletions backend/src/test/kotlin/rest/it/CorsIT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.hexagonkt.http.client.jetty.JettyClientAdapter
import com.hexagonkt.http.model.ContentType
import com.hexagonkt.http.model.Header
import com.hexagonkt.http.model.Headers
import com.hexagonkt.realworld.application
import com.hexagonkt.realworld.restApi
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable
import kotlin.test.assertEquals
Expand All @@ -17,7 +17,7 @@ import kotlin.test.assertEquals
internal class CorsIT : ITBase() {

@Test fun `OPTIONS returns correct CORS headers`() {
val baseUrl = urlOf("http://localhost:${application.server.runtimePort}/api")
val baseUrl = urlOf("http://localhost:${restApi.server.runtimePort}/api")
val settings = HttpClientSettings(baseUrl = baseUrl, contentType = ContentType(APPLICATION_JSON))
val client = HttpClient(JettyClientAdapter(), settings)
client.start()
Expand Down
4 changes: 2 additions & 2 deletions backend/src/test/kotlin/rest/it/ITBase.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.hexagonkt.realworld.rest.it

import com.hexagonkt.core.Jvm
import com.hexagonkt.realworld.application
import com.hexagonkt.realworld.restApi
import com.hexagonkt.realworld.main
import com.hexagonkt.serialization.SerializationManager
import com.hexagonkt.serialization.jackson.json.Json
Expand Down Expand Up @@ -36,6 +36,6 @@ internal open class ITBase {
}

@AfterAll fun shutdown() {
application.server.stop()
restApi.server.stop()
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.hexagonkt.realworld.rest.it

import com.hexagonkt.realworld.RealWorldClient
import com.hexagonkt.realworld.application
import com.hexagonkt.realworld.restApi
import com.hexagonkt.realworld.domain.model.User
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable
import java.net.URI

@DisabledIfEnvironmentVariable(named = "DOCKER_BUILD", matches = "true")
internal class ProfilesRouterIT : ITBase() {
internal class ProfilesControllerIT : ITBase() {

private val jake = User(
username = "jake",
Expand All @@ -27,7 +27,7 @@ internal class ProfilesRouterIT : ITBase() {
)

@Test fun `Follow and unfollow a profile`() {
val client = RealWorldClient("http://localhost:${application.server.runtimePort}/api")
val client = RealWorldClient("http://localhost:${restApi.server.runtimePort}/api")
val jakeClient = client.initializeUser(jake)
val janeClient = client.initializeUser(jane)

Expand Down
4 changes: 2 additions & 2 deletions backend/src/test/kotlin/rest/it/TagsIT.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.hexagonkt.realworld.rest.it

import com.hexagonkt.realworld.RealWorldClient
import com.hexagonkt.realworld.application
import com.hexagonkt.realworld.restApi
import com.hexagonkt.realworld.domain.model.Article
import com.hexagonkt.realworld.domain.model.User
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -38,7 +38,7 @@ internal class TagsIT : ITBase() {
)

@Test fun `Get all tags don't return duplicates`() {
val client = RealWorldClient("http://localhost:${application.server.runtimePort}/api")
val client = RealWorldClient("http://localhost:${restApi.server.runtimePort}/api")
val jakeClient = client.initializeUser(jake)

jakeClient.deleteArticle(trainDragon.slug)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.hexagonkt.core.requirePath
import com.hexagonkt.http.model.ContentType
import com.hexagonkt.http.model.UNAUTHORIZED_401
import com.hexagonkt.realworld.RealWorldClient
import com.hexagonkt.realworld.application
import com.hexagonkt.realworld.restApi
import com.hexagonkt.realworld.rest.messages.ErrorResponse
import com.hexagonkt.realworld.rest.messages.PutUserRequest
import com.hexagonkt.realworld.domain.model.User
Expand All @@ -16,7 +16,7 @@ import java.net.URI
import kotlin.test.assertEquals

@DisabledIfEnvironmentVariable(named = "DOCKER_BUILD", matches = "true")
internal class UserRouterIT : ITBase() {
internal class UserControllerIT : ITBase() {

private val jake = User(
username = "jake",
Expand All @@ -27,7 +27,7 @@ internal class UserRouterIT : ITBase() {
)

@Test fun `Get and update current user`() {
val client = RealWorldClient("http://localhost:${application.server.runtimePort}/api")
val client = RealWorldClient("http://localhost:${restApi.server.runtimePort}/api")
val jakeClient = client.initializeUser(jake)

jakeClient.getUser(jake)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.hexagonkt.realworld.rest.it
import com.hexagonkt.core.requirePath
import com.hexagonkt.http.model.INTERNAL_SERVER_ERROR_500
import com.hexagonkt.realworld.RealWorldClient
import com.hexagonkt.realworld.application
import com.hexagonkt.realworld.restApi
import com.hexagonkt.realworld.rest.messages.ErrorResponse
import com.hexagonkt.realworld.domain.model.User
import com.hexagonkt.rest.bodyMap
Expand All @@ -18,7 +18,7 @@ import kotlin.test.assertEquals
* - Login with bad password
*/
@DisabledIfEnvironmentVariable(named = "DOCKER_BUILD", matches = "true")
internal class UsersRouterIT : ITBase() {
internal class UsersControllerIT : ITBase() {

private val jake = User(
username = "jake",
Expand All @@ -29,7 +29,7 @@ internal class UsersRouterIT : ITBase() {
)

@Test fun `Delete, login and register users`() {
val client = RealWorldClient("http://localhost:${application.server.runtimePort}/api")
val client = RealWorldClient("http://localhost:${restApi.server.runtimePort}/api")

client.deleteUser(jake)
client.deleteUser(jake, setOf(404))
Expand Down

0 comments on commit 354c693

Please sign in to comment.