Skip to content

Commit

Permalink
Fix configuration issue
Browse files Browse the repository at this point in the history
  • Loading branch information
IRus committed Nov 10, 2024
1 parent 64c37b5 commit 0b9ca86
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 33 deletions.
6 changes: 5 additions & 1 deletion src/main/kotlin/io/heapy/ddns/Application.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
@file:JvmName("Application")
package io.heapy.ddns

import io.heapy.ddns.client.ClientFactory
import io.heapy.ddns.server.ServerFactory
import io.heapy.komok.tech.dotenv.dotenv

suspend fun main() {
val config = dotenv()
val config = config()

if (config["MODE"] == "server") {
ServerFactory(config).start()
} else {
ClientFactory(config).start()
}
}

fun config(): Map<String, String> =
System.getenv() + dotenv()
11 changes: 11 additions & 0 deletions src/main/kotlin/io/heapy/ddns/client/ClientConfiguration.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.heapy.ddns.client

import kotlin.time.Duration

data class ClientConfiguration(
val serverUrl: String,
val checkPeriod: Duration,
val requestTimeout: Duration,
val attemptsBeforeWarning: Int,
val telegram: TelegramConfiguration?,
)
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package io.heapy.ddns
package io.heapy.ddns.client

import io.heapy.ddns.dns_clients.CloudflareDnsClient
import io.heapy.ddns.dns_clients.CloudflareVerifyToken
import io.heapy.ddns.dns_clients.DigitalOceanDnsClient
import io.heapy.ddns.dns_clients.DnsClient
import io.heapy.ddns.ip_provider.IpProvider
import io.heapy.ddns.ip_provider.ServerIpProvider
import io.heapy.ddns.updater.SimpleUpdater
import io.heapy.ddns.updater.Updater
import io.heapy.ddns.notifiers.Notifier
import io.heapy.ddns.notifiers.TelegramNotifier
import io.heapy.ddns.updater.SimpleUpdater
import io.heapy.ddns.updater.Updater
import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.serialization.kotlinx.json.json
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.json.Json
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes
Expand All @@ -22,19 +22,6 @@ import kotlin.time.Duration.Companion.seconds
open class ClientFactory(
open val config: Map<String, String>,
) {
data class Configuration(
val serverUrl: String,
val checkPeriod: Duration,
val requestTimeout: Duration,
val attemptsBeforeWarning: Int,
val telegram: Telegram?,
) {
data class Telegram(
val token: String,
val chatId: String,
)
}

open val httpClient by lazy {
HttpClient(CIO) {
install(ContentNegotiation) {
Expand All @@ -47,7 +34,7 @@ open class ClientFactory(

open val updater: Updater by lazy {
SimpleUpdater(
checkPeriod = configuration.checkPeriod,
checkPeriod = clientConfiguration.checkPeriod,
ipProvider = ipProvider,
notifier = notifier,
dnsClients = dnsClients,
Expand Down Expand Up @@ -76,7 +63,7 @@ open class ClientFactory(
?: error("DIGITALOCEAN_DOMAIN_NAME is not set"),
subdomain = config["DIGITALOCEAN_SUBDOMAIN"]
?: error("DIGITALOCEAN_SUBDOMAIN is not set"),
ttl = configuration.checkPeriod.inWholeSeconds,
ttl = clientConfiguration.checkPeriod.inWholeSeconds,
)
}

Expand Down Expand Up @@ -111,7 +98,7 @@ open class ClientFactory(
}

open val notifier: Notifier? by lazy {
configuration.telegram?.let { telegram ->
clientConfiguration.telegram?.let { telegram ->
TelegramNotifier(
httpClient = httpClient,
telegram = telegram,
Expand All @@ -122,25 +109,25 @@ open class ClientFactory(
open val ipProvider: IpProvider by lazy {
ServerIpProvider(
httpClient = httpClient,
serverUrl = configuration.serverUrl,
serverUrl = clientConfiguration.serverUrl,
)
}

open val checkPeriod by lazy {
config["CHECK_PERIOD"]?.let(Duration::parse)
config["CHECK_PERIOD"]?.let(Duration.Companion::parse)
?: 5.minutes
}

open val configuration by lazy {
Configuration(
open val clientConfiguration by lazy {
ClientConfiguration(
serverUrl = config["SERVER_URL"]
?: error("SERVER_URL is not set"),
checkPeriod = checkPeriod,
requestTimeout = config["REQUEST_TIMEOUT"]?.let(Duration::parse)
requestTimeout = config["REQUEST_TIMEOUT"]?.let(Duration.Companion::parse)
?: 30.seconds,
attemptsBeforeWarning = config["ATTEMPTS_BEFORE_WARNING"]?.toInt() ?: 5,
telegram = config["TELEGRAM_TOKEN"]?.let { token ->
Configuration.Telegram(
TelegramConfiguration(
token = token,
chatId = config["TELEGRAM_CHAT_ID"]
?: error("TELEGRAM_CHAT_ID is not set"),
Expand All @@ -153,3 +140,4 @@ open class ClientFactory(
updater.start()
}
}

6 changes: 6 additions & 0 deletions src/main/kotlin/io/heapy/ddns/client/TelegramConfiguration.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.heapy.ddns.client

data class TelegramConfiguration(
val token: String,
val chatId: String,
)
4 changes: 2 additions & 2 deletions src/main/kotlin/io/heapy/ddns/notifiers/TelegramNotifier.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.heapy.ddns.notifiers

import io.heapy.ddns.ClientFactory
import io.heapy.ddns.client.TelegramConfiguration
import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.http.*
Expand All @@ -9,7 +9,7 @@ import kotlinx.serialization.Serializable

class TelegramNotifier(
private val httpClient: HttpClient,
private val telegram: ClientFactory.Configuration.Telegram,
private val telegram: TelegramConfiguration,
) : Notifier {
@Serializable
data class SendMessageRequest(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package io.heapy.ddns.dns_clients

import io.heapy.ddns.ClientFactory
import io.heapy.komok.tech.dotenv.dotenv
import io.heapy.ddns.client.ClientFactory
import io.heapy.ddns.config
import kotlinx.coroutines.runBlocking
import org.junit.Test
import kotlin.test.assertEquals

class CloudflareClientFactoryTest {
@Test
fun `verify test token`() = runBlocking {
val factory = ClientFactory(dotenv())
val factory = ClientFactory(config())
val token = factory.cloudflareToken

val response = factory.cloudflareVerifyToken(token)
Expand All @@ -22,7 +22,7 @@ class CloudflareClientFactoryTest {

@Test
fun `get dns record`() = runBlocking {
val factory = ClientFactory(dotenv())
val factory = ClientFactory(config())
val config = factory.cloudflareConfiguration

val record = factory.cloudflareDnsClient.getRecord(
Expand Down

0 comments on commit 0b9ca86

Please sign in to comment.