droboticflow/internal/api/annotation_read_cache.go

32 lines
1.1 KiB
Go

package api
import (
"context"
"droboticflow/internal/domain"
"net/http"
"strings"
)
type annotationReadCacheKey struct{}
type annotationRequestReads struct {
items map[string]*domain.HumanItemRecord
summary bool
}
func annotationReadCache(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/api/plugins/annotation-qa/tasks/") {
view := r.URL.Query().Get("view")
detailPath := !strings.Contains(strings.TrimPrefix(r.URL.Path, "/api/plugins/annotation-qa/tasks/"), "/")
reads := &annotationRequestReads{items: map[string]*domain.HumanItemRecord{}, summary: detailPath && r.Method == http.MethodGet && (view == "status" || view == "summary")}
r = r.WithContext(context.WithValue(r.Context(), annotationReadCacheKey{}, reads))
}
next.ServeHTTP(w, r)
})
}
// Detached jobs must reread ownership and state, not inherit an HTTP snapshot.
func withoutAnnotationReadCache(ctx context.Context) context.Context {
return context.WithValue(ctx, annotationReadCacheKey{}, (*annotationRequestReads)(nil))
}