refactor(global): move config to global package and consolidate handlers
- Move Config struct and related functions to global package - Consolidate handler utilities into utils package - Remove deprecated handlers and instance packages - Add new handlers package with settings and proxy functionality - Update config.json to include watchdog enabled flag
This commit is contained in:
@@ -2,26 +2,15 @@ package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"super-frpc/global"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ListenAddr string `json:"listenAddr"`
|
||||
ListenPort string `json:"listenPort"`
|
||||
FrpcPath string `json:"frpcPath"`
|
||||
InstancePath string `json:"instancePath"`
|
||||
Debug bool `json:"debug"`
|
||||
Watchdog struct {
|
||||
Port int `json:"port"`
|
||||
} `json:"watchdog"`
|
||||
}
|
||||
|
||||
type InstanceInfo struct {
|
||||
Name string `json:"name"`
|
||||
ServerAddr string `json:"serverAddr"`
|
||||
@@ -56,60 +45,64 @@ type FrpcConfig struct {
|
||||
Proxies []map[string]interface{} `toml:"proxies"`
|
||||
}
|
||||
|
||||
var globalConfig *Config
|
||||
|
||||
func LoadConfig(configPath string, getInitSystem func() string) (*Config, error) {
|
||||
func LoadConfig(configPath string, getInitSystem func() string) error {
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read config file: %w", err)
|
||||
return fmt.Errorf("failed to read config file: %w", err)
|
||||
}
|
||||
|
||||
var config Config
|
||||
if err := json.Unmarshal(data, &config); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse config file: %w", err)
|
||||
var fileConfig global.Config
|
||||
if err := json.Unmarshal(data, &fileConfig); err != nil {
|
||||
return fmt.Errorf("failed to parse config file: %w", err)
|
||||
}
|
||||
|
||||
if config.ListenAddr == "" {
|
||||
config.ListenAddr = "0.0.0.0"
|
||||
if fileConfig.ListenAddr != "" {
|
||||
global.CurrentConfig.ListenAddr = fileConfig.ListenAddr
|
||||
}
|
||||
|
||||
if config.ListenPort == "" {
|
||||
config.ListenPort = "8080"
|
||||
if fileConfig.ListenPort != "" {
|
||||
global.CurrentConfig.ListenPort = fileConfig.ListenPort
|
||||
}
|
||||
|
||||
if config.FrpcPath == "" {
|
||||
if fileConfig.FrpcPath != "" {
|
||||
global.CurrentConfig.FrpcPath = fileConfig.FrpcPath
|
||||
} else {
|
||||
if getInitSystem() == "windows" {
|
||||
config.FrpcPath = "frp_client/frpc.exe"
|
||||
global.CurrentConfig.FrpcPath = "frp_client/frpc.exe"
|
||||
} else {
|
||||
config.FrpcPath = "/usr/bin/frpc"
|
||||
global.CurrentConfig.FrpcPath = "/usr/bin/frpc"
|
||||
}
|
||||
}
|
||||
|
||||
if config.InstancePath == "" {
|
||||
config.InstancePath = "./configs"
|
||||
if fileConfig.InstancePath != "" {
|
||||
global.CurrentConfig.InstancePath = fileConfig.InstancePath
|
||||
} else {
|
||||
global.CurrentConfig.InstancePath = "./configs"
|
||||
}
|
||||
|
||||
if config.Watchdog.Port == 0 {
|
||||
config.Watchdog.Port = 12380
|
||||
global.CurrentConfig.Debug = fileConfig.Debug
|
||||
|
||||
if fileConfig.Watchdog.Port != 0 {
|
||||
global.CurrentConfig.Watchdog.Port = fileConfig.Watchdog.Port
|
||||
} else {
|
||||
global.CurrentConfig.Watchdog.Port = 12380
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(config.InstancePath, 0755); err != nil {
|
||||
return nil, fmt.Errorf("failed to create config directory: %w", err)
|
||||
global.CurrentConfig.Watchdog.Enabled = fileConfig.Watchdog.Enabled
|
||||
|
||||
if err := os.MkdirAll(global.CurrentConfig.InstancePath, 0755); err != nil {
|
||||
return fmt.Errorf("failed to create config directory: %w", err)
|
||||
}
|
||||
|
||||
globalConfig = &config
|
||||
return &config, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetConfig() (*Config, error) {
|
||||
if globalConfig == nil {
|
||||
return nil, errors.New("config not loaded")
|
||||
}
|
||||
return globalConfig, nil
|
||||
func GetConfig() *global.Config {
|
||||
return &global.CurrentConfig
|
||||
}
|
||||
|
||||
func SaveConfig(configPath string, config *Config) error {
|
||||
data, err := json.MarshalIndent(config, "", " ")
|
||||
func SaveConfig(configPath string) error {
|
||||
data, err := json.MarshalIndent(global.CurrentConfig, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal config: %w", err)
|
||||
}
|
||||
@@ -118,7 +111,6 @@ func SaveConfig(configPath string, config *Config) error {
|
||||
return fmt.Errorf("failed to write config file: %w", err)
|
||||
}
|
||||
|
||||
globalConfig = config
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user