admin_backend/common/yaml.go
2025-02-20 15:40:00 +08:00

100 lines
2.0 KiB
Go

package common
import (
"fmt"
"log"
"os"
"gopkg.in/yaml.v2"
)
type SshConfig struct {
Name string `yaml:"name"`
Host string `yaml:"host"`
Port int `yaml:"port"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}
type MysqlConfig struct {
Name string `yaml:"name"`
Host string `yaml:"host"`
Port int `yaml:"port"`
Username string `yaml:"user"`
Password string `yaml:"password"`
Database string `yaml:"database"`
}
type SystemConfig struct {
NMap bool `yaml:"nmap"` // 是否开启端口扫描
FeishUrl string `yaml:"feishu_url"` // 飞书机器人url
NoticeUrl string `yaml:"notice_url"` // 通知url
OperationUrl string `yaml:"operation_url"` // 运营url
}
type Config struct {
Servers []SshConfig `yaml:"servers"`
Mysqls []MysqlConfig `yaml:"mysqls"`
System SystemConfig `yaml:"system"`
}
var config *Config
func init() {
err := loadConfig()
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
}
func loadConfig() error {
data, err := os.ReadFile("conf/server.yml")
if err != nil {
return err
}
err = yaml.Unmarshal(data, &config)
if err != nil {
return err
}
return nil
}
func GetServerConfig(name string) (*SshConfig, error) {
for _, server := range config.Servers {
if server.Name == name {
return &server, nil
}
}
return nil, fmt.Errorf("server configuration not found: %s", name)
}
func GetMysqlConfig(name string) (*MysqlConfig, error) {
for _, mysql := range config.Mysqls {
if mysql.Name == name {
return &mysql, nil
}
}
return nil, fmt.Errorf("mysql configuration not found: %s", name)
}
func GetWsConfig(name string) (*SshConfig, error) {
return GetServerConfig("ws")
}
func GetFeishuUrl() string {
return config.System.FeishUrl
}
func GetNoticeUrl() string {
return config.System.NoticeUrl
}
func GetOperationUrl() string {
return config.System.OperationUrl
}
func GetNMap() bool {
return config.System.NMap
}