Files
go-cart-actor/cmd/backoffice/main.go
T
mats 81569b051a
Build and Publish / BuildAndDeployAmd64 (push) Failing after 5s
Build and Publish / BuildAndDeployArm64 (push) Failing after 14s
http and common
2026-07-18 23:23:27 +02:00

82 lines
2.2 KiB
Go

package main
import (
"context"
"log"
"net/http"
"os"
"time"
"git.k6n.net/mats/go-cart-actor/pkg/backofficeadmin"
"git.k6n.net/mats/platform/config"
"git.k6n.net/mats/platform/httpclient"
"git.k6n.net/mats/platform/httpserver"
"git.k6n.net/mats/platform/rabbit"
)
func main() {
addr := config.EnvString("ADDR", ":8080")
amqpURL := os.Getenv("AMQP_URL")
app, err := backofficeadmin.New(backofficeadmin.Config{
DataDir: config.EnvString("CART_DIR", "data"),
CheckoutDataDir: config.EnvString("CHECKOUT_DIR", "checkout-data"),
})
if err != nil {
log.Fatalf("Error creating backoffice: %v", err)
}
mux := http.NewServeMux()
app.RegisterRoutes(mux)
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
})
mux.HandleFunc("/readyz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
})
mux.HandleFunc("/livez", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
})
// Global CORS middleware allowing all origins and handling preflight.
srv := &http.Server{
Addr: addr,
Handler: httpclient.CORS(
httpclient.WithOrigin("*"),
httpclient.WithMethods("GET, POST, PUT, PATCH, DELETE, OPTIONS"),
httpclient.WithHeaders("Content-Type, Authorization, X-Requested-With"),
httpclient.WithExposeHeaders("*"),
)(mux),
ReadTimeout: 15 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 60 * time.Second,
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var conn *rabbit.Conn
if amqpURL != "" {
conn, err = rabbit.Dial(amqpURL, "cart-backoffice")
if err != nil {
log.Fatalf("failed to connect to RabbitMQ: %v", err)
}
defer conn.Close()
}
if err := app.Start(ctx, conn); err != nil {
log.Printf("AMQP listener disabled: %v", err)
} else if conn != nil {
log.Printf("AMQP listener connected")
}
log.Printf("backoffice HTTP listening on %s", addr)
if err := httpserver.RunServerWithShutdown(srv, "cart-backoffice", 10*time.Second, 5*time.Second,
func(context.Context) error { cancel(); return nil },
); err != nil {
log.Fatalf("server: %v", err)
}
}