diff --git a/api-tests/cart.http b/api-tests/cart.http index ed78675..578ca9a 100644 --- a/api-tests/cart.http +++ b/api-tests/cart.http @@ -18,7 +18,7 @@ Content-Type: application/json } ### Delete item from cart -DELETE https://cart.tornberg.me/api/12345/1 +DELETE https://cart.tornberg.me/api/1002/1 ### Set delivery diff --git a/go.mod b/go.mod index ddeea29..9f69bec 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,9 @@ module git.tornberg.me/go-cart-actor go 1.23.3 require ( + github.com/Flaconi/go-klarna v0.0.0-20230216165926-e2f708c721d9 github.com/prometheus/client_golang v1.20.5 + golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f google.golang.org/protobuf v1.34.2 k8s.io/api v0.31.2 k8s.io/apimachinery v0.31.2 @@ -11,7 +13,6 @@ require ( ) require ( - github.com/Flaconi/go-klarna v0.0.0-20230216165926-e2f708c721d9 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect diff --git a/go.sum b/go.sum index bbada89..4cdb221 100644 --- a/go.sum +++ b/go.sum @@ -105,6 +105,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo= +golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= @@ -135,8 +137,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg= -golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= +golang.org/x/tools v0.27.0 h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o= +golang.org/x/tools v0.27.0/go.mod h1:sUi0ZgbwW9ZPAq26Ekut+weQPR5eIM6GQLQ1Yjm1H0Q= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/id_test.go b/id_test.go new file mode 100644 index 0000000..d8953b8 --- /dev/null +++ b/id_test.go @@ -0,0 +1,19 @@ +package main + +import ( + "testing" +) + +func TestIdGeneration(t *testing.T) { + // Generate a random ID + id := NewCartId() + + // Generate a random ID + id2 := NewCartId() + // Compare the two IDs + if id == id2 { + t.Errorf("IDs are the same: %v == %v", id, id2) + } else { + t.Log("ID generation test passed", id, id2) + } +} diff --git a/main.go b/main.go index 9648039..78e4671 100644 --- a/main.go +++ b/main.go @@ -142,13 +142,13 @@ func main() { syncedServer := NewPoolServer(syncedPool, fmt.Sprintf("%s, %s", name, podIp)) mux := http.NewServeMux() - mux.Handle("/api/", http.StripPrefix("/api", syncedServer.Serve())) + mux.Handle("/cart/", http.StripPrefix("/cart", syncedServer.Serve())) // only for local // mux.HandleFunc("GET /add/remote/{host}", func(w http.ResponseWriter, r *http.Request) { // syncedPool.AddRemote(r.PathValue("host")) // }) // mux.HandleFunc("GET /save", app.HandleSave) - + //mux.HandleFunc("/", app.RewritePath) mux.HandleFunc("/debug/pprof/", pprof.Index) mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) mux.HandleFunc("/debug/pprof/profile", pprof.Profile) diff --git a/pool-server.go b/pool-server.go index 4565dc6..8ebfbbf 100644 --- a/pool-server.go +++ b/pool-server.go @@ -3,12 +3,16 @@ package main import ( "bytes" "encoding/json" + "fmt" "log" "net/http" "os" "strconv" + "strings" + "time" messages "git.tornberg.me/go-cart-actor/proto" + "golang.org/x/exp/rand" ) type PoolServer struct { @@ -249,8 +253,39 @@ func (s *PoolServer) HandleCheckout(w http.ResponseWriter, r *http.Request) erro return nil } +func NewCartId() CartId { + id := time.Now().UnixNano() + rand.Int63() + + return ToCartId(fmt.Sprintf("%d", id)) +} + +func (a *PoolServer) RewritePath(w http.ResponseWriter, r *http.Request) { + var cartId CartId + if strings.Contains(r.URL.Path, ".") { + http.NotFound(w, r) + return + } + cartIdCookie := r.CookiesNamed("cartid") + if cartIdCookie == nil || len(cartIdCookie) == 0 { + cartId = NewCartId() + http.SetCookie(w, &http.Cookie{ + Name: "cartid", + Value: cartId.String(), + Path: "/", + SameSite: http.SameSiteLaxMode, + }) + } else { + cartId = ToCartId(cartIdCookie[0].Value) + } + adjustedPath := strings.Replace(r.URL.Path, "/cart", "", 1) + location := fmt.Sprintf("/cart/%s%s", cartId, strings.TrimRight(adjustedPath, "/")) + w.Header().Set("Location", location) + w.WriteHeader(http.StatusMovedPermanently) +} + func (s *PoolServer) Serve() *http.ServeMux { mux := http.NewServeMux() + mux.HandleFunc("/", s.RewritePath) mux.HandleFunc("GET /{id}", ErrorHandler(s.HandleGet)) mux.HandleFunc("GET /{id}/add/{sku}", ErrorHandler(s.HandleAddSku)) mux.HandleFunc("POST /{id}", ErrorHandler(s.HandleAddRequest))