All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.kr.jadekim.jext.ktor.module.DefaultPluginsModule.kt Maven / Gradle / Ivy

The newest version!
package kr.jadekim.jext.ktor.module

import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.server.application.*
import io.ktor.server.plugins.autohead.*
import io.ktor.server.plugins.cachingheaders.*
import io.ktor.server.plugins.callid.*
import io.ktor.server.plugins.doublereceive.*
import io.ktor.server.plugins.forwardedheaders.*
import io.ktor.util.*
import io.ktor.util.date.*

private val ATTRIBUTE_CACHE_OPTION: AttributeKey = AttributeKey("CachingHeader.options")

fun ApplicationCall.cacheOption(option: CacheControl? = null, expires: GMTDate? = null) {
    attributes.put(ATTRIBUTE_CACHE_OPTION, CachingOptions(option, expires))
}

object DefaultFeatureModule : KtorModuleDefinition {

    class Configuration : KtorModuleConfiguration {
        var forHeaders: List = listOf(
            "CF-Connecting-IP",
            "CLIENT_IP",
            HttpHeaders.XForwardedFor,
        )

        var callIdConfig: CallIdConfig.() -> Unit = {
            retrieveFromHeader("traceparent")
        }
    }

    override fun createServerModule(config: Configuration): KtorServerModule = KtorServerModule {
        install(XForwardedHeaders) {
            forHeaders.clear()
            forHeaders.addAll(config.forHeaders)
        }

        install(DoubleReceive)

        install(AutoHeadResponse)

        install(CachingHeaders) {
            options { call, _ ->
                call.attributes.getOrNull(ATTRIBUTE_CACHE_OPTION)
            }
        }

        install(CallId, config.callIdConfig)
    }

    override fun createDefaultConfiguration(): Configuration = Configuration()
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy