update the code
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 44s
Build and Publish / BuildAndDeployArm64 (push) Successful in 4m39s

This commit is contained in:
matst80
2025-11-20 15:27:40 +01:00
parent 874963812f
commit 1c8e9cc974
3 changed files with 64 additions and 5 deletions

View File

@@ -7,6 +7,7 @@ import (
"fmt"
"io"
"io/fs"
"log"
"net/http"
"os"
"path/filepath"
@@ -17,6 +18,7 @@ import (
"git.tornberg.me/go-cart-actor/pkg/actor"
"git.tornberg.me/go-cart-actor/pkg/cart"
"github.com/gogo/protobuf/proto"
)
type FileServer struct {
@@ -232,6 +234,22 @@ type JsonError struct {
Error string `json:"error"`
}
func acceptAll(_ proto.Message, _ int, _ time.Time) bool {
return true
}
func acceptUntilIndex(maxIndex int) func(msg proto.Message, index int, when time.Time) bool {
return func(msg proto.Message, index int, when time.Time) bool {
return index < maxIndex
}
}
func acceptUntilTimestamp(until time.Time) func(msg proto.Message, index int, when time.Time) bool {
return func(msg proto.Message, index int, when time.Time) bool {
return when.Before(until)
}
}
func (fs *FileServer) CartHandler(w http.ResponseWriter, r *http.Request) {
idStr := r.PathValue("id")
if idStr == "" {
@@ -243,10 +261,29 @@ func (fs *FileServer) CartHandler(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusBadRequest, JsonError{Error: "invalid id"})
return
}
// parse query parameters for filtering
query := r.URL.Query()
filterFunction := acceptAll
if maxIndexStr := query.Get("maxIndex"); maxIndexStr != "" {
log.Printf("filter maxIndex: %s", maxIndexStr)
maxIndex, err := strconv.Atoi(maxIndexStr)
if err != nil {
writeJSON(w, http.StatusBadRequest, JsonError{Error: "invalid maxIndex"})
return
}
filterFunction = acceptUntilIndex(maxIndex)
} else if untilStr := query.Get("until"); untilStr != "" {
log.Printf("filter until: %s", untilStr)
until, err := time.Parse(time.RFC3339, untilStr)
if err != nil {
writeJSON(w, http.StatusBadRequest, JsonError{Error: "invalid until timestamp"})
return
}
filterFunction = acceptUntilTimestamp(until)
}
// reconstruct state from event log if present
grain := cart.NewCartGrain(id, time.Now())
err := fs.storage.LoadEvents(r.Context(), id, grain)
err := fs.storage.LoadEventsFunc(r.Context(), id, grain, filterFunction)
if err != nil {
writeJSON(w, http.StatusInternalServerError, JsonError{Error: err.Error()})
return