update stuff
This commit is contained in:
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user