feat(config): 本地config文件缺失时使用默认配置

This commit is contained in:
tangerineyu
2026-03-24 19:05:06 +08:00
parent 01bfe23e1c
commit 21efce6049
5 changed files with 62 additions and 9 deletions

View File

@@ -15,10 +15,16 @@ import (
func main() {
// 加载配置
log.Printf("Loading config from configs/config.yaml")
cfg, err := config.Load("configs/config.yaml")
const configPath = "configs/config.yaml"
cfg, usedDefault, err := config.LoadLocalDev(configPath)
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
if usedDefault {
log.Printf("Config File %s not found, using default local config", configPath)
} else {
log.Printf("Config loaded from file: %s", configPath)
}
// 连接数据库
//log.Printf("Database config: %v", cfg.Database)

View File

@@ -38,11 +38,17 @@ const (
func main() {
// 加载配置
log.Printf("Loading config from configs/config.yaml")
cfg, err := config.Load("configs/config.yaml")
const configPath = "configs/config.yaml"
log.Printf("Loading config from %s", configPath)
cfg, usedDefault, err := config.LoadLocalDev(configPath)
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
if usedDefault {
log.Printf("Config File %s not found, using default local config", configPath)
} else {
log.Printf("Config loaded from file: %s", configPath)
}
// 连接数据库
sqlDB, err := db.NewDB(cfg.Database)
if err != nil {

View File

@@ -46,7 +46,7 @@ require (
golang.org/x/arch v0.20.0 // indirect
golang.org/x/mod v0.25.0 // indirect
golang.org/x/net v0.42.0 // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/sync v0.16.0
golang.org/x/sys v0.35.0 // indirect
golang.org/x/text v0.27.0 // indirect
golang.org/x/tools v0.34.0 // indirect

View File

@@ -1,8 +1,9 @@
package config
import (
"io/ioutil"
"fmt"
"os"
"errors"
"gopkg.in/yaml.v3"
)
@@ -40,15 +41,54 @@ type RabbitMQConfig struct {
}
func Load(filename string) (Config, error) {
data, err := ioutil.ReadFile(filename)
data, err := os.ReadFile(filename)
if err != nil {
return Config{}, err
return Config{}, fmt.Errorf("failed to read config file: %w", err)
}
var cfg Config
if err := yaml.Unmarshal(data, &cfg); err != nil {
return Config{}, err
return Config{}, fmt.Errorf("parse config %s: %w", filename, err)
}
return cfg, nil
}
// 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",
},
}
}

View File

@@ -61,6 +61,7 @@ func (r *LikeRepository) IsLiked(ctx context.Context, videoID, accountID uint) (
}
return count > 0, nil
}
func (r *LikeRepository) BatchGetLiked(ctx context.Context, videoIDs []uint, accountID uint) (map[uint]bool, error) {
likeMap := make(map[uint]bool)
if len(videoIDs) == 0 {