DDD Meilisearch showcase: domain/app/infra/interfaces, Taskfile, tests
This commit is contained in:
commit
a9674ce14e
22 files changed
+1584
No files matched your search
@@ -0,0 +1,59 @@
|
||||
// Command api serves the catalog HTTP API backed by Meilisearch.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
stdhttp "net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.jokresner.de/johannes/meilisearch-showcase/internal/app"
|
||||
"git.jokresner.de/johannes/meilisearch-showcase/internal/infra/config"
|
||||
"git.jokresner.de/johannes/meilisearch-showcase/internal/infra/meili"
|
||||
httpiface "git.jokresner.de/johannes/meilisearch-showcase/internal/interfaces/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log := slog.New(slog.NewTextHandler(os.Stdout, nil))
|
||||
cfg := config.Load()
|
||||
|
||||
idx := meili.NewMovieIndex(cfg.MeiliHost, cfg.MeiliKey, cfg.IndexUID)
|
||||
defer idx.Close()
|
||||
svc := app.NewCatalogService(idx)
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
setupCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
if err := svc.Setup(setupCtx); err != nil {
|
||||
log.Error("index setup failed", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
srv := &stdhttp.Server{
|
||||
Addr: cfg.HTTPAddr,
|
||||
Handler: httpiface.NewHandler(svc, log).Routes(),
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
}
|
||||
|
||||
go func() {
|
||||
log.Info("listening", "addr", cfg.HTTPAddr, "meili", cfg.MeiliHost, "index", cfg.IndexUID)
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, stdhttp.ErrServerClosed) {
|
||||
log.Error("server failed", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}()
|
||||
|
||||
<-ctx.Done()
|
||||
shutdownCtx, cancelShutdown := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancelShutdown()
|
||||
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||||
log.Error("shutdown", "err", err)
|
||||
}
|
||||
log.Info("stopped")
|
||||
}
|
||||
Reference in new issue
Block a user