2024-01-20 01:27:10 +02:00
|
|
|
package config
|
|
|
|
|
2024-01-20 02:06:27 +02:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
)
|
|
|
|
|
2024-01-20 01:27:10 +02:00
|
|
|
type DHConfig struct {
|
2024-01-20 18:35:50 +02:00
|
|
|
DiskFetchFrequency int `json:"diskFetchFrequency"`
|
|
|
|
MaxHistoryAge int `json:"maxHistoryAge"`
|
|
|
|
DatabaseFilePath string
|
|
|
|
Listen string
|
2024-01-20 01:27:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetConfiguration() DHConfig {
|
2024-01-20 02:06:27 +02:00
|
|
|
// Load .env file if it exists
|
|
|
|
if err := godotenv.Load(); err != nil {
|
|
|
|
log.Println("No .env file found")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := DHConfig{
|
2024-01-20 18:35:50 +02:00
|
|
|
DiskFetchFrequency: 5, // default value
|
|
|
|
MaxHistoryAge: 2592000, // default value
|
2024-01-21 15:27:42 +02:00
|
|
|
DatabaseFilePath: "./data.sqlite",
|
2024-01-20 02:09:10 +02:00
|
|
|
|
|
|
|
Listen: ":8080",
|
2024-01-20 02:06:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if val, exists := os.LookupEnv("DISK_FETCH_FREQUENCY"); exists {
|
|
|
|
if intValue, err := strconv.Atoi(val); err == nil {
|
|
|
|
config.DiskFetchFrequency = intValue
|
|
|
|
}
|
|
|
|
}
|
2024-01-20 01:27:10 +02:00
|
|
|
|
2024-01-20 02:06:27 +02:00
|
|
|
if val, exists := os.LookupEnv("MAX_HISTORY_AGE"); exists {
|
|
|
|
if intValue, err := strconv.Atoi(val); err == nil {
|
|
|
|
config.MaxHistoryAge = intValue
|
|
|
|
}
|
2024-01-20 01:27:10 +02:00
|
|
|
}
|
|
|
|
|
2024-01-20 02:09:10 +02:00
|
|
|
if val, exists := os.LookupEnv("LISTEN"); exists {
|
|
|
|
config.Listen = val
|
|
|
|
}
|
|
|
|
|
2024-01-20 18:35:50 +02:00
|
|
|
if val, exists := os.LookupEnv("DATABASE_FILE_PATH"); exists {
|
|
|
|
config.DatabaseFilePath = val
|
|
|
|
}
|
|
|
|
|
2024-01-20 02:06:27 +02:00
|
|
|
return config
|
2024-01-20 01:27:10 +02:00
|
|
|
}
|