From 3ee280a124981f6a536aa91a0ba526052b1136df Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sun, 1 May 2022 22:01:53 +0200 Subject: [PATCH] Problem: can't fetch a block with the API Solution: add route to fetch a single with the API --- main.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/main.go b/main.go index 92d1fe2..4b64166 100644 --- a/main.go +++ b/main.go @@ -1066,6 +1066,37 @@ func main() { mp = NewFilePages(dataDir, searchIndex) http.Handle("/auth/", &authHandler{}) + http.HandleFunc("/api/block/view", wrapAuth(func(w http.ResponseWriter, r *http.Request) { + defer r.Body.Close() + + if !r.Context().Value(authKey).(bool) { + http.Error(w, "Unauthorized", 401) + return + } + + if r.Method != "GET" { + http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed) + return + } + + id := r.URL.Query().Get("id") + + repo := blockRepo{dirname: "data"} + block, err := repo.Load(id) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + + w.Header().Set("Content-Type", "application/json") + w.Header().Set("Cache-Control", "no-store") + enc := json.NewEncoder(w) + enc.SetIndent("", " ") + err = enc.Encode(block) + if err != nil { + http.Error(w, err.Error(), 500) + } + })) http.HandleFunc("/api/block/", wrapAuth(func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close()