https://kotlinlang.org logo
#kobweb
Title
# kobweb
f

Funyinoluwa Kashimawo

10/19/2023, 7:33 PM
I noticed that only the system env variables are available in the jvm module. How can I inject user env variables into the app?
d

David Herman

10/19/2023, 7:44 PM
The Kobweb block has a "globals" map which you can set from Gradle. So as long as you can get those env variables from your Gradle script, you can do something like
Copy code
kobweb {
  app {
     globals.put("version", "123")
  }
}
You can then reference those values from your code using the
AppGlobals
global. For the above case, you could get the version by writing
AppGlobals.getValue("version").toInt()
Does that work for you?
f

Funyinoluwa Kashimawo

10/19/2023, 8:09 PM
Thanks
So that would mean I would have to use a secret gradle file?
d

David Herman

10/19/2023, 8:09 PM
A secret Gradle file?
Gradle can read local env values
You should just be able to add this code to the build.gradle of your site project
f

Funyinoluwa Kashimawo

10/19/2023, 8:14 PM
Yeah because the gradle file also can't access the user env value only system env value
So the solution I am looking for is basically an env file that can either be inserted through the run command or pulled from system env when the app is deployed.
d

David Herman

10/19/2023, 8:42 PM
You can insert values into your gradle.properties file if that works for you
Does System.genenv not work?
f

Funyinoluwa Kashimawo

10/19/2023, 8:44 PM
System.getenv works but does not provide the values attached to the run command. Check the image sent earlier.
Yeah I think the gadle.properties option might work
f

Funyinoluwa Kashimawo

10/19/2023, 11:03 PM
Do the .properties did not really work and the app globals to didn't work because I needed the value in the jvm but the AppGlobals is only available in the js
I'll share how I resolved it shortly
I defined the variable in the local.properties file an made it available to the app using this gradle plugin https://github.com/gmazzo/gradle-buildconfig-plugin
In local.properties
MONGO_URI=
In project level build.gradle
Copy code
val properties = Properties()
val propertiesFile = project.rootProject.file("local.properties")
if (propertiesFile.exists()) {
    properties.load(propertiesFile.inputStream())
}

val mongoUri = properties.getProperty("MONGO_URI")

buildConfig {
    buildConfigField("String", "MONGO_URI", "\"${mongoUri}\"")
}
In code
Copy code
private val client = MongoClient.create(BuildConfig.MONGO_URI)
d

David Herman

10/19/2023, 11:14 PM
Great, thanks for sharing!
I'm glad another plugin could provide the necessary behavior
Note that I've used findProperty in some of my own build scripts
For example, I use it for signing: https://github.com/varabyte/kotter/blob/52b8a07cb28a8a17d32f09546d19b8c566e6a27b/kotter/build.gradle.kts#L80 I declare that value in a gradle.properties file for that project
f

Funyinoluwa Kashimawo

10/20/2023, 12:54 AM
wow do you mean the findProperty can also return values from the local.properties file?
d

David Herman

10/20/2023, 12:55 AM
I'm not sure. Worth a shot!
f

Funyinoluwa Kashimawo

10/20/2023, 12:55 AM
Update I did this instead of picking the values separately
Copy code
val properties = Properties()
val propertiesFile = project.rootProject.file("local.properties")
if (propertiesFile.exists()) {
    properties.load(propertiesFile.inputStream())
}
buildConfig {
    properties.forEach {
        if ("${it.key}" != "sdk.dir") {
            buildConfigField("String", it.key.toString(), "\"${it.value}\"")
        }
    }
}
d

David Herman

10/20/2023, 12:56 AM
https://github.com/gradle/gradle/issues/12283 reading this now, but it seems like they might not intentionally support local properties?
Yeah official guidance is using gradle.properties instead
f

Funyinoluwa Kashimawo

10/20/2023, 12:59 AM
Oh If it works for gradle.properties that's fine. I don't mind making that into a secret
2 Views