From ea173260b98b0e71038334c9c8957d8a70813729 Mon Sep 17 00:00:00 2001 From: matst80 Date: Tue, 13 May 2025 18:02:57 +0200 Subject: [PATCH] test checkout --- main.go | 109 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 64 insertions(+), 45 deletions(-) diff --git a/main.go b/main.go index d6403a4..9199667 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,13 @@ package main import ( "encoding/json" "fmt" + messages "git.tornberg.me/go-cart-actor/proto" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/prometheus/client_golang/prometheus/promhttp" + "html/template" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" "log" "net/http" "net/http/pprof" @@ -10,13 +17,6 @@ import ( "os/signal" "syscall" "time" - - messages "git.tornberg.me/go-cart-actor/proto" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promauto" - "github.com/prometheus/client_golang/prometheus/promhttp" - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/rest" ) var ( @@ -113,6 +113,20 @@ func GetDiscovery() Discovery { return NewK8sDiscovery(client) } +var tpl = ` + + + + + s10r testing - checkout + + + + {{ .HTMLSnippet }} + + +` + func main() { // Create a new instance of the server storage, err := NewDiskStorage(fmt.Sprintf("data/%s_state.gob", name)) @@ -185,6 +199,49 @@ func main() { w.WriteHeader(http.StatusOK) w.Write([]byte("ok")) }) + mux.HandleFunc("/checkout", func(w http.ResponseWriter, r *http.Request) { + cookie, err := r.Cookie("cartid") + if err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(err.Error())) + return + } + if cookie.Value == "" { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("no cart id to checkout is empty")) + return + } + cartId := ToCartId(cookie.Value) + reply, err := syncedServer.pool.Process(cartId, Message{ + Type: CreateCheckoutOrderType, + Content: &messages.CreateCheckoutOrder{ + Terms: "https://slask-finder.tornberg.me/terms", + Checkout: "https://slask-finder.tornberg.me/checkout?order_id={checkout.order.id}", + Confirmation: "https://slask-finder.tornberg.me/confirmation/{checkout.order.id}", + Push: "https://cart.tornberg.me/push?order_id={checkout.order.id}", + }, + }) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + } + order := CheckoutOrder{} + err = json.Unmarshal(reply.Payload, &order) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + t, err := template.New("checkout").Parse(tpl) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + } + err = t.Execute(w, &order) + if err != nil { + log.Printf("Error executing template: %v\n", err) + } + }) mux.HandleFunc("/push", func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { @@ -241,44 +298,6 @@ func main() { w.Write([]byte("1.0.0")) }) - // l, err := net.Listen("tcp", ":1234") - // if err == nil { - - // go func(listener net.Listener) { - // conn, err := l.Accept() - // log.Printf("Accepted connection from %s\n", conn.RemoteAddr()) - // if err != nil { - // log.Fatalf("Error accepting echo connection: %v\n", err) - // } - // go func(c net.Conn) { - // defer c.Close() - // buf := make([]byte, 1024) - // for { - // n, err := c.Read(buf) - // if err != nil { - // log.Printf("Error reading from connection: %v\n", err) - // break - // } - // if n == 0 { - // break - // } - // str := string(buf[:n]) - // if strings.HasPrefix(str, "exit") { - // break - // } - // log.Println("Echoing", str) - // _, err = c.Write([]byte(str)) - // if err != nil { - // log.Printf("Error writing to connection: %v\n", err) - // break - // } - // } - // }(conn) - // }(l) - // } else { - // log.Printf("Error creating echo server: %v\n", err) - // } - sigs := make(chan os.Signal, 1) done := make(chan bool, 1) signal.Notify(sigs, syscall.SIGTERM)