Files
food-ai/backend/cmd/server/main.go
dbastrikin 39193ec13c feat: async dish recognition (Kafka/Watermill/SSE) + remove Wire + consolidate migrations
Async recognition pipeline:
- POST /ai/recognize-dish → 202 {job_id, queue_position, estimated_seconds}
- GET /ai/jobs/{id}/stream — SSE stream: queued → processing → done/failed
- Kafka topics: ai.recognize.paid (3 partitions) + ai.recognize.free (1 partition)
- 5-worker WorkerPool with priority loop (paid consumers first)
- SSEBroker via PostgreSQL LISTEN/NOTIFY
- Kafka adapter migrated from franz-go to Watermill (watermill-kafka/v2)
- Docker Compose: added Kafka + Zookeeper + kafka-init service
- Flutter: recognition_service.dart uses SSE; home_screen shows live job status

Remove google/wire (archived):
- Deleted wire.go (wireinject spec) and wire_gen.go
- Added cmd/server/init.go — plain Go manual DI, same initApp() logic
- Removed github.com/google/wire from go.mod

Consolidate migrations:
- Merged 001_initial_schema + 002_seed_data + 003_recognition_jobs into single 001_initial_schema.sql
- Deleted 002_seed_data.sql and 003_recognition_jobs.sql

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-18 16:32:06 +02:00

81 lines
2.0 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"
)
func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelInfo,
}))
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)
}