Restore authorized Delamain note slices

This commit is contained in:
takuya authored and Test committed 2026-11-29 13:11:53 +03:00
1 parent 96314809c8
commit 0ed5e1fd7a
3 files changed
+128 -3

No files matched your search

+17 -2
View File
@@ -10,6 +10,7 @@ import (
"net/http/httputil"
"net/url"
"strings"
"sync"
"time"
)
@@ -17,6 +18,8 @@ type apiGuard struct {
upstream *url.URL
client *http.Client
proxy *httputil.ReverseProxy
notesMu sync.Mutex
notes map[noteHandle]exportedNote
}
func main() {
@@ -35,8 +38,9 @@ func main() {
func newGuard(upstream *url.URL) *apiGuard {
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.ResponseHeaderTimeout = 15 * time.Second
return &apiGuard{
guard := &apiGuard{
upstream: upstream,
notes: make(map[noteHandle]exportedNote),
client: &http.Client{
Transport: transport,
Timeout: 5 * time.Second,
@@ -55,6 +59,8 @@ func newGuard(upstream *url.URL) *apiGuard {
},
},
}
guard.proxy.ModifyResponse = guard.rememberExport
return guard
}
func (g *apiGuard) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@@ -70,6 +76,11 @@ func (g *apiGuard) ServeHTTP(w http.ResponseWriter, r *http.Request) {
g.proxy.ServeHTTP(w, r)
}
return
case "/api/slices":
if r.Method == http.MethodPost {
g.readSlice(w, r)
return
}
}
if r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/api/trips/") {
@@ -155,7 +166,11 @@ func normalizePackage(w http.ResponseWriter, r *http.Request) bool {
}
func writeError(w http.ResponseWriter, status int, message string) {
writeJSON(w, status, map[string]string{"error": message})
}
func writeJSON(w http.ResponseWriter, status int, body any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(map[string]string{"error": message})
_ = json.NewEncoder(w).Encode(body)
}