add configurators for ktor server

This commit is contained in:
InsanusMokrassar 2020-08-12 19:42:18 +06:00
parent 03e0728fdf
commit 46c84099be
7 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1,41 @@
buildscript {
repositories {
mavenLocal()
jcenter()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$gradle_bintray_plugin_version"
}
}
plugins {
id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version"
}
project.version = "$core_version"
project.group = "com.insanusmokrassar"
apply plugin: "java-library"
apply plugin: "kotlin"
repositories {
mavenLocal()
jcenter()
mavenCentral()
maven { url "https://kotlin.bintray.com/kotlinx" }
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
api projectByName("postssystem.core.ktor.common")
api projectByName("postssystem.ktor.server")
testImplementation "org.xerial:sqlite-jdbc:$test_sqlite_version"
testImplementation "org.jetbrains.kotlin:kotlin-test"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit"
}

View File

@ -0,0 +1,21 @@
package com.insanusmokrassar.postssystem.ktor.server.configurators
import io.ktor.application.Application
import io.ktor.application.install
import io.ktor.features.CachingHeaders
import kotlinx.serialization.ContextualSerialization
data class ApplicationCachingHeadersConfigurator(
private val elements: List<@ContextualSerialization Element>
) : KtorApplicationConfigurator{
interface Element { operator fun CachingHeaders.Configuration.invoke() }
override fun Application.configure() {
install(CachingHeaders) {
elements.forEach {
it.apply { invoke() }
}
}
}
}

View File

@ -0,0 +1,26 @@
package com.insanusmokrassar.postssystem.ktor.server.configurators
import io.ktor.application.*
import io.ktor.routing.Routing
import kotlinx.serialization.ContextualSerialization
import kotlinx.serialization.Serializable
@Serializable
class ApplicationRoutingConfigurator(
private val elements: List<@ContextualSerialization Element>
) : KtorApplicationConfigurator {
interface Element { operator fun Routing.invoke() }
override fun Application.configure() {
try {
feature(Routing)
} catch (e: IllegalStateException) {
install(Routing) {
elements.forEach {
it.apply { invoke() }
}
}
}
}
}

View File

@ -0,0 +1,20 @@
package com.insanusmokrassar.postssystem.ktor.server.configurators
import io.ktor.application.Application
import io.ktor.application.install
import io.ktor.sessions.Sessions
import kotlinx.serialization.ContextualSerialization
class ApplicationSessionsConfigurator(
private val elements: List<@ContextualSerialization Element>
) : KtorApplicationConfigurator {
interface Element { operator fun Sessions.Configuration.invoke() }
override fun Application.configure() {
install(Sessions) {
elements.forEach {
it.apply { invoke() }
}
}
}
}

View File

@ -0,0 +1,7 @@
package com.insanusmokrassar.postssystem.ktor.server.configurators
import io.ktor.application.Application
interface KtorApplicationConfigurator {
fun Application.configure()
}

View File

@ -0,0 +1,21 @@
package com.insanusmokrassar.postssystem.ktor.server.configurators
import io.ktor.application.Application
import io.ktor.application.install
import io.ktor.features.StatusPages
import kotlinx.serialization.ContextualSerialization
class StatusPagesConfigurator(
private val elements: List<@ContextualSerialization Element>
) : KtorApplicationConfigurator {
interface Element { operator fun StatusPages.Configuration.invoke() }
override fun Application.configure() {
install(StatusPages) {
elements.forEach {
it.apply { invoke() }
}
}
}
}

View File

@ -18,6 +18,7 @@ String[] includes = [
':core:ktor',
':core:ktor:common',
':core:ktor:client',
':core:ktor:server',
':publishing:api',
':publishing:exposed',