Refactor Ping service tests and introduce PingProperties configuration for cleaner service name handling
This commit is contained in:
+11
@@ -0,0 +1,11 @@
|
||||
package at.mocode.ping.infrastructure
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "app")
|
||||
data class PingProperties(
|
||||
var serviceName: String = "ping-service",
|
||||
var serviceNameFallback: String = "ping-service-fallback"
|
||||
)
|
||||
+7
-11
@@ -2,6 +2,7 @@ package at.mocode.ping.infrastructure.web
|
||||
|
||||
import at.mocode.ping.api.*
|
||||
import at.mocode.ping.application.PingUseCase
|
||||
import at.mocode.ping.infrastructure.PingProperties
|
||||
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.security.access.prepost.PreAuthorize
|
||||
@@ -18,10 +19,10 @@ import kotlin.uuid.ExperimentalUuidApi
|
||||
* Nutzt den Application Port (PingUseCase).
|
||||
*/
|
||||
@RestController
|
||||
// @CrossOrigin Annotation entfernt. CORS wird zentral im API-Gateway gehandhabt.
|
||||
@OptIn(ExperimentalUuidApi::class)
|
||||
class PingController(
|
||||
private val pingUseCase: PingUseCase
|
||||
private val pingUseCase: PingUseCase,
|
||||
private val properties: PingProperties
|
||||
) : PingApi {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(PingController::class.java)
|
||||
@@ -54,7 +55,7 @@ class PingController(
|
||||
return EnhancedPingResponse(
|
||||
status = "pong",
|
||||
timestamp = domainPing.timestamp.atOffset(ZoneOffset.UTC).format(formatter),
|
||||
service = "ping-service",
|
||||
service = properties.serviceName,
|
||||
circuitBreakerState = "CLOSED",
|
||||
responseTime = elapsedMs
|
||||
)
|
||||
@@ -93,7 +94,7 @@ class PingController(
|
||||
private fun createResponse(domainPing: at.mocode.ping.domain.Ping, status: String) = PingResponse(
|
||||
status = status,
|
||||
timestamp = domainPing.timestamp.atOffset(ZoneOffset.UTC).format(formatter),
|
||||
service = "ping-service"
|
||||
service = properties.serviceName
|
||||
)
|
||||
|
||||
// Fallback
|
||||
@@ -103,7 +104,7 @@ class PingController(
|
||||
return EnhancedPingResponse(
|
||||
status = "fallback",
|
||||
timestamp = java.time.OffsetDateTime.now().format(formatter),
|
||||
service = "ping-service-fallback",
|
||||
service = properties.serviceNameFallback,
|
||||
circuitBreakerState = "OPEN",
|
||||
responseTime = 0
|
||||
)
|
||||
@@ -114,13 +115,8 @@ class PingController(
|
||||
return HealthResponse(
|
||||
status = "up",
|
||||
timestamp = java.time.OffsetDateTime.now().format(formatter),
|
||||
service = "ping-service",
|
||||
service = properties.serviceName,
|
||||
healthy = true
|
||||
)
|
||||
}
|
||||
|
||||
@GetMapping("/ping/history")
|
||||
fun getHistory() = pingUseCase.getPingHistory().map {
|
||||
mapOf("id" to it.id.toString(), "message" to it.message, "time" to it.timestamp.toString())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,3 +80,10 @@ resilience4j:
|
||||
instances:
|
||||
pingCircuitBreaker:
|
||||
base-config: default
|
||||
|
||||
# ==========================================================================
|
||||
# Custom Application Properties
|
||||
# ==========================================================================
|
||||
app:
|
||||
service-name: ${spring.application.name}
|
||||
service-name-fallback: "${spring.application.name}-fallback"
|
||||
|
||||
Reference in New Issue
Block a user