Backend: - gemini/client.go: refactor to shared callGroq transport; add generateVisionContent using llama-3.2-11b-vision-preview model - gemini/recognition.go: RecognizeReceipt, RecognizeProducts, RecognizeDish (vision), ClassifyIngredient (text); shared parseJSON helper - ingredient/repository.go: add FuzzyMatch (wraps Search, returns best hit) - recognition/handler.go: POST /ai/recognize-receipt, /ai/recognize-products, /ai/recognize-dish; enrichItems with fuzzy match + AI classify fallback; parallel multi-image processing with deduplication - server.go + main.go: wire recognition handler under /ai routes Flutter: - pubspec.yaml: add image_picker ^1.1.0 - AndroidManifest.xml: add CAMERA and READ_EXTERNAL_STORAGE permissions - Info.plist: add NSCameraUsageDescription and NSPhotoLibraryUsageDescription - recognition_service.dart: RecognitionService wrapping /ai/* endpoints; RecognizedItem, ReceiptResult, DishResult models - scan_screen.dart: mode selector (receipt / products / dish / manual); image source picker; loading overlay; navigates to confirm or dish screen - recognition_confirm_screen.dart: editable list of recognized items; inline qty/unit editing; swipe-to-delete; batch-add to pantry - dish_result_screen.dart: dish name, KBZHU breakdown, similar dishes chips - app_router.dart: /scan, /scan/confirm, /scan/dish routes (no bottom nav) - products_screen.dart: FAB now shows bottom sheet with Manual / Scan options Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
102 lines
2.8 KiB
Go
102 lines
2.8 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/food-ai/backend/internal/auth"
|
|
"github.com/food-ai/backend/internal/ingredient"
|
|
"github.com/food-ai/backend/internal/middleware"
|
|
"github.com/food-ai/backend/internal/product"
|
|
"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/user"
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
func NewRouter(
|
|
pool *pgxpool.Pool,
|
|
authHandler *auth.Handler,
|
|
userHandler *user.Handler,
|
|
recommendationHandler *recommendation.Handler,
|
|
savedRecipeHandler *savedrecipe.Handler,
|
|
ingredientHandler *ingredient.Handler,
|
|
productHandler *product.Handler,
|
|
recognitionHandler *recognition.Handler,
|
|
authMiddleware func(http.Handler) http.Handler,
|
|
allowedOrigins []string,
|
|
) *chi.Mux {
|
|
r := chi.NewRouter()
|
|
|
|
// Global middleware
|
|
r.Use(middleware.RequestID)
|
|
r.Use(middleware.Logging)
|
|
r.Use(middleware.Recovery)
|
|
r.Use(middleware.CORS(allowedOrigins))
|
|
|
|
// Public
|
|
r.Get("/health", healthCheck(pool))
|
|
r.Route("/auth", func(r chi.Router) {
|
|
r.Post("/login", authHandler.Login)
|
|
r.Post("/refresh", authHandler.Refresh)
|
|
r.Post("/logout", authHandler.Logout)
|
|
})
|
|
|
|
// Public search (still requires auth to prevent scraping)
|
|
r.Group(func(r chi.Router) {
|
|
r.Use(authMiddleware)
|
|
r.Get("/ingredients/search", ingredientHandler.Search)
|
|
})
|
|
|
|
// Protected
|
|
r.Group(func(r chi.Router) {
|
|
r.Use(authMiddleware)
|
|
|
|
r.Get("/profile", userHandler.Get)
|
|
r.Put("/profile", userHandler.Update)
|
|
|
|
r.Get("/recommendations", recommendationHandler.GetRecommendations)
|
|
|
|
r.Route("/saved-recipes", func(r chi.Router) {
|
|
r.Post("/", savedRecipeHandler.Save)
|
|
r.Get("/", savedRecipeHandler.List)
|
|
r.Get("/{id}", savedRecipeHandler.GetByID)
|
|
r.Delete("/{id}", savedRecipeHandler.Delete)
|
|
})
|
|
|
|
r.Route("/products", func(r chi.Router) {
|
|
r.Get("/", productHandler.List)
|
|
r.Post("/", productHandler.Create)
|
|
r.Post("/batch", productHandler.BatchCreate)
|
|
r.Put("/{id}", productHandler.Update)
|
|
r.Delete("/{id}", productHandler.Delete)
|
|
})
|
|
|
|
r.Route("/ai", func(r chi.Router) {
|
|
r.Post("/recognize-receipt", recognitionHandler.RecognizeReceipt)
|
|
r.Post("/recognize-products", recognitionHandler.RecognizeProducts)
|
|
r.Post("/recognize-dish", recognitionHandler.RecognizeDish)
|
|
})
|
|
})
|
|
|
|
return r
|
|
}
|
|
|
|
func healthCheck(pool *pgxpool.Pool) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
dbStatus := "connected"
|
|
if err := pool.Ping(r.Context()); err != nil {
|
|
dbStatus = "disconnected"
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(map[string]string{
|
|
"status": "ok",
|
|
"version": "0.1.0",
|
|
"db": dbStatus,
|
|
})
|
|
}
|
|
}
|