feat: wire up MusicBrainz provider in application entry point
- Add App struct with Config, DB, and MusicBrainz client dependencies - NewApp() initializes database and MusicBrainz client from config - App.Close() cleans up resources (MB client + DB) on shutdown - App.run() blocks until context cancelled (goroutine-ready for future tasks) - Config validation already requires musicbrainz.user_agent - Add 6 tests: graceful shutdown, config integration, NewApp creation, shutdown cleanup, app.run shutdown, UserAgent validation - All tests pass (6/6 in cmd, full suite green)
This commit is contained in:
@@ -3,14 +3,24 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"naviwatcher/internal/config"
|
||||
"naviwatcher/internal/database"
|
||||
"naviwatcher/internal/musicbrainz"
|
||||
)
|
||||
|
||||
// App holds all application dependencies for clean shutdown and testability.
|
||||
type App struct {
|
||||
cfg *config.Config
|
||||
db *database.DB
|
||||
mbClient *musicbrainz.MusicBrainzClient
|
||||
}
|
||||
|
||||
const defaultConfigPath = "config.yaml"
|
||||
|
||||
func main() {
|
||||
@@ -35,20 +45,57 @@ func main() {
|
||||
go func() {
|
||||
sig := <-sigCh
|
||||
log.Printf("Received signal %v, shutting down...", sig)
|
||||
cancel()
|
||||
}()
|
||||
cancel()}()
|
||||
|
||||
if err := run(ctx, cfg); err != nil {
|
||||
app, err := NewApp(ctx, cfg)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to initialize application: %v", err)
|
||||
}
|
||||
defer app.Close()
|
||||
|
||||
if err := app.run(ctx); err != nil {
|
||||
log.Fatalf("Application error: %v", err)
|
||||
}
|
||||
|
||||
log.Println("NaviWatcher stopped.")
|
||||
}
|
||||
|
||||
func run(ctx context.Context, cfg *config.Config) error {
|
||||
// Main application loop — blocks until context is cancelled.
|
||||
// Business logic will be added in future tasks.
|
||||
<-ctx.Done()
|
||||
// NewApp initializes all application components: config, database, and MusicBrainz client.
|
||||
func NewApp(ctx context.Context, cfg *config.Config) (*App, error) {
|
||||
// Initialize database (uses default path or could be made configurable).
|
||||
db, err := database.New("naviwatcher.db")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to initialize database: %w", err)
|
||||
}
|
||||
|
||||
// Initialize MusicBrainz client with rate limiting.
|
||||
mbClient := musicbrainz.NewClient(cfg.MusicBrainz)
|
||||
|
||||
log.Printf("MusicBrainz client initialized (user-agent: %s)", cfg.MusicBrainz.UserAgent)
|
||||
|
||||
return &App{
|
||||
cfg: cfg,
|
||||
db: db,
|
||||
mbClient: mbClient,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Close cleans up all application resources in reverse order of initialization.
|
||||
func (a *App) Close() {
|
||||
if a.mbClient != nil {
|
||||
a.mbClient.Close()
|
||||
}
|
||||
if a.db != nil {
|
||||
if err := a.db.Close(); err != nil {
|
||||
log.Printf("Error closing database: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) run(ctx context.Context) error {
|
||||
// Main application loop — blocks until context is cancelled.
|
||||
// Business logic (scanner, notifier, web server) will be wired into
|
||||
// separate goroutines here in future tasks.
|
||||
<-ctx.Done()
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user