This commit is contained in:
pavel 2026-05-15 17:40:09 +02:00
commit 07522043b4
16 changed files with 870 additions and 38 deletions

View file

@ -14,32 +14,43 @@ import (
"github.com/joho/godotenv"
)
func Run() error {
func initApp() (*App, func(), error) {
_ = godotenv.Load()
cfg, err := loadConfig()
if err != nil {
return fmt.Errorf("config error: %w", err)
return nil, nil, fmt.Errorf("config error: %w", err)
}
db, err := sql.Open("pgx", cfg.DatabaseURL)
if err != nil {
return fmt.Errorf("database open error: %w", err)
return nil, nil, fmt.Errorf("database open error: %w", err)
}
defer db.Close()
cleanup := func() { _ = db.Close() }
if err := db.Ping(); err != nil {
return fmt.Errorf("database ping error: %w", err)
cleanup()
return nil, nil, fmt.Errorf("database ping error: %w", err)
}
app := &App{cfg: cfg, db: db}
if err := app.migrate(context.Background()); err != nil {
return fmt.Errorf("migration error: %w", err)
a := &App{cfg: cfg, db: db}
if err := a.migrate(context.Background()); err != nil {
cleanup()
return nil, nil, fmt.Errorf("migration error: %w", err)
}
return a, cleanup, nil
}
func Run() error {
app, cleanup, err := initApp()
if err != nil {
return err
}
defer cleanup()
handler := app.routes()
servers, cleanup, err := startServers(cfg, handler)
servers, sockCleanup, err := startServers(app.cfg, handler)
if err != nil {
return fmt.Errorf("server start error: %w", err)
}
defer cleanup()
defer sockCleanup()
log.Printf("server started: tcp=%q unix=%q dev_mode=%t", cfg.ListenAddr, cfg.UnixSocketPath, cfg.DevMode)
log.Printf("server started: tcp=%q unix=%q dev_mode=%t", app.cfg.ListenAddr, app.cfg.UnixSocketPath, app.cfg.DevMode)
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
<-sigCh