dryvPacks

@dryv/ktor-backendv0.1.0

Ktor 3.6 routes with typed serializable models and coroutine service boundaries.

Ktor BackendBackendUnitPublished 7 days ago2 downloads

dryv pack add @dryv/ktor-backend
1.2 KBRaw ↗
jinja
package {{ pack.inputs.package_name }}

import io.ktor.serialization.kotlinx.json.json
import io.ktor.server.application.Application
import io.ktor.server.application.install
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import kotlinx.serialization.json.Json

class DryvBindingException(message: String, cause: Throwable? = null) : IllegalArgumentException(message, cause)

object DryvKtor {
    val json: Json = Json {
        ignoreUnknownKeys = false
        explicitNulls = true
    }

    fun required(raw: String?, name: String): String =
        raw ?: throw DryvBindingException("Missing required value '$name'")

    fun boolean(raw: String, name: String): Boolean =
        raw.toBooleanStrictOrNull()
            ?: throw DryvBindingException("Invalid boolean value for '$name'")

    fun long(raw: String, name: String): Long =
        raw.toLongOrNull()
            ?: throw DryvBindingException("Invalid integer value for '$name'")

    fun double(raw: String, name: String): Double =
        raw.toDoubleOrNull()
            ?: throw DryvBindingException("Invalid number value for '$name'")
}

fun Application.installDryvContentNegotiation(json: Json = DryvKtor.json) {
    install(ContentNegotiation) {
        json(json)
    }
}