varabyte / kobweb

A modern framework for full stack web apps in Kotlin, built upon Compose HTML
https://kobweb.varabyte.com
Apache License 2.0
1.51k stars 66 forks source link

Include ktor Application.module() in kobweb backend api #235

Closed JamshedAlamQaderi closed 1 year ago

JamshedAlamQaderi commented 1 year ago

With this feature, developers would be able to include their existing Ktor server modules into the Kobweb backend API by annotating a function with @KtorModule and then calling their modules inside that function. This would make it easier for developers to migrate their existing server code to Kobweb while still being able to take advantage of Kobweb's frontend page routing.

Example:

// in the Ktor server module
fun Application.users() {
    // statements
}

fun Application.account() {
    // statements
}

// in the Kobweb server module
@KtorModule
fun Application.includeCustomModule() {
    users()
    account()
}

I hope this helps clarify your feature request, and please let me know if you have any further questions or concerns!

bitspittle commented 1 year ago

Basically, this request means we should allow people to register their own ktor code as plugins to Kobweb.

The user would like to

  1. define ktor code in a different module in their project
  2. build a jar
  3. register that jar with Kobweb somehow, telling it which functions to call

Alternately, another approach may be to extract Kobweb bits into their own separate libraries, so users can add Kobweb logic into their own ktor servers. This may be less complicated, although Kobweb is making a few opinionated choices in its own server structure that might make such a library fairly fragile (like, you need to set up cors, compression, and some json handshake policies or else things might not work, plus the whole api.jar thing).


@JamshedAlamQaderi FYI the more I think about this, I think it will be a pretty tricky feature to get right, plus it is the sort of feature that if I don't use regularly, I might easily break it as I develop other features.

Can you give me a very clear, concrete picture of what your project looks like right now? What did your ktor server do before you started using Kobweb? What sort of logic is in there that you don't want to give up? What is the final goal you're hoping to accomplish with your project? The more specifics you can share, the better.

Maybe we're chasing the wrong solution here. But I can't tell yet unless I know quite a bit more.

JamshedAlamQaderi commented 1 year ago

In simple term i want to include ktor module into kobweb api. there will be multiple submodules in the project. When i include ktor module into kobweb module gradle will handle the build operation to include modules into to the kobweb backend module. Then i won't need any jar file to include.

I hope you understand

bitspittle commented 1 year ago

I might be missing something.

Just because your code is there doesn't mean my code can add it automatically.

For example, here's my server code that configures Ktor: https://github.com/varabyte/kobweb/blob/a81ae8b317f8424a812b62e01b574d9a86d2431b/backend/server/src/main/kotlin/com/varabyte/kobweb/server/Application.kt#L86

How would I make sure that your code is additionally installed here? And how can I make sure that things are added in the right order?

Can you tell me very specifically what your project is trying to do? Because I might be able to offer or design alternate approaches that are a lot simpler (or maybe I'll better understand what you're asking for, I might be making some false assumptions).