-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-40 Replace reactive plugin with a new routing API
- Loading branch information
Showing
11 changed files
with
158 additions
and
192 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
53 changes: 30 additions & 23 deletions
53
...routines/src/main/kotlin/io/javalin/community/routing/coroutines/ReactiveRoutingPlugin.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,46 @@ | ||
package io.javalin.community.routing.coroutines | ||
|
||
import io.javalin.Javalin | ||
import io.javalin.community.routing.Route | ||
import io.javalin.community.routing.coroutines.servlet.CoroutinesServlet | ||
import io.javalin.community.routing.registerRoute | ||
import io.javalin.community.routing.sortRoutes | ||
import io.javalin.config.JavalinConfig | ||
import io.javalin.http.Handler | ||
import io.javalin.plugin.Plugin | ||
import io.javalin.http.HandlerType | ||
import io.javalin.router.InternalRouter | ||
import io.javalin.router.RoutingApiInitializer | ||
import java.util.function.Consumer | ||
|
||
class ReactiveRoutingPlugin<ROUTE : ReactiveRoute<CONTEXT, RESPONSE>, CONTEXT, RESPONSE : Any>( | ||
private val servlet: CoroutinesServlet<CONTEXT, RESPONSE> | ||
) : Plugin { | ||
class CoroutinesRouting<CONTEXT, RESPONSE : Any> { | ||
internal val routes = mutableListOf<ReactiveRoute<CONTEXT, RESPONSE>>() | ||
|
||
private val routes = mutableListOf<ROUTE>() | ||
fun route(route: ReactiveRoute<CONTEXT, RESPONSE>) { | ||
routes.add(route) | ||
} | ||
|
||
override fun apply(app: Javalin) { | ||
routes | ||
.sortRoutes() | ||
.map { it to Handler { ctx -> servlet.handle(ctx, it) } } | ||
.forEach { (reactiveRoute, handler) -> app.registerRoute(reactiveRoute.method, reactiveRoute.path, handler) } | ||
fun route(method: Route, path: String, async: Boolean = true, handler: suspend CONTEXT.() -> RESPONSE) { | ||
routes.add(ReactiveRoute(path, method, async, handler)) | ||
} | ||
|
||
fun <ROUTES : ReactiveRoutes<ROUTE, CONTEXT, RESPONSE>> routing(vararg reactiveRoutes: ROUTES) { | ||
reactiveRoutes.forEach { | ||
routes.addAll(it.routes()) | ||
} | ||
fun routes(exampleEndpoint: ReactiveRoutes<ReactiveRoute<CONTEXT, RESPONSE>, CONTEXT, RESPONSE>): CoroutinesRouting<CONTEXT, RESPONSE> { | ||
exampleEndpoint.routes().forEach { route(it) } | ||
return this | ||
} | ||
|
||
} | ||
|
||
fun <ROUTE : ReactiveRoute<CONTEXT, RESPONSE>, ROUTES : ReactiveRoutes<ROUTE, CONTEXT, RESPONSE>, CONTEXT, RESPONSE : Any> JavalinConfig.reactiveRouting( | ||
servlet: CoroutinesServlet<CONTEXT, RESPONSE>, | ||
vararg routes: ROUTES | ||
) { | ||
val reactiveRoutingPlugin = ReactiveRoutingPlugin<ROUTE, CONTEXT, RESPONSE>(servlet) | ||
reactiveRoutingPlugin.routing(*routes) | ||
this.plugins.register(reactiveRoutingPlugin) | ||
class Coroutines<ROUTE : ReactiveRoute<CONTEXT, RESPONSE>, CONTEXT, RESPONSE : Any>( | ||
private val servlet: CoroutinesServlet<CONTEXT, RESPONSE>, | ||
) : RoutingApiInitializer<CoroutinesRouting<CONTEXT, RESPONSE>> { | ||
|
||
override fun initialize(cfg: JavalinConfig, internalRouter: InternalRouter, setup: Consumer<CoroutinesRouting<CONTEXT, RESPONSE>>) { | ||
val coroutinesRouting = CoroutinesRouting<CONTEXT, RESPONSE>() | ||
setup.accept(coroutinesRouting) | ||
|
||
coroutinesRouting | ||
.routes | ||
.sortRoutes() | ||
.map { it to Handler { ctx -> servlet.handle(ctx, it) } } | ||
.forEach { (route, handler) -> internalRouter.addHttpHandler(HandlerType.valueOf(route.method.toString()), route.path, handler) } | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.