Skip to content
This repository was archived by the owner on Aug 10, 2024. It is now read-only.

Commit 487d33f

Browse files
authored
add urlPathPrefix configuration parameter (#377)
1 parent e1b51c1 commit 487d33f

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed

api/kweb-core.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@ public abstract class kweb/config/KwebConfiguration {
917917
public fun getClientOfflineToastTextTemplate ()Ljava/lang/String;
918918
public abstract fun getClientStateStatsEnabled ()Z
919919
public abstract fun getClientStateTimeout ()Ljava/time/Duration;
920+
public fun getUrlPathPrefix ()Ljava/lang/String;
920921
public fun onWebsocketMessageHandlingFailure (Ljava/lang/Exception;)V
921922
public fun robotsTxt (Lio/ktor/server/application/ApplicationCall;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
922923
public final fun validate ()V

jitpack.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
jdk:
2+
- openjdk17

src/main/kotlin/kweb/Kweb.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ class Kweb private constructor(
223223
kwebConfig.faviconIco(call)
224224
}
225225

226-
get("/{visitedUrl...}") {
226+
get("${kwebConfig.urlPathPrefix}/{visitedUrl...}") {
227227
respondKweb(call, buildPage)
228228
}
229229

@@ -237,7 +237,7 @@ class Kweb private constructor(
237237
HtmlDocumentSupplier.createDocTemplate(plugins, application.routing { })
238238

239239
application.routing {
240-
webSocket("/ws") {
240+
webSocket("/kweb_ws") {
241241
listenForWebsocketConnection()
242242
}
243243
}
@@ -329,7 +329,7 @@ class Kweb private constructor(
329329
suspend fun respondKweb(call: ApplicationCall, buildPage: WebBrowser.() -> Unit) {
330330
val htmlDocument = HtmlDocumentSupplier.getTemplateCopy()
331331

332-
// The client prefix allows to monitor the ressource usage (#Sessions, State Size) per User
332+
// The client prefix allows to monitor the resource usage (#Sessions, State Size) per User
333333
val clientPrefix = determineClientPrefix(call)
334334
val kwebSessionId = clientPrefix + ":" + createNonce()
335335

src/main/kotlin/kweb/config/KwebConfiguration.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,27 @@ abstract class KwebConfiguration {
8686
*/
8787
open val clientOfflineToastTextTemplate: String = "Connection to server lost, attempting to reconnect"
8888

89+
/**
90+
* By default, Kweb will handle all paths under `/`, but this may not be desirable if you have other
91+
* Ktor route handlers. This function allows you to add a prefix to the Kweb route handler, e.g. `/my_kweb_site`,
92+
* so that only URLs under that path will be handled by Kweb.
93+
*
94+
* ```kotlin
95+
* cfg.urlPathPrefix = ""
96+
* path("/users/{userId}") { } // will be accessible at /users/1234
97+
*
98+
* cfg.urlPathPrefix = "/my_kweb_site"
99+
* path("/my_kweb_site/users/{userId}") { } // will be accessible at /my_kweb_site/users/1234
100+
*
101+
* cfg.urlPathPrefix = "/my_kweb_site"
102+
* path("/users/{userId}") { } // Will **NOT** be accessible
103+
* ```
104+
*
105+
* The path used for the WebSocket connection (`/kweb_ws`) and static assets (`/kweb_static/`) will be
106+
* unaffected by this setting.
107+
*/
108+
open val urlPathPrefix : String = ""
109+
89110
protected object Accessor {
90111
private val env = System.getenv()
91112

src/main/resources/kweb/kweb_bootstrap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function debugErr(debugToken, err, errMsg) {
9393
}
9494

9595
function connectWs() {
96-
var wsURL = toWSUrl("ws");
96+
var wsURL = toWSUrl("kweb_ws");
9797
console.debug("Establishing websocket connection", wsURL);
9898
socket = new WebSocket(wsURL);
9999
if (window.WebSocket === undefined) {

0 commit comments

Comments
 (0)