2025-12-04 17:09:04 +08:00
|
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-03-24 19:05:06 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
"errors"
|
2025-12-04 17:09:04 +08:00
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
|
|
Server ServerConfig `yaml:"server"`
|
|
|
|
|
|
Database DatabaseConfig `yaml:"database"`
|
2025-12-29 04:13:13 +08:00
|
|
|
|
Redis RedisConfig `yaml:"redis"`
|
|
|
|
|
|
RabbitMQ RabbitMQConfig `yaml:"rabbitmq"`
|
2025-12-04 17:09:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type ServerConfig struct {
|
2025-12-23 02:21:33 +08:00
|
|
|
|
Port int `yaml:"port"`
|
2025-12-04 17:09:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type DatabaseConfig struct {
|
|
|
|
|
|
Host string `yaml:"host"`
|
2025-12-23 02:21:33 +08:00
|
|
|
|
Port int `yaml:"port"`
|
2025-12-04 17:09:04 +08:00
|
|
|
|
User string `yaml:"user"`
|
|
|
|
|
|
Password string `yaml:"password"`
|
|
|
|
|
|
DBName string `yaml:"dbname"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 04:13:13 +08:00
|
|
|
|
type RedisConfig struct {
|
|
|
|
|
|
Host string `yaml:"host"`
|
|
|
|
|
|
Port int `yaml:"port"`
|
|
|
|
|
|
Password string `yaml:"password"`
|
|
|
|
|
|
DB int `yaml:"db"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type RabbitMQConfig struct {
|
|
|
|
|
|
Host string `yaml:"host"`
|
|
|
|
|
|
Port int `yaml:"port"`
|
|
|
|
|
|
Username string `yaml:"username"`
|
|
|
|
|
|
Password string `yaml:"password"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-04 17:09:04 +08:00
|
|
|
|
func Load(filename string) (Config, error) {
|
2026-03-24 19:05:06 +08:00
|
|
|
|
data, err := os.ReadFile(filename)
|
2025-12-04 17:09:04 +08:00
|
|
|
|
if err != nil {
|
2026-03-24 19:05:06 +08:00
|
|
|
|
return Config{}, fmt.Errorf("failed to read config file: %w", err)
|
2025-12-04 17:09:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var cfg Config
|
|
|
|
|
|
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
2026-03-24 19:05:06 +08:00
|
|
|
|
return Config{}, fmt.Errorf("parse config %s: %w", filename, err)
|
2025-12-04 17:09:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
|
|
}
|
2026-03-24 19:05:06 +08:00
|
|
|
|
|
|
|
|
|
|
// bool用来表示是否使用了默认配置,true表示使用了默认配置
|
|
|
|
|
|
func LoadLocalDev(filename string) (Config, bool, error) {
|
|
|
|
|
|
cfg, err := Load(filename)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
return cfg, false, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
|
|
return DefaultLocalConfig(), true, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return Config{}, false, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func DefaultLocalConfig() Config {
|
|
|
|
|
|
return Config{
|
|
|
|
|
|
Server: ServerConfig{
|
|
|
|
|
|
Port: 8080,
|
|
|
|
|
|
},
|
|
|
|
|
|
Database: DatabaseConfig{
|
|
|
|
|
|
Host: "localhost",
|
|
|
|
|
|
Port: 3306,
|
|
|
|
|
|
User: "root",
|
|
|
|
|
|
Password: "123456",
|
|
|
|
|
|
DBName: "feedsystem",
|
|
|
|
|
|
},
|
|
|
|
|
|
Redis: RedisConfig{
|
|
|
|
|
|
Host: "localhost",
|
|
|
|
|
|
Port: 6379,
|
|
|
|
|
|
Password: "123456",
|
|
|
|
|
|
DB: 0,
|
|
|
|
|
|
},
|
|
|
|
|
|
RabbitMQ: RabbitMQConfig{
|
|
|
|
|
|
Host: "localhost",
|
|
|
|
|
|
Port: 5672,
|
|
|
|
|
|
Username: "admin",
|
|
|
|
|
|
Password: "password123",
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|