2025-03-31 17:52:55 +02:00

73 lines
1.6 KiB
Go

package main
import (
"html/template"
"log"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
)
func init() {
loadEnvConfig()
loadYAMLConfig()
loadTemplate()
}
func loadEnvConfig() {
requiredEnvVars := []string{
"LDAP_SERVER", "LDAP_PORT", "LDAP_BIND_DN", "LDAP_BIND_PASSWORD",
"LDAP_BASE_DN", "LDAP_FILTER", "SERVER_PORT",
}
for _, envVar := range requiredEnvVars {
if value := os.Getenv(envVar); value == "" {
log.Fatalf("Required environment variable %s is not set", envVar)
}
}
ldapConfig = LDAPConfig{
Server: os.Getenv("LDAP_SERVER"),
Port: os.Getenv("LDAP_PORT"),
BindDN: os.Getenv("LDAP_BIND_DN"),
BindPassword: os.Getenv("LDAP_BIND_PASSWORD"),
BaseDN: os.Getenv("LDAP_BASE_DN"),
Filter: os.Getenv("LDAP_FILTER"),
}
serverPort = os.Getenv("SERVER_PORT")
}
func loadYAMLConfig() {
configPath := filepath.Join("static", "config.yaml")
configFile, err := os.ReadFile(configPath)
if err != nil {
log.Fatalf("Failed to read config file: %v", err)
}
err = yaml.Unmarshal(configFile, &config)
if err != nil {
log.Fatalf("Failed to parse config file: %v", err)
}
validateConfig()
}
func validateConfig() {
if config.PhoneRules.Country.Prefix == "" ||
len(config.PhoneRules.Country.AreaCodes) == 0 ||
config.PhoneRules.Country.InternalPrefix == "" ||
config.PhoneRules.InvalidNumber == "" {
log.Fatalf("Missing required configuration in config.yaml")
}
}
func loadTemplate() {
var err error
tmpl, err = template.ParseFiles(filepath.Join("static", "index.html"))
if err != nil {
log.Fatalf("Failed to parse template: %v", err)
}
}