proxy-detector/config/config.go

54 lines
1.3 KiB
Go
Raw Permalink Normal View History

package config
import (
"os"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
2024-12-10 10:44:32 +08:00
var configFilePath = "./config/config.yaml"
type Options struct {
//Domain string `json:"domain" yaml:"domain"`
Port string `json:"port" yaml:"port"`
CrawlInterval uint64 `json:"crawl-interval" yaml:"crawl-interval"`
SourceFiles []string `json:"source-files" yaml:"source-files"`
HealthCheckTimeout int `json:"healthcheck-timeout" yaml:"healthcheck-timeout"`
HealthCheckInterval int `json:"healthcheck-interval" yaml:"healthcheck-interval"`
Goroutines int `json:"goroutines" json:"goroutines"`
}
// Config 配置
var Config Options
func LoadConfig(cfg string) {
if cfg == "" {
cfg = configFilePath
}
f, err := os.Open(cfg)
if err != nil {
log.Fatalf("open config file error: %s", err.Error())
}
defer f.Close()
err = yaml.NewDecoder(f).Decode(&Config)
if err != nil {
log.Fatalf("decode config file error: %s", err.Error())
}
if Config.Goroutines <= 0 {
Config.Goroutines = 500
}
if Config.CrawlInterval == 0 {
Config.CrawlInterval = 30
}
if Config.HealthCheckTimeout == 0 {
Config.HealthCheckTimeout = 10
}
if Config.HealthCheckInterval == 0 {
Config.HealthCheckInterval = 30
}
if Config.Port == "" {
Config.Port = "12580"
}
}