Skip to content

Commit

Permalink
GH-40 Replace plugins with a new routing API v1
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Nov 10, 2023
1 parent 034e74f commit 4d0e808
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 95 deletions.
6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ subprojects {
compileOnly("io.javalin:javalin:$javalin")
testImplementation("io.javalin:javalin:$javalin")
testImplementation("io.javalin:javalin-testtools:$javalin")
kaptTest("io.javalin.community.openapi:openapi-annotation-processor:$javalin")
testImplementation("io.javalin.community.openapi:javalin-openapi-plugin:$javalin")

val openapi = "5.6.3"
kaptTest("io.javalin.community.openapi:openapi-annotation-processor:$openapi")
testImplementation("io.javalin.community.openapi:javalin-openapi-plugin:$openapi")

val jackson = "2.15.3"
testImplementation("com.fasterxml.jackson.core:jackson-databind:$jackson")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.text.Collator
import java.text.RuleBasedCollator
import java.util.Locale

fun <ROUTE : Routed> List<ROUTE>.sortRoutes(): List<ROUTE> =
fun <ROUTE : Routed> Collection<ROUTE>.sortRoutes(): List<ROUTE> =
sortedWith(RouteComparator())

open class RouteComparator : Comparator<Routed> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.javalin.community.routing.dsl

import io.javalin.community.routing.dsl.defaults.DefaultDsl
import io.javalin.community.routing.dsl.defaults.DefaultDsl.DefaultConfiguration
import io.javalin.community.routing.dsl.defaults.DefaultDsl.DefaultScope
import io.javalin.community.routing.dsl.defaults.DefaultRoute
import io.javalin.community.routing.sortRoutes
import io.javalin.config.JavalinConfig
import io.javalin.http.HandlerType
import io.javalin.router.InternalRouter
import io.javalin.router.RoutingApiInitializer
import java.util.function.Consumer

open class DslRouting<
CONFIG : RoutingDslConfiguration<ROUTE, CONTEXT, RESPONSE>,
ROUTE : DslRoute<CONTEXT, RESPONSE>,
CONTEXT,
RESPONSE : Any
>(
private val factory: RoutingDslFactory<CONFIG, ROUTE, CONTEXT, RESPONSE>
) : RoutingApiInitializer<CONFIG> {

companion object {
object Dsl : DslRouting<DefaultConfiguration, DefaultRoute, DefaultScope, Unit>(DefaultDsl)
}

override fun initialize(cfg: JavalinConfig, internalRouter: InternalRouter, setup: Consumer<CONFIG>) {
val dslConfig = factory.createConfiguration()
setup.accept(dslConfig)

dslConfig.routes
.sortRoutes()
.map { route -> route to factory.createHandler(route) }
.forEach { (route, handler) -> internalRouter.addHttpHandler(HandlerType.valueOf(route.method.toString()), route.path, handler) }

dslConfig.exceptionHandlers.forEach { (exceptionClass, handler) ->
internalRouter.addHttpExceptionHandler(exceptionClass.java, factory.createExceptionHandler(handler))
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.javalin.community.routing.dsl

import io.javalin.Javalin
import io.javalin.community.routing.Route
import io.javalin.community.routing.dsl.DslRouting.Companion.Dsl
import io.javalin.community.routing.dsl.defaults.Path
import io.javalin.community.routing.dsl.specification.TestSpecification
import io.javalin.testtools.JavalinTest
Expand All @@ -18,8 +19,8 @@ class InPlaceRoutingDslTest : TestSpecification() {
fun `each http dsl method is properly mapped to javalin handler`() = JavalinTest.test(
// given: a javalin app with routes defined using the dsl
Javalin.create { config ->
config.routing {
before("/before") { header("test", "before") }
config.router.mount(Dsl) {
it.before("/before") { header("test", "before") }
after("/after") { header("test", "after") }

get("/throwing") { throw RuntimeException() }
Expand All @@ -37,7 +38,8 @@ class InPlaceRoutingDslTest : TestSpecification() {
defaultConfig
) { _, client ->
// when: a request is made to the http route
Route.values()
Route.entries
.asSequence()
.filter { it.isHttpMethod }
.map { it.name.lowercase() }
.map { it to request(it, "${client.origin}/$it").asEmpty() }
Expand Down

0 comments on commit 4d0e808

Please sign in to comment.