Replaces the flat JSONB-based recipe schema with a normalized relational model:
Schema (migrations consolidated to 001_initial_schema + 002_seed_data):
- New: dishes, dish_translations, dish_tags — canonical dish catalog
- New: cuisines, tags, dish_categories with _translations tables + full seed data
- New: recipe_ingredients, recipe_steps with _translations (replaces JSONB blobs)
- New: user_saved_recipes thin bookmark (drops saved_recipes + saved_recipe_translations)
- New: product_ingredients M2M table
- recipes: now a cooking variant of a dish (dish_id FK, no title/JSONB columns)
- recipe_translations: repurposed to per-language notes only
- products: mapping_id → primary_ingredient_id
- menu_items: recipe_id FK → recipes; adds dish_id
- meal_diary: adds dish_id, recipe_id → recipes, portion_g
Backend (Go):
- New packages: internal/cuisine, internal/tag, internal/dish (registry + handler + repo)
- New GET /cuisines, GET /tags (public), GET /dishes, GET /dishes/{id}, GET /recipes/{id}
- recipe, savedrecipe, menu, diary, product, ingredient packages updated for new schema
Flutter:
- New models: Cuisine, Tag; new providers: cuisineNamesProvider, tagNamesProvider
- recipe.dart: RecipeIngredient gains unit_code + effectiveUnit getter
- saved_recipe.dart: thin model, manual fromJson, computed nutrition getter
- diary_entry.dart: adds dishId, recipeId, portionG
- recipe_detail_screen.dart: localized cuisine/tag names via providers
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
205 lines
5.5 KiB
Go
205 lines
5.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/food-ai/backend/internal/auth"
|
|
"github.com/food-ai/backend/internal/config"
|
|
"github.com/food-ai/backend/internal/cuisine"
|
|
"github.com/food-ai/backend/internal/database"
|
|
"github.com/food-ai/backend/internal/diary"
|
|
"github.com/food-ai/backend/internal/dish"
|
|
"github.com/food-ai/backend/internal/gemini"
|
|
"github.com/food-ai/backend/internal/home"
|
|
"github.com/food-ai/backend/internal/ingredient"
|
|
"github.com/food-ai/backend/internal/locale"
|
|
"github.com/food-ai/backend/internal/menu"
|
|
"github.com/food-ai/backend/internal/middleware"
|
|
"github.com/food-ai/backend/internal/units"
|
|
"github.com/food-ai/backend/internal/pexels"
|
|
"github.com/food-ai/backend/internal/product"
|
|
"github.com/food-ai/backend/internal/recipe"
|
|
"github.com/food-ai/backend/internal/recognition"
|
|
"github.com/food-ai/backend/internal/recommendation"
|
|
"github.com/food-ai/backend/internal/savedrecipe"
|
|
"github.com/food-ai/backend/internal/server"
|
|
"github.com/food-ai/backend/internal/tag"
|
|
"github.com/food-ai/backend/internal/user"
|
|
)
|
|
|
|
// jwtAdapter adapts auth.JWTManager to middleware.AccessTokenValidator.
|
|
type jwtAdapter struct {
|
|
jm *auth.JWTManager
|
|
}
|
|
|
|
func (a *jwtAdapter) ValidateAccessToken(tokenStr string) (*middleware.TokenClaims, error) {
|
|
claims, err := a.jm.ValidateAccessToken(tokenStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &middleware.TokenClaims{
|
|
UserID: claims.UserID,
|
|
Plan: claims.Plan,
|
|
}, nil
|
|
}
|
|
|
|
func main() {
|
|
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
|
|
Level: slog.LevelInfo,
|
|
}))
|
|
slog.SetDefault(logger)
|
|
|
|
if err := run(); err != nil {
|
|
slog.Error("fatal error", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run() error {
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
return fmt.Errorf("load config: %w", err)
|
|
}
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
pool, err := database.NewPool(ctx, cfg.DatabaseURL)
|
|
if err != nil {
|
|
return fmt.Errorf("connect to database: %w", err)
|
|
}
|
|
defer pool.Close()
|
|
slog.Info("connected to database")
|
|
|
|
if err := locale.LoadFromDB(ctx, pool); err != nil {
|
|
return fmt.Errorf("load languages: %w", err)
|
|
}
|
|
slog.Info("languages loaded", "count", len(locale.Languages))
|
|
|
|
if err := units.LoadFromDB(ctx, pool); err != nil {
|
|
return fmt.Errorf("load units: %w", err)
|
|
}
|
|
slog.Info("units loaded", "count", len(units.Records))
|
|
|
|
if err := cuisine.LoadFromDB(ctx, pool); err != nil {
|
|
return fmt.Errorf("load cuisines: %w", err)
|
|
}
|
|
slog.Info("cuisines loaded", "count", len(cuisine.Records))
|
|
|
|
if err := tag.LoadFromDB(ctx, pool); err != nil {
|
|
return fmt.Errorf("load tags: %w", err)
|
|
}
|
|
slog.Info("tags loaded", "count", len(tag.Records))
|
|
|
|
// Firebase auth
|
|
firebaseAuth, err := auth.NewFirebaseAuthOrNoop(cfg.FirebaseCredentialsFile)
|
|
if err != nil {
|
|
return fmt.Errorf("init firebase auth: %w", err)
|
|
}
|
|
|
|
// JWT manager
|
|
jwtManager := auth.NewJWTManager(cfg.JWTSecret, cfg.JWTAccessDuration, cfg.JWTRefreshDuration)
|
|
|
|
// User domain
|
|
userRepo := user.NewRepository(pool)
|
|
userService := user.NewService(userRepo)
|
|
userHandler := user.NewHandler(userService)
|
|
|
|
// Auth domain
|
|
authService := auth.NewService(firebaseAuth, userRepo, jwtManager)
|
|
authHandler := auth.NewHandler(authService)
|
|
|
|
// Auth middleware
|
|
authMW := middleware.Auth(&jwtAdapter{jm: jwtManager})
|
|
|
|
// External API clients
|
|
geminiClient := gemini.NewClient(cfg.OpenAIAPIKey)
|
|
pexelsClient := pexels.NewClient(cfg.PexelsAPIKey)
|
|
|
|
// Ingredient domain
|
|
ingredientRepo := ingredient.NewRepository(pool)
|
|
ingredientHandler := ingredient.NewHandler(ingredientRepo)
|
|
|
|
// Product domain
|
|
productRepo := product.NewRepository(pool)
|
|
productHandler := product.NewHandler(productRepo)
|
|
|
|
// Recognition domain
|
|
recognitionHandler := recognition.NewHandler(geminiClient, ingredientRepo)
|
|
|
|
// Recommendation domain
|
|
recommendationHandler := recommendation.NewHandler(geminiClient, pexelsClient, userRepo, productRepo)
|
|
|
|
// Dish domain
|
|
dishRepo := dish.NewRepository(pool)
|
|
dishHandler := dish.NewHandler(dishRepo)
|
|
|
|
// Recipe domain
|
|
recipeRepo := recipe.NewRepository(pool)
|
|
recipeHandler := recipe.NewHandler(recipeRepo)
|
|
|
|
// Saved recipes domain
|
|
savedRecipeRepo := savedrecipe.NewRepository(pool, dishRepo)
|
|
savedRecipeHandler := savedrecipe.NewHandler(savedRecipeRepo)
|
|
|
|
// Menu domain
|
|
menuRepo := menu.NewRepository(pool)
|
|
menuHandler := menu.NewHandler(menuRepo, geminiClient, pexelsClient, userRepo, productRepo, dishRepo)
|
|
|
|
// Diary domain
|
|
diaryRepo := diary.NewRepository(pool)
|
|
diaryHandler := diary.NewHandler(diaryRepo)
|
|
|
|
// Home domain
|
|
homeHandler := home.NewHandler(pool)
|
|
|
|
// Router
|
|
router := server.NewRouter(
|
|
pool,
|
|
authHandler,
|
|
userHandler,
|
|
recommendationHandler,
|
|
savedRecipeHandler,
|
|
ingredientHandler,
|
|
productHandler,
|
|
recognitionHandler,
|
|
menuHandler,
|
|
diaryHandler,
|
|
homeHandler,
|
|
dishHandler,
|
|
recipeHandler,
|
|
authMW,
|
|
cfg.AllowedOrigins,
|
|
)
|
|
|
|
srv := &http.Server{
|
|
Addr: fmt.Sprintf(":%d", cfg.Port),
|
|
Handler: router,
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 120 * time.Second, // menu generation can take ~60s
|
|
IdleTimeout: 60 * time.Second,
|
|
}
|
|
|
|
go func() {
|
|
slog.Info("server starting", "port", cfg.Port)
|
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
slog.Error("server error", "err", err)
|
|
}
|
|
}()
|
|
|
|
<-ctx.Done()
|
|
slog.Info("shutting down...")
|
|
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
return srv.Shutdown(shutdownCtx)
|
|
}
|