config.go 3.87 KB
Newer Older
1 2 3 4 5 6 7 8
package config

import (
	"encoding/json"
	"os"
	"strings"

	"github.com/rs/zerolog/log"
9
	"gitlab.com/tensorsecurity-rd/waf-console/internal/config"
10 11 12 13 14 15 16 17 18 19 20
)

const (
	DB_USER     = "ivan"
	DB_PASSWORD = "Mysql-ha@123"
	DB_HOST     = "localhost"
	DB_PORT     = "3306"
	DB_NAME     = "waf"
)

type Config struct {
21 22 23 24 25 26 27
	DBConfig            *DBConfig                   `json:"db_config"`
	RegionConfigs       []RegionConfig              `json:"region_configs"`
	Debug               bool                        `json:"debug"`
	GatewayUrl          string                      `json:"gateway_url"`
	SSOUrl              string                      `json:"sso_url"`
	ElasticsearchConfig *config.ElasticsearchConfig `json:"elasticsearch_config"`
	KafkaConfig         *config.KafkaConfig         `json:"kafka_config"`
28 29 30 31 32 33 34 35 36 37 38 39 40
}

type DBConfig struct {
	User     string `json:"user"`
	Password string `json:"password"`
	Host     string `json:"host"`
	Port     string `json:"port"`
	Database string `json:"database"`
}

type RegionConfig struct {
	RegionCode     string `json:"region_code"`
	ApiServer      string `json:"api_server"`
41
	WafApiServer   string `json:"waf_api_server"`
42 43 44 45 46 47 48
	CAData         string `json:"ca_data"`
	Token          string `json:"token"`
	ClientCertData string `json:"client_cert_data"`
	ClientKeyData  string `json:"client_key_data"`
	Insecure       bool   `json:"insecure"`
}

49 50 51 52 53 54
// type ElasticsearchConfig struct {
// 	Url      string `json:"url"`
// 	Username string `json:"username"`
// 	Password string `json:"password"`
// 	Sniff    bool   `json:"sniff"`
// }
55

56 57 58 59 60 61 62 63 64
// 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"`
// }
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

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
	}
	password := os.Getenv("RDB_PASSWORD")
	if password != "" {
		config.DBConfig.Password = password
	}
	// 如果config.DBConfig为nil,则使用默认值
	if config.DBConfig == nil {
		config.DBConfig = &DBConfig{
			User:     DB_USER,
			Password: DB_PASSWORD,
			Host:     DB_HOST,
			Port:     DB_PORT,
			Database: DB_NAME,
		}
	}
	esURL := os.Getenv("ELASTIC_URL")
	if esURL != "" {
		config.ElasticsearchConfig.Url = esURL
	}
	esUsername := os.Getenv("ELASTIC_USERNAME")
	if esUsername != "" {
		config.ElasticsearchConfig.Username = esUsername
	}
	esPassword := os.Getenv("ELASTIC_PASSWORD")
	if esPassword != "" {
		config.ElasticsearchConfig.Password = esPassword
	}
	sniff := os.Getenv("ELASTIC_SNIFF")
	if sniff != "" {
		config.ElasticsearchConfig.Sniff = (sniff == "true")
	}

	kafkaBrokers := os.Getenv("KAFKA_BROKERS")
	if kafkaBrokers != "" {
		config.KafkaConfig.Brokers = strings.Split(kafkaBrokers, ",")
	}
	kafkaTopic := os.Getenv("KAFKA_TOPIC")
	if kafkaTopic != "" {
		config.KafkaConfig.Topic = kafkaTopic
	}
	kafkaGroup := os.Getenv("KAFKA_GROUP")
	if kafkaGroup != "" {
		config.KafkaConfig.Group = kafkaGroup
	}
	kafkaAuthMethod := os.Getenv("KAFKA_AUTH_METHOD")
	if kafkaAuthMethod != "" {
		config.KafkaConfig.AuthMethod = kafkaAuthMethod
	}
	kafkaUsername := os.Getenv("KAFKA_USERNAME")
	if kafkaUsername != "" {
		config.KafkaConfig.Username = kafkaUsername
	}
	kafkaPassword := os.Getenv("KAFKA_PASSWORD")
	if kafkaPassword != "" {
		config.KafkaConfig.Password = kafkaPassword
	}
	kafkaScramAlgo := os.Getenv("KAFKA_SCRAM_ALGO")
	if kafkaScramAlgo != "" {
		config.KafkaConfig.ScramAlgo = kafkaScramAlgo
	}

	return &config
}