-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for middleware
@ApiInterceptor
This allows users to define a method on the server like: ``` @ApiInterceptor suspend fun interceptRequest( ctx: ApiInterceptorContext ) : Response { return when { ctx.path == "/legacy" -> ctx.dispatcher.dispatch("/new") else -> ctx.dispatcher.dispatch() } } ``` This should be able to allow users to define robust authentication support, as one example -- basically check the incoming path to see if it is protected, then pull information out of the request cookies to see if the user it authenticated, and if not, abort early with a 401 response. Bug #635
- Loading branch information
1 parent
e66bb22
commit 7dcf8a0
Showing
15 changed files
with
425 additions
and
74 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
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
36 changes: 36 additions & 0 deletions
36
backend/kobweb-api/src/main/kotlin/com/varabyte/kobweb/api/intercept/ApiInterceptor.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,36 @@ | ||
package com.varabyte.kobweb.api.intercept | ||
|
||
import com.varabyte.kobweb.api.http.Response | ||
|
||
/** | ||
* An annotation which tags a method that will be given a chance to process all incoming requests. | ||
* | ||
* The signature of the method being annotated must take a [ApiInterceptorContext] as its only parameter, and | ||
* return a [Response]. | ||
* | ||
* The "no-op" implementation of an interceptor that doesn't change the normal behavior of a Kobweb server is as | ||
* follows: | ||
* | ||
* ``` | ||
* @ApiInterceptor | ||
* suspend fun intercept(ctx: ApiInterceptorContext): Response { | ||
* return ctx.dispatcher.dispatch() | ||
* } | ||
* ``` | ||
* | ||
* but you can of course intercept specific API paths with the following pattern: | ||
* | ||
* ``` | ||
* @ApiInterceptor | ||
* suspend fun intercept(ctx: ApiInterceptorContext): Response { | ||
* if (ctx.path == "/example") { | ||
* return Response().apply { setBodyText("Intercepted!") } | ||
* } | ||
* return ctx.dispatcher.dispatch() | ||
* } | ||
* ``` | ||
* | ||
* This only works on intercepting API routes. API streams are not involved. | ||
*/ | ||
@Target(AnnotationTarget.FUNCTION) | ||
annotation class ApiInterceptor |
34 changes: 34 additions & 0 deletions
34
...end/kobweb-api/src/main/kotlin/com/varabyte/kobweb/api/intercept/ApiInterceptorContext.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,34 @@ | ||
package com.varabyte.kobweb.api.intercept | ||
|
||
import com.varabyte.kobweb.api.Apis | ||
import com.varabyte.kobweb.api.data.Data | ||
import com.varabyte.kobweb.api.env.Environment | ||
import com.varabyte.kobweb.api.http.MutableRequest | ||
import com.varabyte.kobweb.api.init.InitApi | ||
import com.varabyte.kobweb.api.init.InitApiContext | ||
import com.varabyte.kobweb.api.log.Logger | ||
|
||
/** | ||
* A context for a method annotated with [ApiInterceptor]. | ||
* | ||
* The classes can be used to help the user write interception logic, dispatching the request either to its original | ||
* endpoint, an alternate one, or simply returning a custom response instead entirely. See [ApiInterceptor] for more | ||
* information. | ||
* | ||
* @property env The current server environment, in case you need to branch logic in development vs production | ||
* environments. | ||
* @property path The path of the API endpoint being requested. | ||
* @property req Request information sent from the client. This instance of the request is mutable, meaning some fields | ||
* (headers, cookies, body, and content-type) can still be changed. | ||
* @property data Readonly data store potentially populated by methods annotated with [InitApi]. | ||
* See also: [InitApiContext]. | ||
* @property logger A logger which can be used to log messages into the log files. | ||
*/ | ||
class ApiInterceptorContext( | ||
val env: Environment, | ||
val dispatcher: Apis.Dispatcher, | ||
val path: String, | ||
val req: MutableRequest, | ||
val data: Data, | ||
val logger: Logger, | ||
) |
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
14 changes: 14 additions & 0 deletions
14
playground/site/src/jvmMain/kotlin/playground/api/Middleware.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,14 @@ | ||
package playground.api | ||
|
||
import com.varabyte.kobweb.api.http.Response | ||
import com.varabyte.kobweb.api.intercept.ApiInterceptor | ||
import com.varabyte.kobweb.api.intercept.ApiInterceptorContext | ||
|
||
// Visit http://localhost:8080/api/hello and then open .kobweb/server/logs/kobweb-server.log | ||
// to confirm that this interceptor was triggered. | ||
|
||
@ApiInterceptor | ||
suspend fun interceptRequest(ctx: ApiInterceptorContext): Response { | ||
ctx.logger.debug("Intercepting request for ${ctx.path}.") | ||
return ctx.dispatcher.dispatch() | ||
} |
Oops, something went wrong.