From 0bce7bd60aeb683515c1741c075f066947e14233 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 6 Oct 2024 12:25:42 +0600 Subject: [PATCH] fix of #488 --- CHANGELOG.md | 3 + .../KtorApplicationConfigurator.kt | 96 ++++++++++++++++++- 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a09fb554e17..8b172140a9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ * Add extension `withReplacedAt`/`withReplaced` ([#489](https://github.com/InsanusMokrassar/MicroUtils/issues/489)) * `Coroutines`: * Add extension `Flow.debouncedBy` +* `Ktor`: + * `Server`: + * Add `KtorApplicationConfigurator.Routing.Static` as solution for [#488](https://github.com/InsanusMokrassar/MicroUtils/issues/488) ## 0.22.4 diff --git a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/configurators/KtorApplicationConfigurator.kt b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/configurators/KtorApplicationConfigurator.kt index 424be129bdc..a75503b323a 100644 --- a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/configurators/KtorApplicationConfigurator.kt +++ b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/configurators/KtorApplicationConfigurator.kt @@ -1,7 +1,101 @@ package dev.inmo.micro_utils.ktor.server.configurators -import io.ktor.server.application.Application +import io.ktor.server.application.* +import io.ktor.server.http.content.* +import io.ktor.server.plugins.cachingheaders.* +import io.ktor.server.plugins.statuspages.* +import io.ktor.server.routing.* +import io.ktor.server.sessions.* +import kotlinx.serialization.Contextual +import kotlinx.serialization.Serializable +import java.io.File interface KtorApplicationConfigurator { + @Serializable + class Routing( + private val elements: List<@Contextual Element> + ) : KtorApplicationConfigurator { + fun interface Element { operator fun Route.invoke() } + private val rootInstaller = Element { + elements.forEach { + it.apply { invoke() } + } + } + + override fun Application.configure() { + pluginOrNull(io.ktor.server.routing.Routing) ?.apply { + rootInstaller.apply { invoke() } + } ?: install(io.ktor.server.routing.Routing) { + rootInstaller.apply { invoke() } + } + } + + /** + * @param pathToFolder Contains [Pair]s where firsts are paths in urls and seconds are folders file paths + * @param pathToResource Contains [Pair]s where firsts are paths in urls and seconds are packages in resources + */ + class Static( + private val pathToFolder: List> = emptyList(), + private val pathToResource: List> = emptyList(), + ) : Element { + override fun Route.invoke() { + pathToFolder.forEach { + staticFiles( + it.first, + File(it.second) + ) + } + pathToResource.forEach { + staticResources( + it.first, + it.second + ) + } + } + } + } + + class StatusPages( + private val elements: List<@Contextual Element> + ) : KtorApplicationConfigurator { + fun interface Element { operator fun StatusPagesConfig.invoke() } + + override fun Application.configure() { + install(StatusPages) { + elements.forEach { + it.apply { invoke() } + } + } + } + } + + class Sessions( + private val elements: List<@Contextual Element> + ) : KtorApplicationConfigurator { + fun interface Element { operator fun SessionsConfig.invoke() } + + override fun Application.configure() { + install(Sessions) { + elements.forEach { + it.apply { invoke() } + } + } + } + } + + class CachingHeaders( + private val elements: List<@Contextual Element> + ) : KtorApplicationConfigurator { + fun interface Element { operator fun CachingHeadersConfig.invoke() } + + override fun Application.configure() { + install(CachingHeaders) { + elements.forEach { + it.apply { invoke() } + } + } + } + } + fun Application.configure() }