diff --git a/backend/cmd/main.go b/backend/cmd/main.go index 0f8b205..e853ed5 100644 --- a/backend/cmd/main.go +++ b/backend/cmd/main.go @@ -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) diff --git a/backend/cmd/worker/main.go b/backend/cmd/worker/main.go index 72c05b5..1618b3a 100644 --- a/backend/cmd/worker/main.go +++ b/backend/cmd/worker/main.go @@ -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 { diff --git a/backend/go.mod b/backend/go.mod index fd2b43e..d0134dc 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -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 diff --git a/backend/internal/config/loadconfig.go b/backend/internal/config/loadconfig.go index 5a443e3..0cd3523 100644 --- a/backend/internal/config/loadconfig.go +++ b/backend/internal/config/loadconfig.go @@ -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", + }, + } +} \ No newline at end of file diff --git a/backend/internal/video/like_repo.go b/backend/internal/video/like_repo.go index 28a72fe..237c6c3 100644 --- a/backend/internal/video/like_repo.go +++ b/backend/internal/video/like_repo.go @@ -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 {