-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backend: Start/stop logic, events and initial API surface
- Loading branch information
Showing
12 changed files
with
482 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.kordex.extra.web | ||
|
||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.response.* | ||
|
||
@Suppress("StringLiteralDuplication") | ||
public abstract class Route(public val extension: String) { | ||
public abstract val path: String | ||
|
||
public open suspend fun delete(call: ApplicationCall) { | ||
call.response.header("Content-Type", "application/json") | ||
call.respond(HttpStatusCode.MethodNotAllowed, mutableMapOf("error" to "Method not allowed")) | ||
} | ||
|
||
public open suspend fun get(call: ApplicationCall) { | ||
call.response.header("Content-Type", "application/json") | ||
call.respond(HttpStatusCode.MethodNotAllowed, mutableMapOf("error" to "Method not allowed")) | ||
} | ||
|
||
public open suspend fun head(call: ApplicationCall) { | ||
call.response.header("Content-Type", "application/json") | ||
call.respond(HttpStatusCode.MethodNotAllowed, mutableMapOf("error" to "Method not allowed")) | ||
} | ||
|
||
public open suspend fun options(call: ApplicationCall) { | ||
call.response.header("Content-Type", "application/json") | ||
call.respond(HttpStatusCode.MethodNotAllowed, mutableMapOf("error" to "Method not allowed")) | ||
} | ||
|
||
public open suspend fun patch(call: ApplicationCall) { | ||
call.response.header("Content-Type", "application/json") | ||
call.respond(HttpStatusCode.MethodNotAllowed, mutableMapOf("error" to "Method not allowed")) | ||
} | ||
|
||
public open suspend fun post(call: ApplicationCall) { | ||
call.response.header("Content-Type", "application/json") | ||
call.respond(HttpStatusCode.MethodNotAllowed, mutableMapOf("error" to "Method not allowed")) | ||
} | ||
|
||
public open suspend fun put(call: ApplicationCall) { | ||
call.response.header("Content-Type", "application/json") | ||
call.respond(HttpStatusCode.MethodNotAllowed, mutableMapOf("error" to "Method not allowed")) | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
web/backend/src/main/kotlin/dev/kordex/extra/web/events/WebServerStartEvent.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,12 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.kordex.extra.web.events | ||
|
||
import com.kotlindiscord.kord.extensions.events.KordExEvent | ||
import dev.kordex.extra.web.server.WebServer | ||
|
||
public class WebServerStartEvent(public val server: WebServer) : KordExEvent |
11 changes: 11 additions & 0 deletions
11
web/backend/src/main/kotlin/dev/kordex/extra/web/events/WebServerStopEvent.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,11 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.kordex.extra.web.events | ||
|
||
import com.kotlindiscord.kord.extensions.events.KordExEvent | ||
|
||
public class WebServerStopEvent : KordExEvent |
70 changes: 70 additions & 0 deletions
70
web/backend/src/main/kotlin/dev/kordex/extra/web/routes/RouteRegistry.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,70 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.kordex.extra.web.routes | ||
|
||
import com.kotlindiscord.kord.extensions.events.ExtensionStateEvent | ||
import com.kotlindiscord.kord.extensions.extensions.ExtensionState | ||
import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent | ||
import dev.kordex.extra.web.Route | ||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.response.* | ||
import io.ktor.util.pipeline.* | ||
|
||
public class RouteRegistry : KordExKoinComponent { | ||
private val routes: MutableMap<String, Route> = mutableMapOf() | ||
|
||
public suspend fun handle(verb: Verb, context: PipelineContext<Unit, ApplicationCall>) { | ||
val call = context.call | ||
|
||
val path = call.parameters.getAll("path") | ||
?.joinToString("/") | ||
|
||
val route = routes[path] | ||
?: return call.respond(HttpStatusCode.NotFound) | ||
|
||
when (verb) { | ||
Verb.DELETE -> route.delete(call) | ||
Verb.GET -> route.get(call) | ||
Verb.HEAD -> route.head(call) | ||
Verb.OPTIONS -> route.options(call) | ||
Verb.PATCH -> route.patch(call) | ||
Verb.POST -> route.post(call) | ||
Verb.PUT -> route.put(call) | ||
} | ||
} | ||
|
||
public fun handleExtensionState(event: ExtensionStateEvent) { | ||
if (event.state == ExtensionState.UNLOADING) { | ||
val toRemove = routes.filter { it.value.extension == event.extension.name } | ||
|
||
toRemove.forEach { routes.remove(it.key) } | ||
} | ||
} | ||
|
||
public fun add(route: Route): Boolean { | ||
val path = "${route.extension}/${route.path}" | ||
|
||
if (path in routes) { | ||
return false | ||
} | ||
|
||
routes[path] = route | ||
|
||
return true | ||
} | ||
|
||
public fun remove(route: Route): Route? { | ||
val path = "${route.extension}/${route.path}" | ||
|
||
return routes.remove(path) | ||
} | ||
|
||
public fun removeAll() { | ||
routes.clear() | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
web/backend/src/main/kotlin/dev/kordex/extra/web/routes/Verb.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,19 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.kordex.extra.web.routes | ||
|
||
import io.ktor.http.* | ||
|
||
public sealed class Verb(public val method: HttpMethod) { | ||
public data object DELETE : Verb(HttpMethod.Delete) | ||
public data object GET : Verb(HttpMethod.Get) | ||
public data object HEAD : Verb(HttpMethod.Head) | ||
public data object OPTIONS : Verb(HttpMethod.Options) | ||
public data object PATCH : Verb(HttpMethod.Patch) | ||
public data object POST : Verb(HttpMethod.Post) | ||
public data object PUT : Verb(HttpMethod.Put) | ||
} |
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
19 changes: 19 additions & 0 deletions
19
web/backend/src/main/kotlin/dev/kordex/extra/web/utils/_Builder.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,19 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.kordex.extra.web.utils | ||
|
||
import com.kotlindiscord.kord.extensions.builders.ExtensibleBotBuilder | ||
import dev.kordex.extra.web.WebExtension | ||
import dev.kordex.extra.web.config.WebServerConfig | ||
|
||
public fun ExtensibleBotBuilder.ExtensionsBuilder.web(builder: WebServerConfig.() -> Unit) { | ||
val config = WebServerConfig() | ||
|
||
builder(config) | ||
|
||
add { WebExtension(config) } | ||
} |
27 changes: 27 additions & 0 deletions
27
web/backend/src/main/kotlin/dev/kordex/extra/web/utils/_Extension.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,27 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.kordex.extra.web.utils | ||
|
||
import com.kotlindiscord.kord.extensions.events.EventContext | ||
import dev.kordex.extra.web.Route | ||
import dev.kordex.extra.web.events.WebServerStartEvent | ||
import dev.kordex.extra.web.websockets.WebsocketBuilder | ||
import dev.kordex.extra.web.websockets.WebsocketBuilderFun | ||
|
||
public fun EventContext<WebServerStartEvent>.route(route: Route) { | ||
if (!event.server.routeRegistry.add(route)) { | ||
error("Route at ${route.path} for extension ${eventHandler.extension.name} already exists.") | ||
} | ||
} | ||
|
||
public fun EventContext<WebServerStartEvent>.websocket(path: String, body: WebsocketBuilderFun) { | ||
val socketBuilder = WebsocketBuilder(eventHandler.extension.name, body) | ||
|
||
if (!event.server.wsRegistry.add(path, socketBuilder)) { | ||
error("Websocket at $path for extension ${eventHandler.extension.name} already exists.") | ||
} | ||
} |
Oops, something went wrong.