Skip to content

Commit

Permalink
Formatting and early backend
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed May 28, 2024
1 parent 806fa4a commit aabd1c9
Show file tree
Hide file tree
Showing 325 changed files with 6,695 additions and 5,577 deletions.
60 changes: 60 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,19 @@ kord = { module = "dev.kord:kord-core-voice", version.ref = "kord" }

kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8" }
ksp = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" }

ktor-logging = { module = "io.ktor:ktor-client-logging", version.ref = "ktor" }
ktor-server-core = { module = "io.ktor:ktor-server-core", version.ref = "ktor" }
ktor-server-netty = { module = "io.ktor:ktor-server-netty", version.ref = "ktor" }

ktor-server-plugin-content-negotiation = { module = "io.ktor:ktor-server-content-negotiation", version.ref = "ktor" }
ktor-server-plugin-cors = { module = "io.ktor:ktor-server-cors", version.ref = "ktor" }
ktor-server-plugin-forwarded-header = { module = "io.ktor:ktor-server-forwarded-header", version.ref = "ktor" }
ktor-server-plugin-websockets = { module = "io.ktor:ktor-server-websockets", version.ref = "ktor" }

ktor-server-plugin-kx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" }
ktor-server-plugin-kx-xml = { module = "io.ktor:ktor-serialization-kotlinx-xml", version.ref = "ktor" }

kx-coro = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kx-coro" }
kx-ser = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kx-ser" }
kx-ser-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kx-ser" }
Expand All @@ -72,3 +84,16 @@ toml = { module = "net.peanuuutz:tomlkt", version.ref = "toml" }
[bundles]
commons = ["commons-validator"]
logging = ["kotlin-logging", "slf4j"]

ktor-server = [
"ktor-server-core",
"ktor-server-netty",

"ktor-server-plugin-content-negotiation",
"ktor-server-plugin-cors",
"ktor-server-plugin-forwarded-header",
"ktor-server-plugin-websockets",

"ktor-server-plugin-kx-json",
"ktor-server-plugin-kx-xml",
]
4 changes: 3 additions & 1 deletion web/backend/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
plugins {
`kordex-module`
`published-module`
`disable-explicit-api-mode`

kotlin("plugin.serialization")
}
Expand All @@ -24,8 +23,11 @@ dependencies {

implementation(libs.bundles.logging)
implementation(libs.kotlin.stdlib)

implementation(libs.ktor.logging)

implementation(libs.bundles.ktor.server)

implementation(project(":kord-extensions"))
}

Expand Down
17 changes: 17 additions & 0 deletions web/backend/src/main/kotlin/dev/kordex/extra/web/WebExtension.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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 com.kotlindiscord.kord.extensions.extensions.Extension

