test checkout
Some checks failed
Build and Publish / BuildAndDeploy (push) Failing after 3m16s
Build and Publish / BuildAndDeployAmd64 (push) Has been cancelled

This commit is contained in:
matst80
2025-05-13 18:02:57 +02:00
parent f485dfa038
commit ea173260b9

109
main.go
View File

@@ -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 = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>s10r testing - checkout</title>
</head>
<body>
{{ .HTMLSnippet }}
</body>
</html>
`
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)