plakken/internal/config/config.go
Ada f94fa6cec8
All checks were successful
ci/woodpecker/push/release Pipeline was successful
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/pr/release Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pull_request_closed/release Pipeline was successful
ci/woodpecker/pull_request_closed/lint Pipeline was successful
ci/woodpecker/pull_request_closed/build Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
️ Revert dotenv removal
2024-05-01 19:19:05 +02:00

64 lines
1.4 KiB
Go

package config
import (
"log"
"os"
"strconv"
"git.gnous.eu/gnouseu/plakken/internal/constant"
"git.gnous.eu/gnouseu/plakken/internal/utils"
"github.com/joho/godotenv"
)
// InitConfig Structure for program initialisation.
type InitConfig struct {
ListenAddress string
RedisAddress string
RedisUser string
RedisPassword string
RedisDB int
URLLength uint8
}
// GetConfig Initialise configuration form .env.
func GetConfig() InitConfig {
if utils.FileExist(".env") {
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
}
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")
}
if urlLength > constant.MaxURLLength {
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,
URLLength: uint8(urlLength),
}
}