Problem: can't fetch a block with the API

Solution: add route to fetch a single with the API
This commit is contained in:
Peter Stuifzand 2022-05-01 22:01:53 +02:00
parent 726f3c944b
commit 3ee280a124

31
main.go
View File

@ -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()