diff --git a/README.md b/README.md index 08e01d8..ed47229 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,10 @@ Classes for the devices, to structure each state, Messages exchanged, as data aw | name | required | info | |------------------------|----------|-------------------------| | spring.profiles.active | * | Spring Boot environment | +| influxdb.bucket | | InfluxDB bucket | +| influxdb.org | | InfluxDB organization | +| influxdb.token | ** | InfluxDB access token | +| influxdb.url | * | InfluxDB host url | Required: * can be stored as text, and ** need to be stored as secret. @@ -18,7 +22,7 @@ Run `release.sh` script from `master` branch. ### Global gradle properties -To authenticate with Gradle to access repositories that require authentication, you can set your user and token in the `gradle.properties` file. +To authenticate with Gradle for services that require authentication, you can set your users and tokens in the `gradle.properties` file. Here's how you can do it: @@ -26,8 +30,17 @@ Here's how you can do it: - On Unix-like systems (Linux, macOS), this directory is typically `~/.gradle/`. - On Windows, this directory is typically `C:\Users\\.gradle\`. 2. Add the following lines to the `gradle.properties` file: - ```properties - repository.user=your_user - repository.token=your_token_value - ``` - or use environment variables `REPOSITORY_USER` and `REPOSITORY_TOKEN` + ```properties + repository.user=your_user + repository.token=your_token_value + + influxdb.token=your_token_value + ``` + or use environment variables `REPOSITORY_USER`, `REPOSITORY_TOKEN`, and `INFLUXDB_TOKEN` + +### InfluxDB configuration + +Create Bucket and Organization in InfluxDB (Local Development) + +Log in to InfluxDB (e.g., via the InfluxDB UI at http://localhost:8086). +Click Create Organization, in the field `Organization Name` enter `hlaeja_ltd`, and in field `Bucket Name` enter `device-data`. diff --git a/build.gradle.kts b/build.gradle.kts index a12f11f..d6aec8f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,4 +1,8 @@ +import com.bmuschko.gradle.docker.tasks.container.DockerCreateContainer +import java.lang.System.getenv + plugins { + alias(hlaeja.plugins.com.bmuschko.docker) alias(hlaeja.plugins.kotlin.jvm) alias(hlaeja.plugins.kotlin.spring) alias(hlaeja.plugins.ltd.hlaeja.plugin.service) @@ -7,6 +11,7 @@ plugins { } dependencies { + implementation(hlaeja.com.influxdb.client.kotlin) implementation(hlaeja.kotlin.reflect) implementation(hlaeja.kotlinx.coroutines) implementation(hlaeja.org.springframework.springboot.actuator.starter) @@ -22,3 +27,22 @@ dependencies { } group = "ltd.hlaeja" + +tasks { +} + +fun influxDbToken(): String = if (extra.has("influxdb.token")) { + extra["influxdb.token"] as String +} else { + getenv("INFLUXDB_TOKEN") ?: "missing_token" +} + +tasks { + named("containerCreate", DockerCreateContainer::class) { + withEnvVar("INFLUXDB_TOKEN", influxDbToken()) + } + withType { + filesMatching("**/application.yml") { filter { it.replace("%INFLUXDB_TOKEN%", influxDbToken()) } } + onlyIf { file("src/main/resources/application.yml").exists() } + } +} diff --git a/src/main/kotlin/ltd/hlaeja/configuration/InfluxDbConfiguration.kt b/src/main/kotlin/ltd/hlaeja/configuration/InfluxDbConfiguration.kt new file mode 100644 index 0000000..58c9f1e --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/configuration/InfluxDbConfiguration.kt @@ -0,0 +1,23 @@ +package ltd.hlaeja.configuration + +import com.influxdb.client.InfluxDBClient +import com.influxdb.client.InfluxDBClientFactory +import ltd.hlaeja.properties.InfluxDbProperties +import org.springframework.boot.context.properties.EnableConfigurationProperties +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration + +@Configuration +@EnableConfigurationProperties(InfluxDbProperties::class) +class InfluxDbConfiguration( + private val properties: InfluxDbProperties, +) { + + @Bean + fun influxDBClient(): InfluxDBClient = InfluxDBClientFactory.create( + properties.url, + properties.token.toCharArray(), + properties.org, + properties.bucket, + ) +} diff --git a/src/main/kotlin/ltd/hlaeja/properties/InfluxDbProperties.kt b/src/main/kotlin/ltd/hlaeja/properties/InfluxDbProperties.kt new file mode 100644 index 0000000..0b01e43 --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/properties/InfluxDbProperties.kt @@ -0,0 +1,11 @@ +package ltd.hlaeja.properties + +import org.springframework.boot.context.properties.ConfigurationProperties + +@ConfigurationProperties(prefix = "influxdb") +data class InfluxDbProperties( + val url: String, + val token: String, + val org: String, + val bucket: String, +) diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 4fc7d7a..9113a82 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -10,6 +10,10 @@ spring: name: "%APP_BUILD_OS_NAME%" version: "%APP_BUILD_OS_VERSION%" +influxdb: + bucket: device-data + org: hlaeja_ltd + --- ############################### ### Development environment ### @@ -19,6 +23,10 @@ spring: activate: on-profile: development +influxdb: + token: %INFLUXDB_TOKEN% + url: http://localhost:8086 + --- ########################## ### Docker environment ### @@ -28,6 +36,9 @@ spring: activate: on-profile: docker +influxdb: + url: http://InfluxDB:8086 + --- ############################## ### Production environment ###