Skip to content

Instantly share code, notes, and snippets.

View nomisRev's full-sized avatar
🏂

Simon Vergauwen nomisRev

🏂
View GitHub Profile
@nomisRev
nomisRev / RefreshToken.kt
Created June 7, 2025 17:59
Example showing Ktor OAuth2 usage as OAuth2 Flow for Compose Desktop
suspend fun refreshToken(): String? = withContext(Dispatchers.IO) {
val callback = CompletableDeferred<OAuth2>()
val server = embeddedServer(CIO, port = 0) {
val port = async { engine.resolvedConnectors().first().port }
authentication {
oauth("oauth") {
@OptIn(ExperimentalCoroutinesApi::class)
urlProvider = { "http://localhost:${port.getCompleted()}/callback" }
providerLookup = {
OAuthServerSettings.OAuth2ServerSettings(
@nomisRev
nomisRev / ValidationBuilder.kt
Created June 1, 2025 14:22
Simple validation DSL
package io.ktor.route.simple
import io.ktor.server.plugins.BadRequestException
import kotlin.reflect.KProperty1
class ValidationBuilder<T> {
private val validators = mutableListOf<Pair<KProperty1<T, *>, (value: Any?) -> String?>>()
fun validate(instance: T): List<String> =
@nomisRev
nomisRev / Ktor3.2EAP.md
Last active May 28, 2025 14:21
TL;DR documentation for Ktor 3.2.0 configuration

Ktor 3.2.0 Config EAP

Ktor 3.2.0 will add support for automatically deserializing configuration files into data classes. Let's define a simple application.yaml that defines some database configuration.

database:
    driverClassName: "$DB_DRIVER_CLASS_NAME:org.postgresql.Driver"
    host: "$DB_HOST:localhost"
    port: "$DATABASE_PORT:5432"
package org.jetbrains.ktor.sample
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.ktor.sample.config.dataSource
import org.jetbrains.ktor.sample.config.flyway
import org.junit.ClassRule
import org.junit.rules.ExternalResource
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@nomisRev
nomisRev / KGraphQLServer.kt
Created March 14, 2025 08:29
Ktor Koin KGraphQL
import com.apurebase.kgraphql.GraphQL
import com.apurebase.kgraphql.schema.Publisher
import com.apurebase.kgraphql.schema.dsl.SchemaBuilder
import io.ktor.server.application.*
import org.koin.dsl.module
import org.koin.ktor.ext.inject
import org.koin.ktor.plugin.Koin
import org.koin.logger.slf4jLogger
import kotlin.getValue
package io.github.nomisrev
import app.cash.sqldelight.driver.jdbc.asJdbcDriver
import arrow.continuations.SuspendApp
import arrow.continuations.ktor.server
import arrow.fx.coroutines.autoCloseable
import arrow.fx.coroutines.closeable
import arrow.fx.coroutines.resourceScope
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
@nomisRev
nomisRev / AutoClose.kt
Last active July 19, 2023 13:03
A Kotlin DSL for AutoCloseable
import arrow.atomic.Atomic
import arrow.atomic.update
/**
* AutoClose offers DSL style API for creating parent-child relationships of AutoCloseable dependencies
*/
interface AutoClose : AutoCloseable {
fun <A : AutoCloseable> autoClose(autoCloseable: A): A
}
@nomisRev
nomisRev / Example.kt
Created April 10, 2023 16:55
StackOverflow Question
import arrow.core.Nel
import arrow.core.raise.Raise
import arrow.core.raise.recover
import arrow.fx.coroutines.parZipOrAccumulate
object ApplicationError
object UserLegalId
object User
object DepartmentCode
object Department
@nomisRev
nomisRev / Racers.kt
Last active February 27, 2023 19:15
POC Race Design
sealed interface RaceRes<A, B>
data class First<A>(val first: A): RaceRes<A, Nothing>
data class FirstWithFailure<A>(val first: A, val second: Throwable): RaceRes<A, Nothing>
data class Second<B>(val second: B): RaceRes<Nothiing, B>
data class SecondWithFailure<B>(val first: Throwable, val second: B): RaceRes<Nothing, B>
/**
* raceSuccess({ req(0) }) { req(0) }).merge()
* What to do with exceptions?? RaceRes seems very clumsy.
*/