-
-
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.
- Loading branch information
Showing
325 changed files
with
6,695 additions
and
5,577 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
.idea/modules/web/backend/kord-extensions.web.backend.main.iml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
17 changes: 17 additions & 0 deletions
17
web/backend/src/main/kotlin/dev/kordex/extra/web/WebExtension.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,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") | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
web/backend/src/main/kotlin/dev/kordex/extra/web/config/ForwardedHeaderMode.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,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 | ||
} |
26 changes: 26 additions & 0 deletions
26
web/backend/src/main/kotlin/dev/kordex/extra/web/config/ForwardedHeaderStrategy.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,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 | ||
} |
19 changes: 19 additions & 0 deletions
19
web/backend/src/main/kotlin/dev/kordex/extra/web/config/WebServerConfig.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.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
113
web/backend/src/main/kotlin/dev/kordex/extra/web/server/WebServer.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,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() | ||
} | ||
} |
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,4 @@ | ||
/dist | ||
/node_modules | ||
.eslintrc.js | ||
/build |
Oops, something went wrong.