Files
food-ai/backend/cmd/server/main.go
dbastrikin 5096df2102 fix: fix menu generation errors and show planned meals on home screen
Backend fixes:
- migration 003: add 'menu' value to recipe_source enum (was causing SQLSTATE 22P02)
- migration 004: rename recipe_products→recipe_ingredients, product_id→ingredient_id (was causing SQLSTATE 42P01)
- dish/repository.go: fix INSERT INTO tags using $1/$1 for two columns → $1/$2 (was causing SQLSTATE 42P08)
- home/handler.go: replace non-existent saved_recipes table with correct joins (recipes→dishes→dish_translations, user_saved_recipes) so today's plan and recommendations load correctly
- reqlog: new slog.Handler wrapper that adds request_id and stack trace to ERROR-level logs
- all handlers: slog.Error→slog.ErrorContext so error logs include request context; writeError includes request_id in response body

Client:
- home_screen.dart: extend home screen to future dates, show planned meals as ghost entries
- l10n: add new localisation keys for home screen date navigation and planned meal UI

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-22 00:35:11 +02:00

83 lines
2.1 KiB
Go

package main
import (
"context"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/food-ai/backend/internal/infra/config"
"github.com/food-ai/backend/internal/infra/database"
"github.com/food-ai/backend/internal/infra/locale"
"github.com/food-ai/backend/internal/infra/reqlog"
)
func main() {
baseHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelInfo,
})
logger := slog.New(reqlog.New(baseHandler))
slog.SetDefault(logger)
if runError := run(); runError != nil {
slog.Error("fatal error", "err", runError)
os.Exit(1)
}
}
func run() error {
appConfig, configError := config.Load()
if configError != nil {
return fmt.Errorf("load config: %w", configError)
}
applicationContext, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
pool, poolError := database.NewPool(applicationContext, appConfig.DatabaseURL)
if poolError != nil {
return fmt.Errorf("connect to database: %w", poolError)
}
defer pool.Close()
slog.Info("connected to database")
if loadError := locale.LoadFromDB(applicationContext, pool); loadError != nil {
return fmt.Errorf("load languages: %w", loadError)
}
slog.Info("languages loaded", "count", len(locale.Languages))
application, initError := initApp(appConfig, pool)
if initError != nil {
return fmt.Errorf("init app: %w", initError)
}
application.Start(applicationContext)
httpServer := &http.Server{
Addr: fmt.Sprintf(":%d", appConfig.Port),
Handler: application,
ReadTimeout: 10 * time.Second,
WriteTimeout: 120 * time.Second, // menu generation can take ~60s
IdleTimeout: 60 * time.Second,
}
go func() {
slog.Info("server starting", "port", appConfig.Port)
if serverError := httpServer.ListenAndServe(); serverError != nil && serverError != http.ErrServerClosed {
slog.Error("server error", "err", serverError)
}
}()
<-applicationContext.Done()
slog.Info("shutting down...")
shutdownContext, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
return httpServer.Shutdown(shutdownContext)
}