Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/root' into root
Browse files Browse the repository at this point in the history
  • Loading branch information
weblate committed Jul 10, 2024
2 parents 4799ca1 + 2f92b01 commit 874711f
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 20 deletions.
50 changes: 50 additions & 0 deletions web/backend/src/main/kotlin/dev/kordex/extra/web/WebRegistries.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 dev.kordex.extra.web.pages.navigation.NavigationRegistry
import dev.kordex.extra.web.routes.RouteRegistry
import dev.kordex.extra.web.websockets.WebsocketRegistry

public class WebRegistries {
public lateinit var navigation: NavigationRegistry
private set

public lateinit var routes: RouteRegistry
private set

public lateinit var websockets: WebsocketRegistry
private set

public fun setup() {
if (!this::navigation.isInitialized) {
navigation = NavigationRegistry()
}

if (!this::routes.isInitialized) {
routes = RouteRegistry()
}

if (!this::websockets.isInitialized) {
websockets = WebsocketRegistry()
}
}

public suspend fun teardown() {
if (this::navigation.isInitialized) {
navigation.removeAll()
}

if (this::routes.isInitialized) {
routes.removeAll()
}

if (this::websockets.isInitialized) {
websockets.removeAll()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ExtensionNavigation(
public val extension: String,
public val icon: Identifier,

public val setup: () -> Unit
public val setup: ExtensionNavigation.() -> Unit
) {
public val navigation: MutableList<NavigationItem> = mutableListOf()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import kotlinx.serialization.Serializable
public data class NavigationItem(
public val name: String,
public val icon: Identifier,
public val page: String?,
public val page: String?, // TODO: Page objects?
public val children: MutableList<NavigationItem> = mutableListOf(),
) {
public class Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,23 @@
package dev.kordex.extra.web.pages.navigation

public class NavigationRegistry {
public val leftItems: MutableList<NavigationItem> = mutableListOf()
public val extensions: MutableMap<String, ExtensionNavigation> = mutableMapOf()

public fun add(navigation: ExtensionNavigation) {
if (navigation.extension in extensions) {
error("Navigation for ${navigation.extension} is already registered.")
}

extensions[navigation.extension] = navigation
}

public fun remove(navigation: ExtensionNavigation): ExtensionNavigation? =
remove(navigation.extension)

public fun remove(extension: String): ExtensionNavigation? =
extensions.remove(extension)

public fun removeAll() {
extensions.clear()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ package dev.kordex.extra.web.server

import com.kotlindiscord.kord.extensions.ExtensibleBot
import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent
import dev.kordex.extra.web.WebRegistries
import dev.kordex.extra.web.config.WebServerConfig
import dev.kordex.extra.web.events.WebServerStartEvent
import dev.kordex.extra.web.events.WebServerStopEvent
import dev.kordex.extra.web.routes.RouteRegistry
import dev.kordex.extra.web.websockets.WebsocketRegistry
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import kotlinx.coroutines.launch
Expand All @@ -21,8 +20,7 @@ import org.koin.core.component.inject
public class WebServer(internal val config: WebServerConfig) : KordExKoinComponent {
private val bot: ExtensibleBot by inject()

public val routeRegistry: RouteRegistry = RouteRegistry()
public val wsRegistry: WebsocketRegistry = WebsocketRegistry()
public val registries: WebRegistries = WebRegistries()

public var running: Boolean = false
private set
Expand Down Expand Up @@ -51,8 +49,7 @@ public class WebServer(internal val config: WebServerConfig) : KordExKoinCompone
}

public suspend fun stop() {
routeRegistry.removeAll()
wsRegistry.removeAll()
registries.teardown()

server.stop(
gracePeriodMillis = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,37 @@ public fun WebServer.configureRouting(app: Application, config: WebServerConfig)

route("/api/e/{path...}") {
delete {
routeRegistry.handle(Verb.DELETE, this)
registries.routes.handle(Verb.DELETE, this)
}

get {
routeRegistry.handle(Verb.GET, this)
registries.routes.handle(Verb.GET, this)
}

head {
routeRegistry.handle(Verb.HEAD, this)
registries.routes.handle(Verb.HEAD, this)
}

options {
routeRegistry.handle(Verb.OPTIONS, this)
registries.routes.handle(Verb.OPTIONS, this)
}

patch {
routeRegistry.handle(Verb.PATCH, this)
registries.routes.handle(Verb.PATCH, this)
}

post {
routeRegistry.handle(Verb.POST, this)
registries.routes.handle(Verb.POST, this)
}

put {
routeRegistry.handle(Verb.PUT, this)
registries.routes.handle(Verb.PUT, this)
}
}

route("/ws/e/{path...}") {
webSocket {
wsRegistry.handle(this)
registries.websockets.handle(this)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,30 @@ package dev.kordex.extra.web.utils

import com.kotlindiscord.kord.extensions.events.EventContext
import dev.kordex.extra.web.events.WebServerStartEvent
import dev.kordex.extra.web.pages.navigation.ExtensionNavigation
import dev.kordex.extra.web.routes.Route
import dev.kordex.extra.web.types.Identifier
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)) {
public fun EventContext<WebServerStartEvent>.apiRoute(route: Route) {
if (!event.server.registries.routes.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)) {
if (!event.server.registries.websockets.add(path, socketBuilder)) {
error("Websocket at $path for extension ${eventHandler.extension.name} already exists.")
}
}

public fun EventContext<WebServerStartEvent>.navigation(icon: Identifier, setup: ExtensionNavigation.() -> Unit) {
val navigation = ExtensionNavigation(eventHandler.extension.name, icon, setup)

navigation.setup()

// TODO: Register!
}

0 comments on commit 874711f

Please sign in to comment.