feat: added server config

This commit is contained in:
darwincereska
2025-12-17 09:47:09 -05:00
parent 5b70c6de89
commit 87d094e2f1

View File

@@ -0,0 +1,82 @@
package org.ccoin.config
import org.slf4j.LoggerFactory
object ServerConfig {
private val logger = LoggerFactory.getLogger(ServerConfig::class.java)
// Server settings
val host: String = System.getenv("SERVER_HOST") ?: "0.0.0.0"
val port: Int = System.getenv("SERVER_PORT")?.toIntOrNull() ?: 8080
val developmentMode: Boolean = System.getenv("DEVELOPMENT_MODE")?.toBoolean() ?: false
// Mining settings
val miningDifficulty: Int = System.getenv("MINING_DIFFICULTY")?.toIntOrNull() ?: 4
val miningReward: Double = System.getenv("MINING_REWARD")?.toDoubleOrNull() ?: 50.0
val blockTimeTarget: Long = System.getenv("BLOCK_TIME_TARGET")?.toLongOrNull() ?: 600000L // 10 minutes
// Transaction settings
val defaultTransactionFee: Double = System.getenv("DEFAULT_TRANSACTION_FEE")?.toDoubleOrNull() ?: 0.01
val maxTransactionSize: Int = System.getenv("MAX_TRANSACTION_SIZE")?.toIntOrNull() ?: (1024 * 1024) // 1MB
val maxMemoLength: Int = System.getenv("MAX_MEMO_LENGTH")?.toIntOrNull() ?: 256
// Security settings
val jwtSecret: String = System.getenv("JWT_SECRET") ?: "change-this-in-production"
val rateLimitRequests: Int = System.getenv("RATE_LIMIT_REQUESTS")?.toIntOrNull() ?: 100
val rateLimitWindow: Long = System.getenv("RATE_LIMIT_WINDOW")?.toLongOrNull() ?: 60000L // 1 minute
// Blockchain settings
val maxBlockSize: Int = System.getenv("MAX_BLOCK_SIZE")?.toIntOrNull() ?: 1024 * 1024 // 1MB
val maxTransactionsPerBlock: Int = System.getenv("MAX_TRANSACTIONS_PER_BLOCK")?.toIntOrNull() ?: 1000
val confirmationsRequired: Int = System.getenv("CONFIRMATIONS_REQUIRED")?.toIntOrNull() ?: 6
// API Settings
val maxPageSize: Int = System.getenv("MAX_PAGE_SIZE")?.toIntOrNull() ?: 100
val defaultPageSize: Int = System.getenv("DEFAULT_PAGE_SIZE")?.toIntOrNull() ?: 50
val apiTimeout: Long = System.getenv("API_TIMEOUT")?.toLongOrNull() ?: 30000L // 30 seconds
// Logging settings
val logLevel: String = System.getenv("LOG_LEVEL") ?: "INFO"
init {
logger.info("Server configuration loaded:")
logger.info("Host: $host")
logger.info("Port: $port")
logger.info("Development mode: $developmentMode")
logger.info("Mining difficulty: $miningDifficulty")
logger.info("Mining reward: $miningReward")
logger.info("Block time target: ${blockTimeTarget}ms")
logger.info("Default transaction fee: $defaultTransactionFee")
logger.info("Confirmations required: $confirmationsRequired")
if (jwtSecret == "change-this-in-production" && !developmentMode) {
logger.warn("WARNING: Using default JWT secret in production mode!")
}
}
fun getServerInfo(): Map<String, Any> {
return mapOf(
"host" to host,
"port" to port,
"developmentMode" to developmentMode,
"version" to "1.0.0",
"miningDifficulty" to miningDifficulty,
"miningReward" to miningReward,
"blockTimeTarget" to blockTimeTarget,
"confirmationsRequired" to confirmationsRequired
)
}
fun validateConfig() {
require(port in 1..65535) { "Port must be between 1 and 65535" }
require(miningDifficulty in 1..32) { "Mining difficulty must be between 1 and 32" }
require(miningReward > 0) { "Mining reward must be positive" }
require(blockTimeTarget > 0) { "Block time target must be positive" }
require(defaultTransactionFee >= 0) { "Default transaction fee cannot be negative" }
require(confirmationsRequired > 0) { "Confirmations required must be positive" }
require(maxPageSize > 0) { "Max page size must be positive" }
require(defaultPageSize > 0) { "Default page size must be positive" }
logger.info("Server configuration validation passed")
}
}