CCTV/config/env.go
2025-02-21 23:37:34 +01:00

63 lines
997 B
Go

package config
import (
"bufio"
"os"
"strings"
"git.neunzweinull.com/jan/CCTV/internal"
)
const (
Prefix = "CCTV_"
)
func LoadEnv() error {
// TODO: Load env from custom file
setDefaultEnvs()
file, err := os.Open(".env")
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.Split(scanner.Text(), "=")
//Ignore empty lines and comments
if len(line) < 2 || strings.HasPrefix(line[0], "#") {
continue
}
key := strings.TrimSpace(line[0])
value := strings.TrimSpace(line[1])
if key == "" || value == "" {
continue
}
// Add prefix
key = Prefix + key
// Remove quotes
value = strings.Trim(value, `"'`)
os.Setenv(key, value)
internal.SetShit()
}
return scanner.Err()
}
func setDefaultEnvs() {
os.Setenv(Prefix+"UploadDir", "uploads/")
os.Setenv(Prefix+"LOG_FILE", "auth_failures.log")
os.Setenv(Prefix+"MaxFailedAttempts", "3")
os.Setenv(Prefix+"BanDuration", "5")
}