59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
// erpmock — the staging stand-in for ERPNext (decision 18). See
|
|
// internal/erpmock. Configuration:
|
|
//
|
|
// ERPMOCK_ADDR listen address (default :8000)
|
|
// ERP_API_KEY expected `token key:secret` (default mock / mock)
|
|
// ERP_API_SECRET
|
|
// ERP_WEBHOOK_URL registry receiver, e.g. http://tenant-registry:8090/v1/internal/erp/webhook
|
|
// ERP_WEBHOOK_SECRET HMAC secret shared with the registry (ERP_WEBHOOK_SECRET there too)
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"git.breakpilot.com/platform/tenant-registry/internal/erpmock"
|
|
)
|
|
|
|
func main() {
|
|
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
|
slog.SetDefault(logger)
|
|
cfg := erpmock.Config{
|
|
APIKey: env("ERP_API_KEY", "mock"),
|
|
APISecret: env("ERP_API_SECRET", "mock"),
|
|
WebhookURL: os.Getenv("ERP_WEBHOOK_URL"),
|
|
WebhookSecret: os.Getenv("ERP_WEBHOOK_SECRET"),
|
|
}
|
|
addr := env("ERPMOCK_ADDR", ":8000")
|
|
if cfg.WebhookURL == "" {
|
|
slog.Warn("ERP_WEBHOOK_URL not set — /_mock/fire/* simulates events without delivering webhooks")
|
|
}
|
|
srv := &http.Server{Addr: addr, Handler: erpmock.New(cfg), ReadTimeout: 10 * time.Second, WriteTimeout: 30 * time.Second}
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
go func() {
|
|
slog.Info("erpmock listening", "addr", addr, "webhook_url", cfg.WebhookURL)
|
|
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
slog.Error("server crashed", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
}()
|
|
<-ctx.Done()
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
_ = srv.Shutdown(shutdownCtx)
|
|
}
|
|
|
|
func env(k, def string) string {
|
|
if v := os.Getenv(k); v != "" {
|
|
return v
|
|
}
|
|
return def
|
|
}
|