public class WebExtension : Extension() {
override val name: String = "kordex-web"

override suspend fun setup() {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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.config

public sealed interface ForwardedHeaderMode {
public data object None : ForwardedHeaderMode
public data object Forwarded : ForwardedHeaderMode
public data object XForwarded : ForwardedHeaderMode
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.config

import io.ktor.server.plugins.*
import io.ktor.server.plugins.forwardedheaders.*

public sealed interface ForwardedHeaderStrategy {
public data object First : ForwardedHeaderStrategy
public data object Last : ForwardedHeaderStrategy

public data class SkipKnown(public val known: List<String>) : ForwardedHeaderStrategy
public data class SkipLast(public val number: Int) : ForwardedHeaderStrategy

public data class Custom(
public val block: (MutableOriginConnectionPoint, List<ForwardedHeaderValue>) -> Unit
) : ForwardedHeaderStrategy

public data class XCustom(
public val block: (MutableOriginConnectionPoint, XForwardedHeaderValues) -> Unit
) : ForwardedHeaderStrategy
}
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.config

public class WebServerConfig {
public var devMode: Boolean = false

@Suppress("MagicNumber")
public var port: Int = 8080

public var forwardedHeaderMode: ForwardedHeaderMode = ForwardedHeaderMode.None
public var forwardedHeaderStrategy: ForwardedHeaderStrategy = ForwardedHeaderStrategy.First

public lateinit var hostname: String
}
113 changes: 113 additions & 0 deletions web/backend/src/main/kotlin/dev/kordex/extra/web/server/WebServer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* 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.server

import dev.kordex.extra.web.config.ForwardedHeaderMode
import dev.kordex.extra.web.config.ForwardedHeaderStrategy
import dev.kordex.extra.web.config.WebServerConfig
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.serialization.kotlinx.xml.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.plugins.cors.routing.*
import io.ktor.server.plugins.forwardedheaders.*
import io.ktor.server.routing.*
import io.ktor.server.websocket.*
import kotlin.time.Duration.Companion.seconds

private val CORS_SCHEMES = listOf("http", "https", "ws", "wss")

public class WebServer(private val config: WebServerConfig) {
private val server = embeddedServer(Netty, port = config.port) {
install(ContentNegotiation) {
json()
xml()
}

install(CORS) {
allowHost(config.hostname, schemes = CORS_SCHEMES)

if (config.devMode) {
allowHost("127.0.0.1", schemes = CORS_SCHEMES)
allowHost("127.0.0.1:${config.port}", schemes = CORS_SCHEMES)

allowHost("localhost", schemes = CORS_SCHEMES)
allowHost("localhost:${config.port}", schemes = CORS_SCHEMES)
}

allowHeader(HttpHeaders.ContentType)

allowMethod(HttpMethod.Options)
allowMethod(HttpMethod.Put)
allowMethod(HttpMethod.Patch)
allowMethod(HttpMethod.Delete)
}

when (config.forwardedHeaderMode) {
ForwardedHeaderMode.None -> {}

ForwardedHeaderMode.Forwarded -> install(ForwardedHeaders) {
when (val s = config.forwardedHeaderStrategy) {
ForwardedHeaderStrategy.First -> useFirstValue()
ForwardedHeaderStrategy.Last -> useLastValue()

is ForwardedHeaderStrategy.SkipKnown -> skipKnownProxies(s.known)
is ForwardedHeaderStrategy.SkipLast -> skipLastProxies(s.number)

is ForwardedHeaderStrategy.Custom -> extractValue(s.block)

is ForwardedHeaderStrategy.XCustom -> error("Use the `Custom` strategy in `Forwarded` mode.")
}
}

ForwardedHeaderMode.XForwarded -> install(XForwardedHeaders) {
when (val s = config.forwardedHeaderStrategy) {
ForwardedHeaderStrategy.First -> useFirstProxy()
ForwardedHeaderStrategy.Last -> useLastProxy()

is ForwardedHeaderStrategy.SkipKnown -> skipKnownProxies(s.known)
is ForwardedHeaderStrategy.SkipLast -> skipLastProxies(s.number)

is ForwardedHeaderStrategy.XCustom -> extractEdgeProxy(s.block)

is ForwardedHeaderStrategy.Custom -> error("Use the `XCustom` strategy in `XForwarded` mode.")
}
}
}

install(WebSockets) {
// For some reason, ktor uses Java durations instead of Kotlin ones for the easy properties?
pingPeriodMillis = 15.seconds.inWholeMilliseconds
timeoutMillis = 15.seconds.inWholeMilliseconds
}

routing {
// TODO: Routing, incl. websockets
// TODO: Static files

setup()
}
}

public fun start() {
server.start(wait = false)
}

public fun stop() {
server.stop(
gracePeriodMillis = 0,
timeoutMillis = 0
)
}

private fun Routing.setup() {
TODO()
}
}
4 changes: 4 additions & 0 deletions web/frontend/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/dist
/node_modules
.eslintrc.js
/build
Loading

0 comments on commit aabd1c9

Please sign in to comment.