Skip to content
Snippets Groups Projects
config.go 2.55 KiB
Newer Older
  • Learn to ignore specific revisions
  • OZGCloud's avatar
    OZGCloud committed
    /*
     * Copyright (C) 2023-2024
     * Das Land Schleswig-Holstein vertreten durch den
     * Ministerpräsidenten des Landes Schleswig-Holstein
     * Staatskanzlei
     * Abteilung Digitalisierung und zentrales IT-Management der Landesregierung
     *
     * Lizenziert unter der EUPL, Version 1.2 oder - sobald
     * diese von der Europäischen Kommission genehmigt wurden -
     * Folgeversionen der EUPL ("Lizenz");
     * Sie dürfen dieses Werk ausschließlich gemäß
     * dieser Lizenz nutzen.
     * Eine Kopie der Lizenz finden Sie hier:
     *
     * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
     *
     * Sofern nicht durch anwendbare Rechtsvorschriften
     * gefordert oder in schriftlicher Form vereinbart, wird
     * die unter der Lizenz verbreitete Software "so wie sie
     * ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
     * ausdrücklich oder stillschweigend - verbreitet.
     * Die sprachspezifischen Genehmigungen und Beschränkungen
     * unter der Lizenz sind dem Lizenztext zu entnehmen.
     */
    
    package config
    
    import (
    	"github.com/kelseyhightower/envconfig"
    	"gopkg.in/yaml.v3"
    
    	log "github.com/sirupsen/logrus"
        "go.elastic.co/ecslogrus"
    
    OZGCloud's avatar
    OZGCloud committed
    	"os"
    	"regexp"
    	"testing"
    
    OZGCloud's avatar
    OZGCloud committed
    	"fmt"
    
    OZGCloud's avatar
    OZGCloud committed
    )
    
    const (
    	grpcUrlRegex    = `^[A-Za-z\d-]+(\.[A-Za-z\d-]+){0,512}:\d{1,5}$`
    
    OZGCloud's avatar
    OZGCloud committed
    	defaultConfig      = "config.yml"
    	testFilePath       = "testdata/"
    	activeProfileParam = "ACTIVE_PROFILE"
    
    OZGCloud's avatar
    OZGCloud committed
    )
    
    type Config struct {
    	Server struct {
    
    OZGCloud's avatar
    OZGCloud committed
    		Port int `yaml:"port"`
    
    OZGCloud's avatar
    OZGCloud committed
    	} `yaml:"server"`
    	Grpc struct {
    
    OZGCloud's avatar
    OZGCloud committed
    		Mock bool   `yaml:"mock"`
    
    OZGCloud's avatar
    OZGCloud committed
    		Url  string `yaml:"url" envconfig:"GRPC_URL"`
    	} `yaml:"grpc"`
    	Logging struct {
    
    OZGCloud's avatar
    OZGCloud committed
    		Level string `yaml:"level"`
    
    OZGCloud's avatar
    OZGCloud committed
    	} `yaml:"logging"`
    }
    
    func LoadConfig(configFilePath ...string) Config {
    	var config Config
    
    	log.SetFormatter(&ecslogrus.Formatter{})
    
    OZGCloud's avatar
    OZGCloud committed
    	configFile := defaultConfig
    
    	env := os.Getenv(activeProfileParam)
    	if env != "" {
    		configFile = fmt.Sprintf("config-%s.yml", env)
    	}
    
    	fp := fmt.Sprintf("config/%s", configFile)
    
    
    OZGCloud's avatar
    OZGCloud committed
    	if len(configFilePath) > 0 {
    		fp = configFilePath[0]
    	} else if testing.Testing() {
    
    OZGCloud's avatar
    OZGCloud committed
    		fp = testFilePath + configFile
    
    OZGCloud's avatar
    OZGCloud committed
    	}
    
    	file, err := os.ReadFile(fp)
    	if err != nil {
    		log.Fatalf("FATAL: error reading YAML file: %v", err)
    	}
    
    	err = yaml.Unmarshal(file, &config)
    	if err != nil {
    		log.Fatalf("FATAL: error unmarshalling YAML file: %v", err)
    	}
    	err = envconfig.Process("", &config)
    	if err != nil {
    		log.Fatalf("FATAL: error reading env: %v", err)
    	}
    	if len(config.Grpc.Url) > 0 && !ValidateGrpcUrl(config.Grpc.Url) {
    		log.Fatalf("FATAL: gRPC URL is not in host:port format")
    	}
    
    	return config
    }
    
    func ValidateGrpcUrl(grpcUrl string) bool {
    	return regexp.MustCompile(grpcUrlRegex).MatchString(grpcUrl)
    }