mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-28 05:46:36 +08:00
81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
ServerURL string `json:"server_url"`
|
|
AgentToken string `json:"agent_token"`
|
|
NodeName string `json:"node_name"`
|
|
NodeIP string `json:"node_ip"`
|
|
AgentVersion string `json:"agent_version"`
|
|
NginxVersion string `json:"nginx_version"`
|
|
RouteConfigPath string `json:"route_config_path"`
|
|
StatePath string `json:"state_path"`
|
|
NginxBinary string `json:"nginx_binary"`
|
|
HeartbeatInterval time.Duration `json:"heartbeat_interval"`
|
|
SyncInterval time.Duration `json:"sync_interval"`
|
|
RequestTimeout time.Duration `json:"request_timeout"`
|
|
}
|
|
|
|
func Load(path string) (*Config, error) {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
cfg := &Config{}
|
|
if err = json.Unmarshal(data, cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
applyDefaults(cfg)
|
|
if err = validate(cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
func applyDefaults(cfg *Config) {
|
|
if cfg.AgentVersion == "" {
|
|
cfg.AgentVersion = "dev"
|
|
}
|
|
if cfg.RouteConfigPath == "" {
|
|
cfg.RouteConfigPath = filepath.Clean("./atsflare_routes.conf")
|
|
}
|
|
if cfg.StatePath == "" {
|
|
cfg.StatePath = filepath.Clean("./atsf_agent_state.json")
|
|
}
|
|
if cfg.NginxBinary == "" {
|
|
cfg.NginxBinary = "nginx"
|
|
}
|
|
if cfg.HeartbeatInterval <= 0 {
|
|
cfg.HeartbeatInterval = 30 * time.Second
|
|
}
|
|
if cfg.SyncInterval <= 0 {
|
|
cfg.SyncInterval = 30 * time.Second
|
|
}
|
|
if cfg.RequestTimeout <= 0 {
|
|
cfg.RequestTimeout = 10 * time.Second
|
|
}
|
|
}
|
|
|
|
func validate(cfg *Config) error {
|
|
if cfg.ServerURL == "" {
|
|
return errors.New("server_url 不能为空")
|
|
}
|
|
if cfg.AgentToken == "" {
|
|
return errors.New("agent_token 不能为空")
|
|
}
|
|
if cfg.NodeName == "" {
|
|
return errors.New("node_name 不能为空")
|
|
}
|
|
if cfg.NodeIP == "" {
|
|
return errors.New("node_ip 不能为空")
|
|
}
|
|
return nil
|
|
}
|