http and common
This commit is contained in:
+13
-16
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -10,6 +9,8 @@ import (
|
|||||||
|
|
||||||
"git.k6n.net/mats/go-cart-actor/pkg/backofficeadmin"
|
"git.k6n.net/mats/go-cart-actor/pkg/backofficeadmin"
|
||||||
"git.k6n.net/mats/platform/config"
|
"git.k6n.net/mats/platform/config"
|
||||||
|
"git.k6n.net/mats/platform/httpclient"
|
||||||
|
"git.k6n.net/mats/platform/httpserver"
|
||||||
"git.k6n.net/mats/platform/rabbit"
|
"git.k6n.net/mats/platform/rabbit"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -41,21 +42,14 @@ func main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Global CORS middleware allowing all origins and handling preflight.
|
// Global CORS middleware allowing all origins and handling preflight.
|
||||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
|
|
||||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With")
|
|
||||||
w.Header().Set("Access-Control-Expose-Headers", "*")
|
|
||||||
if r.Method == http.MethodOptions {
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
mux.ServeHTTP(w, r)
|
|
||||||
})
|
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: addr,
|
Addr: addr,
|
||||||
Handler: handler,
|
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,
|
ReadTimeout: 15 * time.Second,
|
||||||
WriteTimeout: 30 * time.Second,
|
WriteTimeout: 30 * time.Second,
|
||||||
IdleTimeout: 60 * time.Second,
|
IdleTimeout: 60 * time.Second,
|
||||||
@@ -70,6 +64,7 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to connect to RabbitMQ: %v", err)
|
log.Fatalf("failed to connect to RabbitMQ: %v", err)
|
||||||
}
|
}
|
||||||
|
defer conn.Close()
|
||||||
}
|
}
|
||||||
if err := app.Start(ctx, conn); err != nil {
|
if err := app.Start(ctx, conn); err != nil {
|
||||||
log.Printf("AMQP listener disabled: %v", err)
|
log.Printf("AMQP listener disabled: %v", err)
|
||||||
@@ -78,7 +73,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("backoffice HTTP listening on %s", addr)
|
log.Printf("backoffice HTTP listening on %s", addr)
|
||||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
if err := httpserver.RunServerWithShutdown(srv, "cart-backoffice", 10*time.Second, 5*time.Second,
|
||||||
log.Fatalf("http server error: %v", err)
|
func(context.Context) error { cancel(); return nil },
|
||||||
|
); err != nil {
|
||||||
|
log.Fatalf("server: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-32
@@ -5,7 +5,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/pprof"
|
"net/http/pprof"
|
||||||
"os"
|
"os"
|
||||||
@@ -26,6 +25,7 @@ import (
|
|||||||
"git.k6n.net/mats/platform/catalog"
|
"git.k6n.net/mats/platform/catalog"
|
||||||
"git.k6n.net/mats/platform/config"
|
"git.k6n.net/mats/platform/config"
|
||||||
"git.k6n.net/mats/platform/event"
|
"git.k6n.net/mats/platform/event"
|
||||||
|
"git.k6n.net/mats/platform/httpserver"
|
||||||
"git.k6n.net/mats/platform/rabbit"
|
"git.k6n.net/mats/platform/rabbit"
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
@@ -524,9 +524,11 @@ func main() {
|
|||||||
httpAddr := normalizeListenAddr(cartPort)
|
httpAddr := normalizeListenAddr(cartPort)
|
||||||
debugAddr := normalizeListenAddr(config.EnvString("CART_DEBUG_PORT", "8081"))
|
debugAddr := normalizeListenAddr(config.EnvString("CART_DEBUG_PORT", "8081"))
|
||||||
|
|
||||||
|
// Debug mux on separate port (metrics + pprof).
|
||||||
|
go http.ListenAndServe(debugAddr, debugMux)
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: httpAddr,
|
Addr: httpAddr,
|
||||||
BaseContext: func(net.Listener) context.Context { return ctx },
|
|
||||||
ReadTimeout: 10 * time.Second,
|
ReadTimeout: 10 * time.Second,
|
||||||
// Close idle keep-alive connections so a load test with many short-lived
|
// Close idle keep-alive connections so a load test with many short-lived
|
||||||
// clients doesn't pin file handles open indefinitely.
|
// clients doesn't pin file handles open indefinitely.
|
||||||
@@ -535,38 +537,15 @@ func main() {
|
|||||||
Handler: otelhttp.NewHandler(mux, "/"),
|
Handler: otelhttp.NewHandler(mux, "/"),
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
|
||||||
|
|
||||||
fmt.Println("Shutting down due to signal")
|
|
||||||
otelShutdown(context.Background())
|
|
||||||
diskStorage.Close()
|
|
||||||
pool.Close()
|
|
||||||
|
|
||||||
}()
|
|
||||||
|
|
||||||
srvErr := make(chan error, 1)
|
|
||||||
go func() {
|
|
||||||
srvErr <- srv.ListenAndServe()
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Inventory change consumption used to live here over the bare Redis
|
|
||||||
// `inventory_changed` channel; it is now owned by the cart-inventory
|
|
||||||
// service, which translates quantity changes into inventory.level_changed
|
|
||||||
// bus crossings. The cart reads exact stock synchronously when it needs it.
|
|
||||||
// See docs/inventory-shape.md.
|
|
||||||
|
|
||||||
log.Printf("Server started at %s (debug %s)", httpAddr, debugAddr)
|
log.Printf("Server started at %s (debug %s)", httpAddr, debugAddr)
|
||||||
|
|
||||||
go http.ListenAndServe(debugAddr, debugMux)
|
if err := httpserver.RunServerWithShutdown(srv, "cart", 15*time.Second, 5*time.Second,
|
||||||
|
func(context.Context) error { stop(); return nil },
|
||||||
select {
|
func(context.Context) error { otelShutdown(context.Background()); return nil },
|
||||||
case err = <-srvErr:
|
func(context.Context) error { diskStorage.Close(); return nil },
|
||||||
// Error when starting HTTP server.
|
func(context.Context) error { pool.Close(); return nil },
|
||||||
log.Fatalf("Unable to start server: %v", err)
|
); err != nil {
|
||||||
case <-ctx.Done():
|
log.Fatalf("server: %v", err)
|
||||||
// Wait for first CTRL+C.
|
|
||||||
// Stop receiving signal notifications as soon as possible.
|
|
||||||
stop()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,14 +55,6 @@ func getOriginalHost(r *http.Request) string {
|
|||||||
return r.Host
|
return r.Host
|
||||||
}
|
}
|
||||||
|
|
||||||
func getClientIp(r *http.Request) string {
|
|
||||||
ip := r.Header.Get("X-Forwarded-For")
|
|
||||||
if ip == "" {
|
|
||||||
ip = r.RemoteAddr
|
|
||||||
}
|
|
||||||
return ip
|
|
||||||
}
|
|
||||||
|
|
||||||
func CookieCartIdHandler(fn func(cartId cart.CartId, w http.ResponseWriter, r *http.Request) error) func(w http.ResponseWriter, r *http.Request) {
|
func CookieCartIdHandler(fn func(cartId cart.CartId, w http.ResponseWriter, r *http.Request) error) func(w http.ResponseWriter, r *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"git.k6n.net/mats/go-cart-actor/pkg/cart"
|
"git.k6n.net/mats/go-cart-actor/pkg/cart"
|
||||||
"git.k6n.net/mats/go-cart-actor/pkg/checkout"
|
"git.k6n.net/mats/go-cart-actor/pkg/checkout"
|
||||||
|
"git.k6n.net/mats/platform/httpclient"
|
||||||
"git.k6n.net/mats/platform/tax"
|
"git.k6n.net/mats/platform/tax"
|
||||||
adyenCheckout "github.com/adyen/adyen-go-api-library/v21/src/checkout"
|
adyenCheckout "github.com/adyen/adyen-go-api-library/v21/src/checkout"
|
||||||
"github.com/adyen/adyen-go-api-library/v21/src/common"
|
"github.com/adyen/adyen-go-api-library/v21/src/common"
|
||||||
@@ -233,7 +234,7 @@ func GetCheckoutMetaFromRequest(r *http.Request) *CheckoutMeta {
|
|||||||
siteUrl = checkoutPublicURL
|
siteUrl = checkoutPublicURL
|
||||||
}
|
}
|
||||||
return &CheckoutMeta{
|
return &CheckoutMeta{
|
||||||
ClientIp: getClientIp(r),
|
ClientIp: httpclient.ClientIP(r),
|
||||||
SiteUrl: siteUrl,
|
SiteUrl: siteUrl,
|
||||||
CallbackBaseUrl: checkoutCallbackBaseURL,
|
CallbackBaseUrl: checkoutCallbackBaseURL,
|
||||||
Country: country,
|
Country: country,
|
||||||
|
|||||||
+11
-21
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@@ -17,6 +16,7 @@ import (
|
|||||||
"git.k6n.net/mats/go-cart-actor/pkg/telemetry"
|
"git.k6n.net/mats/go-cart-actor/pkg/telemetry"
|
||||||
redisinv "git.k6n.net/mats/go-redis-inventory/pkg/inventory"
|
redisinv "git.k6n.net/mats/go-redis-inventory/pkg/inventory"
|
||||||
"git.k6n.net/mats/platform/config"
|
"git.k6n.net/mats/platform/config"
|
||||||
|
"git.k6n.net/mats/platform/httpserver"
|
||||||
"git.k6n.net/mats/platform/rabbit"
|
"git.k6n.net/mats/platform/rabbit"
|
||||||
"git.k6n.net/mats/platform/tax"
|
"git.k6n.net/mats/platform/tax"
|
||||||
"github.com/adyen/adyen-go-api-library/v21/src/adyen"
|
"github.com/adyen/adyen-go-api-library/v21/src/adyen"
|
||||||
@@ -293,35 +293,25 @@ func main() {
|
|||||||
w.Write([]byte("1.0.0"))
|
w.Write([]byte("1.0.0"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Debug mux on separate port (metrics + pprof).
|
||||||
|
go http.ListenAndServe(":8081", debugMux)
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: ":8080",
|
Addr: ":8080",
|
||||||
BaseContext: func(net.Listener) context.Context { return ctx },
|
|
||||||
ReadTimeout: 10 * time.Second,
|
ReadTimeout: 10 * time.Second,
|
||||||
WriteTimeout: 20 * time.Second,
|
WriteTimeout: 20 * time.Second,
|
||||||
Handler: otelhttp.NewHandler(mux, "/"),
|
Handler: otelhttp.NewHandler(mux, "/"),
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
|
||||||
fmt.Println("Shutting down due to signal")
|
|
||||||
otelShutdown(context.Background())
|
|
||||||
diskStorage.Close()
|
|
||||||
pool.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
srvErr := make(chan error, 1)
|
|
||||||
go func() {
|
|
||||||
srvErr <- srv.ListenAndServe()
|
|
||||||
}()
|
|
||||||
|
|
||||||
log.Print("Checkout server started at port 8080")
|
log.Print("Checkout server started at port 8080")
|
||||||
|
|
||||||
go http.ListenAndServe(":8081", debugMux)
|
if err := httpserver.RunServerWithShutdown(srv, "checkout", 15*time.Second, 5*time.Second,
|
||||||
|
func(context.Context) error { stop(); return nil },
|
||||||
select {
|
func(context.Context) error { otelShutdown(context.Background()); return nil },
|
||||||
case err = <-srvErr:
|
func(context.Context) error { diskStorage.Close(); return nil },
|
||||||
log.Fatalf("Unable to start server: %v", err)
|
func(context.Context) error { pool.Close(); return nil },
|
||||||
case <-ctx.Done():
|
); err != nil {
|
||||||
stop()
|
log.Fatalf("server: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,14 +36,6 @@ func getScheme(r *http.Request) string {
|
|||||||
return "https"
|
return "https"
|
||||||
}
|
}
|
||||||
|
|
||||||
func getClientIp(r *http.Request) string {
|
|
||||||
ip := r.Header.Get("X-Forwarded-For")
|
|
||||||
if ip == "" {
|
|
||||||
ip = r.RemoteAddr
|
|
||||||
}
|
|
||||||
return ip
|
|
||||||
}
|
|
||||||
|
|
||||||
func getCurrency(country string) string {
|
func getCurrency(country string) string {
|
||||||
if country == "no" {
|
if country == "no" {
|
||||||
return "NOK"
|
return "NOK"
|
||||||
|
|||||||
+11
-22
@@ -4,9 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
"os"
|
"os"
|
||||||
@@ -22,6 +20,7 @@ import (
|
|||||||
"git.k6n.net/mats/go-cart-actor/pkg/proxy"
|
"git.k6n.net/mats/go-cart-actor/pkg/proxy"
|
||||||
"git.k6n.net/mats/go-cart-actor/pkg/telemetry"
|
"git.k6n.net/mats/go-cart-actor/pkg/telemetry"
|
||||||
"git.k6n.net/mats/platform/config"
|
"git.k6n.net/mats/platform/config"
|
||||||
|
"git.k6n.net/mats/platform/httpserver"
|
||||||
"git.k6n.net/mats/platform/rabbit"
|
"git.k6n.net/mats/platform/rabbit"
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
||||||
@@ -311,35 +310,25 @@ func main() {
|
|||||||
w.Write([]byte("1.0.0"))
|
w.Write([]byte("1.0.0"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Debug mux on separate port (metrics + pprof).
|
||||||
|
go http.ListenAndServe(":8081", debugMux)
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: config.EnvString("PROFILE_ADDR", ":8080"),
|
Addr: config.EnvString("PROFILE_ADDR", ":8080"),
|
||||||
BaseContext: func(net.Listener) context.Context { return ctx },
|
|
||||||
ReadTimeout: 10 * time.Second,
|
ReadTimeout: 10 * time.Second,
|
||||||
WriteTimeout: 20 * time.Second,
|
WriteTimeout: 20 * time.Second,
|
||||||
Handler: otelhttp.NewHandler(mux, "/"),
|
Handler: otelhttp.NewHandler(mux, "/"),
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
|
||||||
fmt.Println("Shutting down profile service")
|
|
||||||
otelShutdown(context.Background())
|
|
||||||
diskStorage.Close()
|
|
||||||
pool.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
srvErr := make(chan error, 1)
|
|
||||||
go func() {
|
|
||||||
srvErr <- srv.ListenAndServe()
|
|
||||||
}()
|
|
||||||
|
|
||||||
log.Print("Profile server started at port 8080")
|
log.Print("Profile server started at port 8080")
|
||||||
|
|
||||||
go http.ListenAndServe(":8081", debugMux)
|
if err := httpserver.RunServerWithShutdown(srv, "profile", 15*time.Second, 5*time.Second,
|
||||||
|
func(context.Context) error { stop(); return nil },
|
||||||
select {
|
func(context.Context) error { otelShutdown(context.Background()); return nil },
|
||||||
case err = <-srvErr:
|
func(context.Context) error { diskStorage.Close(); return nil },
|
||||||
log.Fatalf("Unable to start server: %v", err)
|
func(context.Context) error { pool.Close(); return nil },
|
||||||
case <-ctx.Done():
|
); err != nil {
|
||||||
stop()
|
log.Fatalf("server: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,15 @@ func NewPublic(store *promotions.Store, eval *promotions.PromotionService) *Serv
|
|||||||
|
|
||||||
func (s *Server) buildPublicTools() []pmcp.Tool {
|
func (s *Server) buildPublicTools() []pmcp.Tool {
|
||||||
return []pmcp.Tool{
|
return []pmcp.Tool{
|
||||||
{
|
s.toolListPromotions(),
|
||||||
|
s.toolGetPromotion(),
|
||||||
|
s.toolPreviewCartDiscount(),
|
||||||
|
s.toolEvaluatePromotion(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) toolListPromotions() pmcp.Tool {
|
||||||
|
return pmcp.Tool{
|
||||||
Name: "list_promotions",
|
Name: "list_promotions",
|
||||||
Description: "List all promotion rules the cart engine evaluates, optionally filtered by status (active|inactive|scheduled|expired).",
|
Description: "List all promotion rules the cart engine evaluates, optionally filtered by status (active|inactive|scheduled|expired).",
|
||||||
InputSchema: pmcp.Object(pmcp.Props{
|
InputSchema: pmcp.Object(pmcp.Props{
|
||||||
@@ -50,8 +58,11 @@ func (s *Server) buildPublicTools() []pmcp.Tool {
|
|||||||
}
|
}
|
||||||
return map[string]any{"promotions": rules, "count": len(rules)}, nil
|
return map[string]any{"promotions": rules, "count": len(rules)}, nil
|
||||||
},
|
},
|
||||||
},
|
}
|
||||||
{
|
}
|
||||||
|
|
||||||
|
func (s *Server) toolGetPromotion() pmcp.Tool {
|
||||||
|
return pmcp.Tool{
|
||||||
Name: "get_promotion",
|
Name: "get_promotion",
|
||||||
Description: "Fetch a single promotion rule by id.",
|
Description: "Fetch a single promotion rule by id.",
|
||||||
InputSchema: pmcp.Object(pmcp.Props{"id": pmcp.String("the promotion id")}, []string{"id"}),
|
InputSchema: pmcp.Object(pmcp.Props{"id": pmcp.String("the promotion id")}, []string{"id"}),
|
||||||
@@ -68,8 +79,11 @@ func (s *Server) buildPublicTools() []pmcp.Tool {
|
|||||||
}
|
}
|
||||||
return r, nil
|
return r, nil
|
||||||
},
|
},
|
||||||
},
|
}
|
||||||
{
|
}
|
||||||
|
|
||||||
|
func (s *Server) toolPreviewCartDiscount() pmcp.Tool {
|
||||||
|
return pmcp.Tool{
|
||||||
Name: "preview_cart_discount",
|
Name: "preview_cart_discount",
|
||||||
Description: "Evaluate the current active promotions against a hypothetical cart total and report the " +
|
Description: "Evaluate the current active promotions against a hypothetical cart total and report the " +
|
||||||
"matched discount. cartTotalIncVat is in öre incl. VAT (e.g. 5 000 000 = 50 000 kr). Useful for " +
|
"matched discount. cartTotalIncVat is in öre incl. VAT (e.g. 5 000 000 = 50 000 kr). Useful for " +
|
||||||
@@ -104,8 +118,11 @@ func (s *Server) buildPublicTools() []pmcp.Tool {
|
|||||||
"appliedPromotions": g.AppliedPromotions,
|
"appliedPromotions": g.AppliedPromotions,
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
},
|
}
|
||||||
{
|
}
|
||||||
|
|
||||||
|
func (s *Server) toolEvaluatePromotion() pmcp.Tool {
|
||||||
|
return pmcp.Tool{
|
||||||
Name: "evaluate_promotion",
|
Name: "evaluate_promotion",
|
||||||
Description: "Evaluate promotions against a hypothetical cart and return the full evaluation result: " +
|
Description: "Evaluate promotions against a hypothetical cart and return the full evaluation result: " +
|
||||||
"which promotions match, what discount they apply, and any pending next-tier nudges (\"spend X more for Y%\"). " +
|
"which promotions match, what discount they apply, and any pending next-tier nudges (\"spend X more for Y%\"). " +
|
||||||
@@ -170,6 +187,5 @@ func (s *Server) buildPublicTools() []pmcp.Tool {
|
|||||||
"evaluationResults": evalResults,
|
"evaluationResults": evalResults,
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,50 +16,8 @@ import (
|
|||||||
// and the handler that runs it.
|
// and the handler that runs it.
|
||||||
func (s *Server) buildTools() []pmcp.Tool {
|
func (s *Server) buildTools() []pmcp.Tool {
|
||||||
return []pmcp.Tool{
|
return []pmcp.Tool{
|
||||||
{
|
s.toolListPromotions(),
|
||||||
Name: "list_promotions",
|
s.toolGetPromotion(),
|
||||||
Description: "List all promotion rules the cart engine evaluates, optionally filtered by status (active|inactive|scheduled|expired).",
|
|
||||||
InputSchema: pmcp.Object(pmcp.Props{
|
|
||||||
"status": pmcp.String("optional status filter: active|inactive|scheduled|expired"),
|
|
||||||
}, nil),
|
|
||||||
Invoke: func(_ context.Context, args json.RawMessage) (any, error) {
|
|
||||||
var a struct {
|
|
||||||
Status string `json:"status"`
|
|
||||||
}
|
|
||||||
if err := pmcp.Decode(args, &a); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
rules := s.store.List()
|
|
||||||
if a.Status != "" {
|
|
||||||
filtered := rules[:0:0]
|
|
||||||
for _, r := range rules {
|
|
||||||
if string(r.Status) == a.Status {
|
|
||||||
filtered = append(filtered, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rules = filtered
|
|
||||||
}
|
|
||||||
return map[string]any{"promotions": rules, "count": len(rules)}, nil
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "get_promotion",
|
|
||||||
Description: "Fetch a single promotion rule by id.",
|
|
||||||
InputSchema: pmcp.Object(pmcp.Props{"id": pmcp.String("the promotion id")}, []string{"id"}),
|
|
||||||
Invoke: func(_ context.Context, args json.RawMessage) (any, error) {
|
|
||||||
var a struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
}
|
|
||||||
if err := pmcp.Decode(args, &a); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
r, ok := s.store.Get(a.ID)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("promotion %q not found", a.ID)
|
|
||||||
}
|
|
||||||
return r, nil
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
Name: "upsert_promotion",
|
Name: "upsert_promotion",
|
||||||
Description: "Create a promotion or replace the existing one with the same id. " +
|
Description: "Create a promotion or replace the existing one with the same id. " +
|
||||||
@@ -142,42 +100,7 @@ func (s *Server) buildTools() []pmcp.Tool {
|
|||||||
return map[string]any{"id": a.ID, "deleted": true}, nil
|
return map[string]any{"id": a.ID, "deleted": true}, nil
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
s.toolPreviewCartDiscount(),
|
||||||
Name: "preview_cart_discount",
|
|
||||||
Description: "Evaluate the current active promotions against a hypothetical cart total and report the " +
|
|
||||||
"matched discount. cartTotalIncVat is in öre incl. VAT (e.g. 5 000 000 = 50 000 kr). Useful for " +
|
|
||||||
"verifying Volymrabatt tiers without touching a real cart.",
|
|
||||||
InputSchema: pmcp.Object(pmcp.Props{
|
|
||||||
"cartTotalIncVat": pmcp.Integer("cart total incl. VAT, in öre"),
|
|
||||||
"itemQuantity": pmcp.Integer("optional total item quantity"),
|
|
||||||
"customerSegment": pmcp.String("optional customer segment"),
|
|
||||||
}, []string{"cartTotalIncVat"}),
|
|
||||||
Invoke: func(_ context.Context, args json.RawMessage) (any, error) {
|
|
||||||
var a struct {
|
|
||||||
CartTotalIncVat int64 `json:"cartTotalIncVat"`
|
|
||||||
ItemQuantity int `json:"itemQuantity"`
|
|
||||||
CustomerSegment string `json:"customerSegment"`
|
|
||||||
}
|
|
||||||
if err := pmcp.Decode(args, &a); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
g := syntheticCart(a.CartTotalIncVat, a.ItemQuantity, s.defaultVatRate())
|
|
||||||
opts := []promotions.ContextOption{promotions.WithNow(time.Now())}
|
|
||||||
if a.CustomerSegment != "" {
|
|
||||||
opts = append(opts, promotions.WithCustomerSegment(a.CustomerSegment))
|
|
||||||
}
|
|
||||||
ctx := promotions.NewContextFromCart(g, opts...)
|
|
||||||
results, actions := s.eval.EvaluateAll(s.store.Snapshot(), ctx)
|
|
||||||
s.eval.ApplyResults(g, results, ctx)
|
|
||||||
return map[string]any{
|
|
||||||
"subtotalIncVat": a.CartTotalIncVat,
|
|
||||||
"discountIncVat": g.TotalDiscount.IncVat,
|
|
||||||
"netIncVat": g.TotalPrice.IncVat,
|
|
||||||
"matchedActions": actions,
|
|
||||||
"appliedPromotions": g.AppliedPromotions,
|
|
||||||
}, nil
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user