config.go 1.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
package config

import (
	"encoding/json"
	"os"

	"github.com/rs/zerolog/log"
	"gitlab.com/tensorsecurity-rd/waf-console/internal/config"
)

type RegionConfig struct {
	RegionCode string `json:"region_code"`
13
	RegionName string `json:"region_name"`
14 15 16 17
	ApiServer  string `json:"api_server"`
}

type Config struct {
18
	RegionConfig        RegionConfig                `json:"region_config"`
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	Debug               bool                        `json:"debug"`
	SSOUrl              string                      `json:"sso_url"`
	ElasticsearchConfig *config.ElasticsearchConfig `json:"elasticsearch_config"`
	KafkaConfig         *config.KafkaConfig         `json:"kafka_config"`
}

// type ElasticsearchConfig struct {
// 	Url      string `json:"url"`
// 	Username string `json:"username"`
// 	Password string `json:"password"`
// 	Sniff    bool   `json:"sniff"`
// }

// type KafkaConfig struct {
// 	Brokers    []string `json:"brokers"`
// 	Topic      string   `json:"topic"`
// 	Group      string   `json:"group"`
// 	AuthMethod string   `json:"auth_method"`
// 	Username   string   `json:"username"`
// 	Password   string   `json:"password"`
// 	ScramAlgo  string   `json:"scram_algo"`
// }

func LoadConfig() *Config {
	configFile := "config/config.json"
	if envFile := os.Getenv("CONFIG_FILE"); envFile != "" {
		configFile = envFile
	}

	data, err := os.ReadFile(configFile)
	if err != nil {
		log.Err(err).Msgf("Failed to read config file: %s", configFile)
		return nil
	}

	var config Config
	if err := json.Unmarshal(data, &config); err != nil {
		log.Err(err).Msg("Failed to parse config file")
		return nil
	}
59 60 61 62 63 64 65 66 67 68
	esPassword := os.Getenv("ELASTIC_PASSWORD")
	if esPassword != "" {
		config.ElasticsearchConfig.Password = esPassword
	}

	kafkaPassword := os.Getenv("KAFKA_PASSWORD")
	if kafkaPassword != "" {
		config.KafkaConfig.Password = kafkaPassword
	}

69 70
	return &config
}