-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-35 Expose compatible with Java register method in the core module (R…
…esolve #35)
- Loading branch information
Showing
4 changed files
with
84 additions
and
32 deletions.
There are no files selected for viewing
31 changes: 0 additions & 31 deletions
31
routing-core/src/main/kotlin/io/javalin/community/routing/JavalinRoutes.kt
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
routing-core/src/main/kotlin/io/javalin/community/routing/JavalinRoutingExtensions.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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.javalin.community.routing | ||
|
||
import io.javalin.Javalin | ||
import io.javalin.http.Handler | ||
import io.javalin.security.RouteRole | ||
|
||
class JavalinRoutingExtensions(private val javalin: Javalin) { | ||
|
||
private val routes = mutableListOf<HandlerEntry>() | ||
|
||
fun addRouteHandler(vararg handlerEntry: HandlerEntry): JavalinRoutingExtensions = also { | ||
routes.addAll(handlerEntry) | ||
} | ||
|
||
@JvmOverloads | ||
fun addRoute(route: Route, path: String, vararg roles: RouteRole = emptyArray(), handler: Handler): JavalinRoutingExtensions = also { | ||
routes.add(HandlerEntry(route, path, handler, roles.toList())) | ||
} | ||
|
||
fun register(): Javalin { | ||
routes | ||
.sortRoutes() | ||
.forEach { javalin.registerRoute(it) } | ||
|
||
return javalin | ||
} | ||
|
||
} | ||
|
||
data class HandlerEntry @JvmOverloads constructor( | ||
val route: Route, | ||
override val path: String, | ||
val handler: Handler, | ||
val roles: List<RouteRole> = emptyList(), | ||
) : Routed | ||
|
||
fun Javalin.registerRoute(handlerEntry: HandlerEntry) = | ||
registerRoute(handlerEntry.route, handlerEntry.path, handlerEntry.handler, *handlerEntry.roles.toTypedArray()) | ||
|
||
fun Javalin.registerRoute(route: Route, path: String, handler: Handler, vararg roles: RouteRole) { | ||
when (route) { | ||
Route.HEAD -> head(path, handler, *roles) | ||
Route.PATCH -> patch(path, handler, *roles) | ||
Route.OPTIONS -> options(path, handler, *roles) | ||
Route.GET -> get(path, handler, *roles) | ||
Route.PUT -> put(path, handler, *roles) | ||
Route.POST -> post(path, handler, *roles) | ||
Route.DELETE -> delete(path, handler, *roles) | ||
Route.AFTER -> after(path, handler) | ||
Route.BEFORE -> before(path, handler) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
routing-core/src/main/kotlin/io/javalin/community/routing/Route.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.javalin.community.routing | ||
|
||
enum class Route(val isHttpMethod: Boolean = true) { | ||
HEAD, | ||
PATCH, | ||
OPTIONS, | ||
GET, | ||
PUT, | ||
POST, | ||
DELETE, | ||
AFTER(isHttpMethod = false), | ||
BEFORE(isHttpMethod = false), | ||
} |
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