diff --git a/README.md b/README.md index 84e6911..81b9406 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Classes and endpoints, to shape and to steer, Devices and sensors, their purpose | server.ssl.key-store | * | HTTP Keystore | | server.ssl.key-store-type | * | HTTP Cert Type | | server.ssl.key-store-password | ** | HTTP Cert Pass | +| device-registry.url | * | Device Register URL | Required: * can be stored as text, and ** need to be stored as secret. diff --git a/build.gradle.kts b/build.gradle.kts index de442c3..b0ee77e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,8 +8,11 @@ plugins { } dependencies { + implementation(hlaeja.fasterxml.jackson.module.kotlin) + implementation(hlaeja.kotlin.logging) implementation(hlaeja.kotlin.reflect) implementation(hlaeja.kotlinx.coroutines) + implementation(hlaeja.library.hlaeja.common.messages) implementation(hlaeja.springboot.starter.actuator) implementation(hlaeja.springboot.starter.webflux) diff --git a/http/registry.http b/http/registry.http new file mode 100644 index 0000000..38ec46e --- /dev/null +++ b/http/registry.http @@ -0,0 +1,7 @@ +### register device for a type +POST {{hostname}}/register +Content-Type: application/json + +{ + "type": "00000000-0000-0000-0000-000000000000" +} diff --git a/src/main/kotlin/ltd/hlaeja/Application.kt b/src/main/kotlin/ltd/hlaeja/Application.kt index 42eea90..f9cb877 100644 --- a/src/main/kotlin/ltd/hlaeja/Application.kt +++ b/src/main/kotlin/ltd/hlaeja/Application.kt @@ -1,8 +1,13 @@ package ltd.hlaeja +import ltd.hlaeja.property.DeviceRegistryProperty import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.boot.runApplication +@EnableConfigurationProperties( + DeviceRegistryProperty::class, +) @SpringBootApplication class Application diff --git a/src/main/kotlin/ltd/hlaeja/controller/RegistryController.kt b/src/main/kotlin/ltd/hlaeja/controller/RegistryController.kt new file mode 100644 index 0000000..200d4c9 --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/controller/RegistryController.kt @@ -0,0 +1,18 @@ +package ltd.hlaeja.controller + +import ltd.hlaeja.library.deviceRegistry.Device +import ltd.hlaeja.service.DeviceRegistryService +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RestController + +@RestController +class RegistryController( + private val registryService: DeviceRegistryService, +) { + + @PostMapping("/register") + suspend fun addDevice( + @RequestBody request: Device.Request, + ): Device.Response = registryService.registerDevice(request) +} diff --git a/src/main/kotlin/ltd/hlaeja/property/DeviceRegistryProperty.kt b/src/main/kotlin/ltd/hlaeja/property/DeviceRegistryProperty.kt new file mode 100644 index 0000000..bee98ec --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/property/DeviceRegistryProperty.kt @@ -0,0 +1,8 @@ +package ltd.hlaeja.property + +import org.springframework.boot.context.properties.ConfigurationProperties + +@ConfigurationProperties(prefix = "device-registry") +data class DeviceRegistryProperty( + val url: String, +) diff --git a/src/main/kotlin/ltd/hlaeja/service/DeviceRegistryService.kt b/src/main/kotlin/ltd/hlaeja/service/DeviceRegistryService.kt new file mode 100644 index 0000000..806b5d5 --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/service/DeviceRegistryService.kt @@ -0,0 +1,24 @@ +package ltd.hlaeja.service + +import ltd.hlaeja.library.deviceRegistry.Device +import ltd.hlaeja.util.logCall +import ltd.hlaeja.property.DeviceRegistryProperty +import org.springframework.http.HttpStatus.REQUEST_TIMEOUT +import org.springframework.stereotype.Service +import org.springframework.web.reactive.function.client.WebClient +import org.springframework.web.reactive.function.client.awaitBodyOrNull +import org.springframework.web.server.ResponseStatusException + +@Service +class DeviceRegistryService( + private val webClient: WebClient, + private val deviceRegistryProperty: DeviceRegistryProperty, +) { + suspend fun registerDevice( + request: Device.Request, + ): Device.Response = webClient.post() + .uri("${deviceRegistryProperty.url}/device".also(::logCall)) + .bodyValue(request) + .retrieve() + .awaitBodyOrNull() ?: throw ResponseStatusException(REQUEST_TIMEOUT) +} diff --git a/src/main/kotlin/ltd/hlaeja/util/Helper.kt b/src/main/kotlin/ltd/hlaeja/util/Helper.kt new file mode 100644 index 0000000..f77be8e --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/util/Helper.kt @@ -0,0 +1,9 @@ +package ltd.hlaeja.util + +import mu.KotlinLogging + +private val log = KotlinLogging.logger {} + +fun logCall(url: String) { + log.debug("calling: {}", url) +} diff --git a/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 26fe8e1..0834e13 100644 --- a/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -19,6 +19,11 @@ "name": "spring.application.build.os.version", "type": "java.lang.String", "description": "Application build os version." + }, + { + "name": "device-registry.url", + "type": "java.lang.String", + "description": "Url for device registry service." } ] } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 7dad9d0..ae2d964 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -27,6 +27,9 @@ server: key-store-type: PKCS12 key-store-password: password +device-registry: + url: http://localhost:9010 + --- ########################## ### Docker environment ### @@ -44,6 +47,9 @@ server: key-store-type: PKCS12 key-store-password: password +device-registry: + url: http://DeviceRegistry:8080 + --- ############################## ### Production environment ###