update cart
Some checks failed
Build and Publish / BuildAndDeploy (push) Successful in 3m12s
Build and Publish / BuildAndDeployAmd64 (push) Has been cancelled

This commit is contained in:
2025-09-28 15:56:40 +02:00
parent b4c9d09657
commit d98122756a
3 changed files with 316 additions and 19 deletions

44
main.go
View File

@@ -8,6 +8,7 @@ import (
"net/http/pprof"
"os"
"os/signal"
"strings"
"syscall"
"time"
@@ -127,16 +128,33 @@ var tpl = `<!DOCTYPE html>
</html>
`
func main() {
baseUrl := os.Getenv("BASE_URL")
func getCountryFromHost(host string) string {
if strings.Contains(strings.ToLower(host), "-no") {
return "no"
}
return "se"
}
func getCheckoutOrder(host string, cartId CartId) *messages.CreateCheckoutOrder {
baseUrl := fmt.Sprintf("https://%s", host)
cartBaseUrl := os.Getenv("CART_BASE_URL")
if cartBaseUrl == "" {
cartBaseUrl = "https://cart.tornberg.me"
}
if baseUrl == "" {
baseUrl = "https://slask-finder.tornberg.me"
country := getCountryFromHost(host)
return &messages.CreateCheckoutOrder{
Terms: fmt.Sprintf("%s/terms", baseUrl),
Checkout: fmt.Sprintf("%s/checkout?order_id={checkout.order.id}", baseUrl),
Confirmation: fmt.Sprintf("%s/confirmation/{checkout.order.id}", baseUrl),
Validation: fmt.Sprintf("%s/validation", cartBaseUrl),
Push: fmt.Sprintf("%s/push?order_id={checkout.order.id}", cartBaseUrl),
Country: country,
}
// Create a new instance of the server
}
func main() {
storage, err := NewDiskStorage(fmt.Sprintf("data/%s_state.gob", name))
if err != nil {
log.Printf("Error loading state: %v\n", err)
@@ -206,12 +224,11 @@ func main() {
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
})
mux.HandleFunc("/checkout", func(w http.ResponseWriter, r *http.Request) {
orderId := r.URL.Query().Get("order_id")
order := &CheckoutOrder{}
country := "se"
log.Printf("host: %s, referer: %s", r.Host, r.Referer())
log.Printf("Checkout for country %s, method: %s, order_id: %s", country, r.Method, orderId)
if orderId == "" {
cookie, err := r.Cookie("cartid")
if err != nil {
@@ -226,15 +243,8 @@ func main() {
}
cartId := ToCartId(cookie.Value)
reply, err := syncedServer.pool.Process(cartId, Message{
Type: CreateCheckoutOrderType,
Content: &messages.CreateCheckoutOrder{
Terms: fmt.Sprintf("%s/terms", baseUrl),
Checkout: fmt.Sprintf("%s/checkout?order_id={checkout.order.id}", baseUrl),
Confirmation: fmt.Sprintf("%s/confirmation/{checkout.order.id}", baseUrl),
Validation: fmt.Sprintf("%s/validation", cartBaseUrl),
Push: fmt.Sprintf("%s/push?order_id={checkout.order.id}", cartBaseUrl),
Country: country,
},
Type: CreateCheckoutOrderType,
Content: getCheckoutOrder(r.Host, cartId),
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)