mirror of
https://github.com/Sagit-chu/flvx.git
synced 2026-09-28 07:36:38 +08:00
44 lines
930 B
Go
44 lines
930 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
// Config 配置结构体
|
|
type Config struct {
|
|
Addr string `json:"addr"`
|
|
Secret string `json:"secret"`
|
|
Http int `json:"http"`
|
|
Tls int `json:"tls"`
|
|
Socks int `json:"socks"`
|
|
}
|
|
|
|
// LoadConfig 加载配置文件
|
|
func LoadConfig(configPath string) (*Config, error) {
|
|
// 检查文件是否存在
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
return nil, fmt.Errorf("配置文件不存在: %s", configPath)
|
|
}
|
|
|
|
// 读取文件内容
|
|
data, err := os.ReadFile(configPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("读取配置文件失败: %v", err)
|
|
}
|
|
|
|
// 解析JSON
|
|
var config Config
|
|
if err := json.Unmarshal(data, &config); err != nil {
|
|
return nil, fmt.Errorf("解析配置文件失败: %v", err)
|
|
}
|
|
|
|
// 验证必要的配置项
|
|
if config.Addr == "" {
|
|
return nil, fmt.Errorf("服务器地址不能为空")
|
|
}
|
|
|
|
return &config, nil
|
|
}
|