2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 04:22:33 +08:00
Files
panel/pkg/config/config.go
2026-01-08 18:03:23 +08:00

101 lines
2.1 KiB
Go

package config
import (
"os"
"go.yaml.in/yaml/v4"
"github.com/acepanel/panel/pkg/io"
)
const configPath = "/opt/ace/panel/storage/config.yml"
// Config 面板配置结构体
type Config struct {
App AppConfig `yaml:"app"`
HTTP HTTPConfig `yaml:"http"`
Database DatabaseConfig `yaml:"database"`
Session SessionConfig `yaml:"session"`
}
type AppConfig struct {
Debug bool `yaml:"debug"`
Key string `yaml:"key"`
Locale string `yaml:"locale"`
Timezone string `yaml:"timezone"`
Root string `yaml:"root"`
APIEndpoint string `yaml:"api_endpoint"`
DownloadEndpoint string `yaml:"download_endpoint"`
}
type HTTPConfig struct {
Debug bool `yaml:"debug"`
Port uint `yaml:"port"`
Entrance string `yaml:"entrance"`
EntranceError string `yaml:"entrance_error"`
TLS bool `yaml:"tls"`
ACME bool `yaml:"acme"`
LoginCaptcha bool `yaml:"login_captcha"`
IPHeader string `yaml:"ip_header"`
BindDomain []string `yaml:"bind_domain"`
BindIP []string `yaml:"bind_ip"`
BindUA []string `yaml:"bind_ua"`
}
type DatabaseConfig struct {
Debug bool `yaml:"debug"`
}
type SessionConfig struct {
Lifetime uint `yaml:"lifetime"`
}
func Load() (*Config, error) {
path := configPath
if !io.Exists(path) {
path = "config.yml" // For testing purpose
}
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer func() { _ = file.Close() }()
var conf Config
decoder := yaml.NewDecoder(file)
if err = decoder.Decode(&conf); err != nil {
return nil, err
}
return &conf, nil
}
func Save(conf *Config) error {
data, err := yaml.Marshal(conf)
if err != nil {
return err
}
return os.WriteFile(configPath, data, 0600)
}
func Check(conf *Config) (bool, error) {
currentConf, err := Load()
if err != nil {
return false, err
}
currentData, err := yaml.Marshal(currentConf)
if err != nil {
return false, err
}
newData, err := yaml.Marshal(conf)
if err != nil {
return false, err
}
return string(currentData) == string(newData), nil
}