rjaros / kvision

Object oriented web framework for Kotlin/JS
https://kvision.io
MIT License
1.2k stars 67 forks source link

Support Koin Annotations with Multiplatform #484

Closed lukelast closed 10 months ago

lukelast commented 12 months ago

It would be great to have Koin Annotations, even if just for the backend. https://insert-koin.io/docs/reference/koin-annotations/start

I wasn't able to get them working on this project: https://github.com/rjaros/kvision-examples/blob/master/template-fullstack-ktor-koin/build.gradle.kts

Do you think it's possible?

rjaros commented 12 months ago

I haven't tried it yet. I'll check it out and let you know in a few days, when I get back from vacation.

rjaros commented 11 months ago

It works fine for me. This is what you need to do:

  1. Add annotation dependency and new source directory to the backendMain sources set:
    val backendMain by getting {
    dependencies {
        // ...
        implementation("io.insert-koin:koin-annotations:1.2.2")
    }
    kotlin.srcDir("build/generated/ksp/backend/backendMain/kotlin")
    }
  2. At the end of the build.gradle.kts file add KSP configuration:
    
    dependencies {
    add("kspBackend", "io.insert-koin:koin-ksp-compiler:1.2.2")
    }

afterEvaluate { tasks { getByName("kspKotlinBackend").apply { dependsOn("kspCommonMainKotlinMetadata") } } }

3. Declare annotated module:
```kotlin
package com.example

import io.ktor.server.application.*
import io.ktor.server.plugins.compression.*
import io.ktor.server.routing.*
import io.kvision.remote.applyRoutes
import io.kvision.remote.getAllServiceManagers
import io.kvision.remote.kvisionInit
import org.koin.core.annotation.ComponentScan
import org.koin.core.annotation.Module
import org.koin.ksp.generated.module

@Module
@ComponentScan("com.example")
class PingModule

fun Application.main() {
    install(Compression)
    routing {
        getAllServiceManagers().forEach { applyRoutes(it) }
    }
    kvisionInit(PingModule().module)
}
  1. Add @Factory annotation to the service class:
    
    import org.koin.core.annotation.Factory

@Suppress("ACTUAL_WITHOUT_EXPECT") @Factory actual class PingService : IPingService {

override suspend fun ping(message: String): String {
    println(message)
    return "Hello world from server!"
}

}

lukelast commented 10 months ago

I'm not able to get it working on template-fullstack-ktor-koin

When trying to place the blok:

dependencies {
    add("kspBackend", "io.insert-koin:koin-ksp-compiler:1.2.2")
}

If I put it before jvm("backend") then I get the error Configuration with name 'kspBackend' not found.

If I put it after, I get the error Cannot change hierarchy of dependency configuration ':backendCompilationApi' after it has been included in dependency resolution.

Any ideas?

rjaros commented 10 months ago

You are right. I was doing my tests with a snapshot version of KVision, which uses new version of KSP. Unfortunately I'm also unable to make it work with current KVision. KVision 7 will be released in a few days and I'll add ktor-annotations template to the examples repository.

lukelast commented 10 months ago

Awesome thank you!

rjaros commented 10 months ago

See: https://github.com/rjaros/kvision-examples/tree/master/template-fullstack-ktor-koin-annotations