Skip to content

Commit

Permalink
#82 move fallback to Server class from Router because Server initiali…
Browse files Browse the repository at this point in the history
…zes NotFoundRoute
  • Loading branch information
angryziber committed Dec 13, 2024
1 parent 828ed8e commit c84f47d
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* json: TSGenerator will now use more type-safe string template types for java.time classes, e.g. `${number}-${number}-${number}` instead of `string`
* server: `Server(InetSocketAddress(0))` can now be used to bind to any available port.
* server: make idle restarts quick #96
* server: initialize `HttpExchange.pathParams` even no route is matched (404), for decorators #82
* jackson: serialize enums using their toString() method by default, this fixes `openapi` module usage with `jackson` #88
* liquibase: do not close jdbc connection if it was passed by user #81

Expand Down
4 changes: 2 additions & 2 deletions server/src/klite/Router.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ class Router(
private val log = logger()
val routes = mutableListOf<Route>() // TODO: use ExplicitBackingFields feature when it is more stable for immutable getter

internal fun route(exchange: HttpExchange): Pair<Route?, PathParams> {
internal fun route(exchange: HttpExchange): Pair<Route, PathParams>? {
val suffix = exchange.path.removePrefix(prefix)
return match(exchange.method, suffix)?.let {
it.first to PathParams(it.second.groups)
} ?: (null to PathParams.EMPTY)
}
}

private fun match(method: RequestMethod, path: String): Pair<Route, MatchResult>? {
Expand Down
2 changes: 1 addition & 1 deletion server/src/klite/Server.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Server(
val notFoundRoute = NotFoundRoute(prefix, notFoundHandler)
addContext(prefix, router) {
val r = router.route(this)
runHandler(this, r.first ?: notFoundRoute, r.second)
runHandler(this, r?.first ?: notFoundRoute, r?.second ?: PathParams.EMPTY)
}
router.block()
notFoundRoute.decoratedHandler = router.decorators.wrap(notFoundHandler)
Expand Down

0 comments on commit c84f47d

Please sign in to comment.