Hi guys, how can I switch my different configurati...
# ktor
h
Hi guys, how can I switch my different configurations when running a Ktor application? I want to know both of in IntelliJ and command line.
g
Ktor I'm not sure about. Generally when I hear "how can I [do something] with gradle and..."7/10 times it isn't easily possible. That being said, here is what I got that might help; its a kotlin multiplatform project where the JVM uses ktor and compilations are central:
Copy code
kotlin {
    //Configure all Targets
    jvm("backend") {
        compilations {
            val main by getting
            create(environment) {
                tasks {
                    val backendJar by getting {
                        dependsOn("frontendBrowserWebpack")
                    }
                    register<JavaExec>("run") {
                        setMain("com.company.BackendAppKt")
                        //setClasspath(main.compileDependencyFiles + main.output.classesDirs + output.allOutputs + backendJar.outputs.files)
                        classpath = files("build/libs/FL-backend-1.0-SNAPSHOT.jar")
                        classpath = files("build/libs/FL-frontend-1.0-SNAPSHOT.jar")
                        classpath += main.compileDependencyFiles
                        classpath += backendJar.outputs.files
                        classpath += main.output.classesDirs
                        classpath += output.allOutputs
                        group = "application"
                    }
                }
            }
        }
    }

    // Configure all compilations of all targets:
    targets.all {
        compilations.all {
            kotlinOptions {
                allWarningsAsErrors = true
            }
        }
    }
}
You might need compilation.getBy[Name Or Path] for that...
j
@Hank, do you mean running different configurations both from intellij and commandline or switching them once running? If first, did you have a look at HOCON configuration in documentation? If second, I guess whatever it is not related with server config such as port/ip and so on, you can always encapsulate in code.
h
Sorry, I was not clearly. I have some variables that might be different depends on environments, like development, deployment etc. I want to know to how use application.conf to do that and how to switch the versions when I start the application (IntelliJ and command line). I’ve checked the HOCON configuration in documentation on Ktor official website but I still don’t know how to do. It will be great if there is an example code. Thank you for responding.
j
Here you can find an example: https://github.com/mathias21/KtorEasy
This is an API focused Ktor server example. But I think HOCON config will work the same for any kind of Ktor app.
h
Thank you for your example. I have a question.
Copy code
ktor {
    deployment {
            dev {
                host = "localhost"
                port = 3500
                databaseHost = "db"
                databasePort = "3306"
            }
            uat {
                host = "<http://myhost.com|myhost.com>"
                port = 3501
                databaseHost = "db"
                databasePort = "3306"
            }
            prod {
                host = "<http://myhost.com|myhost.com>"
                port = 3502
                databaseHost = "databasehost"
                databasePort = "3309"
            }
        }

        application {
            modules = [ com.batcuevasoft ]
        }
}
How can I switch dev, uat or prod block with command line? Using gradle will be great.
j
So, using gradle it is pretty simple. Create a launch configuration in IntelliJ like the following image:
In case you run it from Terminal, just launch gradle task run with environment variables like it is specified in the image. If you follow the code to extractConfig method, you will see how it works.
h
Wow, that’s awesome. I’ll try it late. Thanks a lot.
j
Welcome! good luck
👍 1