Kafka consumers and WorkerPool are moved out of the server process into a dedicated worker binary. Server now handles HTTP + SSE only; worker handles Kafka consumption and AI processing. - cmd/worker/main.go + init.go: new binary with minimal config (DATABASE_URL, OPENAI_API_KEY, KAFKA_BROKERS) - cmd/server: remove WorkerPool, paidConsumer, freeConsumer - Dockerfile: builds both server and worker binaries - docker-compose.yml: add worker service - Makefile: add run-worker and docker-logs-worker targets - README.md: document worker startup and env vars Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"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 {
|
|
workerConfig, configError := loadConfig()
|
|
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, workerConfig.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)
|
|
}
|
|
|
|
workerApp, initError := initWorker(workerConfig, pool)
|
|
if initError != nil {
|
|
return fmt.Errorf("init worker: %w", initError)
|
|
}
|
|
|
|
workerApp.Start(applicationContext)
|
|
slog.Info("worker started")
|
|
|
|
<-applicationContext.Done()
|
|
slog.Info("worker shutting down...")
|
|
return nil
|
|
}
|