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

main.dev.neeffect.nee.ctx.web.WebContext.kt Maven / Gradle / Ivy

There is a newer version: 0.7.5
Show newest version
package dev.neeffect.nee.ctx.web

import dev.neeffect.nee.ANee
import dev.neeffect.nee.ctx.web.util.RenderHelper
import dev.neeffect.nee.effects.Out
import dev.neeffect.nee.effects.async.AsyncEnvWrapper
import dev.neeffect.nee.effects.async.AsyncSupport
import dev.neeffect.nee.effects.async.ExecutionContextProvider
import dev.neeffect.nee.effects.monitoring.TraceProvider
import dev.neeffect.nee.effects.monitoring.TraceResource
import dev.neeffect.nee.effects.security.SecurityProvider
import dev.neeffect.nee.effects.time.TimeProvider
import dev.neeffect.nee.effects.tx.TxConnection
import dev.neeffect.nee.effects.tx.TxProvider
import dev.neeffect.nee.effects.utils.Logging
import dev.neeffect.nee.security.User
import dev.neeffect.nee.security.UserRole
import io.ktor.application.ApplicationCall

data class WebContext>(
    private val jdbcProvider: TxProvider,
    private val securityProvider: SecurityProvider,
    private val executionContextProvider: ExecutionContextProvider,
    private val errorHandler: ErrorHandler = DefaultErrorHandler,
    private val contextProvider: WebContextProvider,
    private val traceProvider: TraceProvider<*>,
    private val timeProvider: TimeProvider,
    private val applicationCall: ApplicationCall,
    private val asyncEnv: AsyncEnvWrapper> = AsyncEnvWrapper()
) : TxProvider>,
    SecurityProvider by securityProvider,
    ExecutionContextProvider by executionContextProvider,
    TraceProvider>,
    TimeProvider by timeProvider,
    Logging,
    AsyncSupport> by asyncEnv {

    private val renderHelper = RenderHelper(contextProvider.jacksonMapper(), errorHandler)

    override fun getTrace(): TraceResource = traceProvider.getTrace()

    override fun setTrace(newState: TraceResource): WebContext =
        this.copy(traceProvider = traceProvider.setTrace(newState))

    override fun getConnection(): TxConnection = jdbcProvider.getConnection()

    override fun setConnectionState(newState: TxConnection) =
        this.copy(jdbcProvider = jdbcProvider.setConnectionState(newState))

    suspend fun serveText(businessFunction: ANee, String>) =
        businessFunction.perform(this).let { result ->
            renderHelper.serveText(applicationCall, result)
        }

    suspend fun  serveMessage(msg: Out): Unit =
        renderHelper.serveMessage(applicationCall, msg)

    suspend fun serveMessage(businessFunction: ANee, Any>) =
        serveMessage(businessFunction.perform(this))
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy