Skip to content

Commit

Permalink
feat: do once billing checkin callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Aliothmoon committed Feb 8, 2025
1 parent f79e76e commit 5e62cf9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.12.0</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
Expand Down
7 changes: 5 additions & 2 deletions src/main/kotlin/biz/cdkey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fun validateCDK(params: ValidateParams): Resp {
val status = record.status
expireTime.isBefore(LocalDateTime.now()).throwIf("The cdk has expired")

limit(cdk)
doLimit(cdk)


val isFirstBinding = status == 0
Expand All @@ -142,6 +142,9 @@ fun validateCDK(params: ValidateParams): Resp {
(row > 0).throwIfNot("cdk binding update failed")
}

Thread.startVirtualThread {
doSendBillingCheckIn(cdk, params.resource ?: "", params.ua ?: "")
}

// log
BT.enqueue(
Expand All @@ -158,7 +161,7 @@ fun validateCDK(params: ValidateParams): Resp {
return Resp.success()
}

private fun limit(cdk: String) {
private fun doLimit(cdk: String) {
if (!Props.Extra.limitEnabled) {
return
}
Expand Down
43 changes: 43 additions & 0 deletions src/main/kotlin/biz/rpc.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package biz

import config.Props
import io.vertx.core.Vertx
import io.vertx.core.http.HttpClient
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.slf4j.LoggerFactory

private val log = LoggerFactory.getLogger("RpcKt")!!

val C: OkHttpClient = OkHttpClient.Builder()
.build()

private val JSON_MT = "application/json; charset=utf-8".toMediaType()

private lateinit var client: HttpClient

fun setupClient(v: Vertx) {
client = v.createHttpClient()
}

fun doSendBillingCheckIn(cdk: String, resource: String, ua: String) {
val body = """
{
"cdk": "$cdk",
"application": "$resource",
"user_agent": "$ua",
"module": "",
}
""".trimIndent().toRequestBody(contentType = JSON_MT)
val resp = Request.Builder()
.url(Props.Extra.billingCheckInUrl)
.post(body)
.build().let {
C.newCall(it).execute()
}
if (resp.code != 200) {
log.error("BillingCheckIn response {}", resp.body?.string())
}
}
3 changes: 3 additions & 0 deletions src/main/kotlin/config/props.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,8 @@ object Props {
val limitCount: Long by lazy {
TomlStore.config.value.getLong("extra.limit.count") ?: 7
}
val billingCheckInUrl: String by lazy {
TomlStore.config.value.getString("extra.billing.checkin_url")
}
}
}

0 comments on commit 5e62cf9

Please sign in to comment.