-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: impl http method for healthcheck
- Loading branch information
1 parent
946d870
commit 366cc4e
Showing
10 changed files
with
165 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* This file was generated by the Gradle 'init' task. | ||
* | ||
* This generated file contains a sample Kotlin application project to get you started. | ||
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.7/userguide/building_java_projects.html in the Gradle documentation. | ||
* This project uses @Incubating APIs which are subject to change. | ||
*/ | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
|
||
plugins { | ||
// Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin. | ||
alias(libs.plugins.jvm) | ||
id("org.springframework.boot") version "3.2.2" | ||
id("io.spring.dependency-management") version "1.1.4" | ||
kotlin("plugin.spring") version "1.9.22" | ||
kotlin("plugin.jpa") version "1.9.22" | ||
// Apply the application plugin to add support for building a CLI application in Java. | ||
application | ||
} | ||
|
||
repositories { | ||
// Use Maven Central for resolving dependencies. | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
// This dependency is used by the application. | ||
implementation(libs.guava) | ||
implementation(platform("org.springframework.boot:spring-boot-dependencies:3.2.2")) | ||
implementation("org.springframework.boot:spring-boot-starter") | ||
implementation("org.springframework.boot:spring-boot-starter-web") | ||
// implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3") | ||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1-Beta") | ||
implementation(project(":healthcheck")) | ||
} | ||
|
||
tasks.withType<KotlinCompile> { | ||
kotlinOptions { | ||
freeCompilerArgs = listOf("-Xjsr305=strict") | ||
jvmTarget = "17" | ||
} | ||
} | ||
|
||
testing { | ||
suites { | ||
// Configure the built-in test suite | ||
val test by getting(JvmTestSuite::class) { | ||
// Use Kotlin Test test framework | ||
useKotlinTest("1.9.22") | ||
} | ||
} | ||
} | ||
|
||
// Apply a specific Java toolchain to ease working on different environments. | ||
java { | ||
toolchain { | ||
languageVersion = JavaLanguageVersion.of(17) | ||
} | ||
} | ||
|
||
application { | ||
// Define the main class for the application. | ||
mainClass = "com.manhinhang.ibgatewaydocker.healthcheck.rest.MainKt" | ||
} |
27 changes: 27 additions & 0 deletions
27
...k-rest/src/main/kotlin/com/manhinhang/ibgatewaydocker/healthcheck/rest/HttpControllers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.manhinhang.ibgatewaydocker.healthcheck.rest | ||
import com.manhinhang.ibgatewaydocker.healthcheck.IBGatewayClient | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.* | ||
import kotlinx.coroutines.* | ||
|
||
@RestController | ||
@RequestMapping("/") | ||
class HealthcheckApiController() { | ||
|
||
val ibClient = IBGatewayClient() | ||
|
||
@GetMapping("/ready") | ||
fun ready(): ResponseEntity<Any> { | ||
return ResponseEntity.status(HttpStatus.OK).body("OK"); | ||
} | ||
|
||
@GetMapping("/healthcheck") | ||
fun healthcheck(): ResponseEntity<Any> { | ||
val result = runBlocking { ibClient.ping() } | ||
if (result.isSuccess) { | ||
return ResponseEntity.status(HttpStatus.OK).body("OK") | ||
} | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Fail") | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
.../healthcheck-rest/src/main/kotlin/com/manhinhang/ibgatewaydocker/healthcheck/rest/Main.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.manhinhang.ibgatewaydocker.healthcheck.rest | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication | ||
import org.springframework.boot.runApplication | ||
|
||
@SpringBootApplication | ||
class RestApplication | ||
|
||
fun main(args: Array<String>) { | ||
runApplication<RestApplication>(*args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
healthcheck/healthcheck/src/main/kotlin/com/manhinhang/ibgatewaydocker/healthcheck/App.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* This source file was generated by the Gradle 'init' task | ||
*/ | ||
package com.manhinhang.ibgatewaydocker.healthcheck | ||
import kotlinx.coroutines.* | ||
|
||
fun main() = runBlocking { | ||
val client = IBGatewayClient() | ||
val result = client.ping() | ||
result.exceptionOrNull()?.let { throw it } | ||
return@runBlocking | ||
} |
49 changes: 49 additions & 0 deletions
49
...healthcheck/src/main/kotlin/com/manhinhang/ibgatewaydocker/healthcheck/IBGatewayClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.manhinhang.ibgatewaydocker.healthcheck | ||
|
||
import com.ib.client.EClientSocket | ||
import com.ib.client.EJavaSignal | ||
import kotlinx.coroutines.* | ||
|
||
class IBGatewayClient { | ||
val client: EClientSocket | ||
companion object { | ||
val clientId = (System.getenv("HEALTHCHECK_CLIENT_ID")?.toIntOrNull() ?: 999) | ||
val port = (System.getenv("IB_GATEWAY_INTERNAL_PORT")?.toIntOrNull() ?: 4001) | ||
val host = "localhost" | ||
} | ||
|
||
init { | ||
client = createIBClient() | ||
} | ||
|
||
private fun createIBClient(): EClientSocket { | ||
val signal = EJavaSignal(); | ||
val client = EClientSocket(Wrapper(), signal) | ||
return client | ||
} | ||
|
||
suspend fun connect():Boolean = withContext(Dispatchers.IO) { | ||
if (!client.isConnected) { | ||
val client = createIBClient() | ||
client.eConnect(host, port, clientId) | ||
} | ||
client.isConnected | ||
} | ||
|
||
fun disconnect() { | ||
client.eDisconnect() | ||
} | ||
|
||
suspend fun ping():Result<Any> = coroutineScope { | ||
runCatching { | ||
val isConnected = connect() | ||
if (isConnected) { | ||
println("Ping IB Gateway successful") | ||
client.eDisconnect() | ||
}else { | ||
throw InterruptedException("Can not connect to IB Gateway") | ||
} | ||
} | ||
} | ||
|
||
} |
File renamed without changes.
24 changes: 0 additions & 24 deletions
24
healthcheck/healthcheck/src/main/kotlin/com/manhinhang/ibgatewaydocker/heathcheck/App.kt
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ plugins { | |
|
||
rootProject.name = "healthcheck" | ||
include("healthcheck") | ||
include("healthcheck-rest") |