Skip to content

Commit d692c8b

Browse files
committed
style: spotless
1 parent 2bad49c commit d692c8b

37 files changed

+2463
-2505
lines changed

build.gradle.kts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ plugins {
33
alias(libs.plugins.ksp)
44
alias(libs.plugins.ktor)
55
alias(libs.plugins.kotlin.plugin.serialization)
6-
alias(libs.plugins.ktfmt)
76
alias(libs.plugins.sonarqube)
87
alias(libs.plugins.kotest)
8+
alias(libs.plugins.spotless)
99
}
1010

1111
group = "com.bangbang93.openbmclapi.agent"
@@ -74,7 +74,11 @@ dependencies {
7474

7575
kotlin { jvmToolchain(17) }
7676

77-
ktfmt { kotlinLangStyle() }
77+
spotless {
78+
kotlin {
79+
ktfmt().metaStyle()
80+
}
81+
}
7882

7983
sonar {
8084
properties {

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mockk = "1.13.14"
2020
bouncycastle = "1.79"
2121
weupnp = "0.1.4"
2222
kotest = "6.1.2"
23+
spotless = '8.2.1'
2324

2425
[libraries]
2526
koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" }
@@ -72,3 +73,4 @@ kotlin-plugin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization"
7273
ktfmt = { id = "com.ncorti.ktfmt.gradle", version.ref = "ktfmt" }
7374
sonarqube = { id = "org.sonarqube", version = "7.1.0.6387" }
7475
kotest = { id = "io.kotest", version.ref = "kotest" }
76+
spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }

src/main/kotlin/com/bangbang93/openbmclapi/agent/Application.kt

Lines changed: 91 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -27,113 +27,111 @@ import org.koin.logger.slf4jLogger
2727
private val logger = KotlinLogging.logger {}
2828

2929
suspend fun main() {
30-
// Start Koin for dependency injection
31-
val koinApp = startKoin {
32-
slf4jLogger()
33-
modules(AppModule().module)
34-
}
30+
// Start Koin for dependency injection
31+
val koinApp = startKoin {
32+
slf4jLogger()
33+
modules(AppModule().module)
34+
}
3535

36-
val config = koinApp.koin.get<ClusterConfig>()
36+
val config = koinApp.koin.get<ClusterConfig>()
3737

38-
logger.info { "Starting OpenBMCLAPI Kotlin version ${Version.current}" }
39-
logger.info { "Cluster ID: ${config.clusterId}" }
38+
logger.info { "Starting OpenBMCLAPI Kotlin version ${Version.current}" }
39+
logger.info { "Cluster ID: ${config.clusterId}" }
4040

41-
// Determine if we should use HTTPS and setup certificates
42-
var useHttps = false
43-
var keystorePath: String? = null
41+
// Determine if we should use HTTPS and setup certificates
42+
var useHttps = false
43+
var keystorePath: String? = null
4444

45-
try {
46-
val tokenManager = koinApp.koin.get<TokenManager>()
47-
val clusterService = koinApp.koin.get<ClusterService>()
48-
val certificateService = koinApp.koin.get<CertificateService>()
45+
try {
46+
val tokenManager = koinApp.koin.get<TokenManager>()
47+
val clusterService = koinApp.koin.get<ClusterService>()
48+
val certificateService = koinApp.koin.get<CertificateService>()
4949

50-
// Get token first
51-
tokenManager.getToken()
50+
// Get token first
51+
tokenManager.getToken()
5252

53-
// Connect to cluster (needed for non-BYOC certificate requests)
54-
clusterService.connect()
53+
// Connect to cluster (needed for non-BYOC certificate requests)
54+
clusterService.connect()
5555

56-
// Setup certificates (will either load local or request from server)
57-
useHttps = certificateService.setupCertificates()
56+
// Setup certificates (will either load local or request from server)
57+
useHttps = certificateService.setupCertificates()
5858

59-
if (useHttps) {
60-
keystorePath = certificateService.getKeystorePath()
61-
logger.info { "HTTPS enabled with keystore at: $keystorePath" }
62-
} else {
63-
logger.info { "HTTP mode - no certificates available" }
64-
}
65-
} catch (e: Exception) {
66-
logger.warn(e) { "Failed to setup certificates, using HTTP" }
67-
useHttps = false
59+
if (useHttps) {
60+
keystorePath = certificateService.getKeystorePath()
61+
logger.info { "HTTPS enabled with keystore at: $keystorePath" }
62+
} else {
63+
logger.info { "HTTP mode - no certificates available" }
6864
}
65+
} catch (e: Exception) {
66+
logger.warn(e) { "Failed to setup certificates, using HTTP" }
67+
useHttps = false
68+
}
69+
70+
val env = koinApp.koin.get<ApplicationEnvironment>()
71+
72+
// Create and start embedded server with appropriate configuration
73+
val server =
74+
embeddedServer(
75+
Netty,
76+
env,
77+
configure = {
78+
// Add HTTPS connector if certificates are available, otherwise HTTP
79+
if (useHttps && keystorePath != null && File(keystorePath).exists()) {
80+
try {
81+
val keyStore = KeyStore.getInstance("JKS")
82+
FileInputStream(keystorePath).use { fis ->
83+
keyStore.load(fis, "openbmclapi".toCharArray())
84+
}
6985

70-
val env = koinApp.koin.get<ApplicationEnvironment>()
71-
72-
// Create and start embedded server with appropriate configuration
73-
val server =
74-
embeddedServer(
75-
Netty,
76-
env,
77-
configure = {
78-
// Add HTTPS connector if certificates are available, otherwise HTTP
79-
if (useHttps && keystorePath != null && File(keystorePath).exists()) {
80-
try {
81-
val keyStore = KeyStore.getInstance("JKS")
82-
FileInputStream(keystorePath).use { fis ->
83-
keyStore.load(fis, "openbmclapi".toCharArray())
84-
}
85-
86-
sslConnector(
87-
keyStore = keyStore,
88-
keyAlias = "openbmclapi",
89-
keyStorePassword = { "openbmclapi".toCharArray() },
90-
privateKeyPassword = { "openbmclapi".toCharArray() },
91-
) {
92-
port = config.port
93-
host = "0.0.0.0"
94-
}
95-
logger.info { "HTTPS connector configured on port ${config.port}" }
96-
} catch (e: Exception) {
97-
logger.error(e) {
98-
"Failed to configure HTTPS connector, falling back to HTTP"
99-
}
100-
connector {
101-
port = config.port
102-
host = "0.0.0.0"
103-
}
104-
}
105-
} else {
106-
// HTTP connector
107-
connector {
108-
port = config.port
109-
host = "0.0.0.0"
110-
}
86+
sslConnector(
87+
keyStore = keyStore,
88+
keyAlias = "openbmclapi",
89+
keyStorePassword = { "openbmclapi".toCharArray() },
90+
privateKeyPassword = { "openbmclapi".toCharArray() },
91+
) {
92+
port = config.port
93+
host = "0.0.0.0"
94+
}
95+
logger.info { "HTTPS connector configured on port ${config.port}" }
96+
} catch (e: Exception) {
97+
logger.error(e) { "Failed to configure HTTPS connector, falling back to HTTP" }
98+
connector {
99+
port = config.port
100+
host = "0.0.0.0"
111101
}
112-
},
113-
Application::module,
114-
)
102+
}
103+
} else {
104+
// HTTP connector
105+
connector {
106+
port = config.port
107+
host = "0.0.0.0"
108+
}
109+
}
110+
},
111+
Application::module,
112+
)
115113

116-
server.start(wait = true)
114+
server.start(wait = true)
117115
}
118116

119117
suspend fun Application.module() {
120-
configureSerialization()
121-
configureMonitoring()
122-
configureHTTP()
123-
configureRouting()
124-
125-
val bootstrapService by inject<BootstrapService>()
126-
127-
monitor.subscribe(ServerReady) { runBlocking { bootstrapService.bootstrap() } }
128-
129-
// Register shutdown hook
130-
monitor.subscribe(ApplicationStopPreparing) {
131-
runBlocking {
132-
try {
133-
bootstrapService.shutdown()
134-
} catch (e: Exception) {
135-
logger.error(e) { "Shutdown failed" }
136-
}
137-
}
118+
configureSerialization()
119+
configureMonitoring()
120+
configureHTTP()
121+
configureRouting()
122+
123+
val bootstrapService by inject<BootstrapService>()
124+
125+
monitor.subscribe(ServerReady) { runBlocking { bootstrapService.bootstrap() } }
126+
127+
// Register shutdown hook
128+
monitor.subscribe(ApplicationStopPreparing) {
129+
runBlocking {
130+
try {
131+
bootstrapService.shutdown()
132+
} catch (e: Exception) {
133+
logger.error(e) { "Shutdown failed" }
134+
}
138135
}
136+
}
139137
}

src/main/kotlin/com/bangbang93/openbmclapi/agent/HTTP.kt

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,21 @@ import io.ktor.server.plugins.defaultheaders.DefaultHeaders
1111
import io.ktor.server.plugins.partialcontent.PartialContent
1212

1313
fun Application.configureHTTP() {
14-
install(PartialContent) {
15-
// Maximum number of ranges that will be accepted from a HTTP request.
16-
// If the HTTP request specifies more ranges, they will all be merged into a single range.
17-
maxRangeCount = 10
18-
}
19-
install(DefaultHeaders) {
20-
header("X-Engine", "Ktor") // will send this header with each response
21-
}
22-
install(Compression)
23-
install(CachingHeaders) {
24-
options { _, outgoingContent ->
25-
when (outgoingContent.contentType?.withoutParameters()) {
26-
ContentType.Text.CSS ->
27-
CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 24 * 60 * 60))
28-
else -> null
29-
}
30-
}
14+
install(PartialContent) {
15+
// Maximum number of ranges that will be accepted from a HTTP request.
16+
// If the HTTP request specifies more ranges, they will all be merged into a single range.
17+
maxRangeCount = 10
18+
}
19+
install(DefaultHeaders) {
20+
header("X-Engine", "Ktor") // will send this header with each response
21+
}
22+
install(Compression)
23+
install(CachingHeaders) {
24+
options { _, outgoingContent ->
25+
when (outgoingContent.contentType?.withoutParameters()) {
26+
ContentType.Text.CSS -> CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 24 * 60 * 60))
27+
else -> null
28+
}
3129
}
30+
}
3231
}

src/main/kotlin/com/bangbang93/openbmclapi/agent/Monitoring.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import io.ktor.server.plugins.callid.callIdMdc
88
import io.ktor.server.plugins.calllogging.CallLogging
99

1010
fun Application.configureMonitoring() {
11-
install(CallId) {
12-
header(HttpHeaders.XRequestId)
13-
verify { callId: String -> callId.isNotEmpty() }
14-
}
15-
install(CallLogging) { callIdMdc("call-id") }
11+
install(CallId) {
12+
header(HttpHeaders.XRequestId)
13+
verify { callId: String -> callId.isNotEmpty() }
14+
}
15+
install(CallLogging) { callIdMdc("call-id") }
1616
}

src/main/kotlin/com/bangbang93/openbmclapi/agent/Routing.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import io.ktor.server.routing.routing
1313
import org.koin.ktor.ext.inject
1414

1515
fun Application.configureRouting() {
16-
val config by inject<ClusterConfig>()
17-
val storage by inject<IStorage>()
18-
val counters by inject<Counters>()
16+
val config by inject<ClusterConfig>()
17+
val storage by inject<IStorage>()
18+
val counters by inject<Counters>()
1919

20-
install(AutoHeadResponse)
21-
routing {
22-
get("/") { call.respondText("OpenBMCLAPI Cluster - Kotlin Edition") }
20+
install(AutoHeadResponse)
21+
routing {
22+
get("/") { call.respondText("OpenBMCLAPI Cluster - Kotlin Edition") }
2323

24-
clusterRoutes(config, storage, counters)
25-
}
24+
clusterRoutes(config, storage, counters)
25+
}
2626
}

src/main/kotlin/com/bangbang93/openbmclapi/agent/Serialization.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ import io.ktor.server.routing.get
99
import io.ktor.server.routing.routing
1010

1111
fun Application.configureSerialization() {
12-
install(ContentNegotiation) { json() }
13-
routing { get("/json/kotlinx-serialization") { call.respond(mapOf("hello" to "world")) } }
12+
install(ContentNegotiation) { json() }
13+
routing { get("/json/kotlinx-serialization") { call.respond(mapOf("hello" to "world")) } }
1414
}

0 commit comments

Comments
 (0)