2024-01-25 17:58:55 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2024-04-10 19:34:36 +02:00
|
|
|
|
|
|
|
"git.gnous.eu/gnouseu/plakken/internal/constant"
|
2024-05-01 19:19:05 +02:00
|
|
|
"git.gnous.eu/gnouseu/plakken/internal/utils"
|
|
|
|
"github.com/joho/godotenv"
|
2024-01-25 17:58:55 +01:00
|
|
|
)
|
|
|
|
|
2024-04-10 19:34:36 +02:00
|
|
|
// InitConfig Structure for program initialisation.
|
2024-01-25 17:58:55 +01:00
|
|
|
type InitConfig struct {
|
|
|
|
ListenAddress string
|
|
|
|
RedisAddress string
|
|
|
|
RedisUser string
|
|
|
|
RedisPassword string
|
|
|
|
RedisDB int
|
2024-04-10 19:34:36 +02:00
|
|
|
URLLength uint8
|
2024-01-25 17:58:55 +01:00
|
|
|
}
|
|
|
|
|
2024-04-10 19:34:36 +02:00
|
|
|
// GetConfig Initialise configuration form .env.
|
2024-01-25 17:58:55 +01:00
|
|
|
func GetConfig() InitConfig {
|
2024-05-01 19:19:05 +02:00
|
|
|
if utils.FileExist(".env") {
|
|
|
|
err := godotenv.Load()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error loading .env file: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-25 17:58:55 +01:00
|
|
|
listenAddress := os.Getenv("PLAKKEN_LISTEN")
|
|
|
|
redisAddress := os.Getenv("PLAKKEN_REDIS_ADDRESS")
|
|
|
|
db := os.Getenv("PLAKKEN_REDIS_DB")
|
|
|
|
uLen := os.Getenv("PLAKKEN_URL_LENGTH")
|
|
|
|
|
|
|
|
if listenAddress == "" || redisAddress == "" {
|
|
|
|
log.Fatal("Missing or invalid listenAddress or PLAKKEN_REDIS_ADDRESS")
|
|
|
|
}
|
|
|
|
|
|
|
|
redisDB, err := strconv.Atoi(db)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Invalid PLAKKEN_REDIS_DB")
|
|
|
|
}
|
|
|
|
|
|
|
|
urlLength, err := strconv.Atoi(uLen)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Invalid PLAKKEN_URL_LENGTH")
|
|
|
|
}
|
|
|
|
|
2024-04-10 19:34:36 +02:00
|
|
|
if urlLength > constant.MaxURLLength {
|
2024-01-25 17:58:55 +01:00
|
|
|
log.Fatal("PLAKKEN_URL_LENGTH cannot be greater than 255")
|
|
|
|
}
|
|
|
|
|
|
|
|
return InitConfig{
|
|
|
|
ListenAddress: listenAddress,
|
|
|
|
RedisAddress: redisAddress,
|
|
|
|
RedisUser: os.Getenv("PLAKKEN_REDIS_USER"),
|
|
|
|
RedisPassword: os.Getenv("PLAKKEN_REDIS_PASSWORD"),
|
|
|
|
RedisDB: redisDB,
|
2024-04-10 19:34:36 +02:00
|
|
|
URLLength: uint8(urlLength),
|
2024-01-25 17:58:55 +01:00
|
|
|
}
|
|
|
|
}
|