OK NEW SERVER PASSES TESTS WOO

This commit is contained in:
Bel LaPointe
2022-02-09 14:46:06 -07:00
parent dc6df3f0bf
commit 15e6b62251
2 changed files with 34 additions and 18 deletions

View File

@@ -212,7 +212,6 @@ func (server *Server) apiV0FilesHandler(w http.ResponseWriter, r *http.Request)
case http.MethodDelete:
return server.apiV0FilesIDDelHandler(w, r)
}
log.Printf("no method for %s: %s", r.Method, r.URL)
http.NotFound(w, r)
return nil
}
@@ -238,7 +237,14 @@ func (server *Server) apiV0FilesPostHandler(w http.ResponseWriter, r *http.Reque
pid := server.fileId(r)
id := append(pid, strings.Split(uuid.New().String(), "-")[0])
return server.tree().Put(id, Leaf{Title: r.Header.Get("Title"), Content: string(b)})
if err := server.tree().Put(id, Leaf{Title: r.Header.Get("Title"), Content: string(b)}); err != nil {
return err
}
return json.NewEncoder(w).Encode(map[string]map[string]string{
"data": map[string]string{
"filePath": path.Join("/api/v0/files/", server.urlFileId(id)),
},
})
}
func (server *Server) apiV0FilesIDGetHandler(w http.ResponseWriter, r *http.Request) error {
@@ -282,7 +288,13 @@ func (server *Server) urlFileId(id []string) string {
}
func (server *Server) fileId(r *http.Request) []string {
return strings.Split(strings.TrimPrefix(r.URL.Path, "/api/v0/files/"), "/")
return strings.Split(
strings.TrimPrefix(
strings.Trim(r.URL.Path, "/"),
"api/v0/files/",
),
"/",
)
}
func (server *Server) apiV0FilesIDPutHandler(w http.ResponseWriter, r *http.Request) error {
@@ -302,7 +314,14 @@ func (server *Server) apiV0FilesIDPutHandler(w http.ResponseWriter, r *http.Requ
leaf.Title = r.Header.Get("Title")
leaf.Deleted = false
return server.tree().Put(id, leaf)
if err := server.tree().Put(id, leaf); err != nil {
return err
}
return json.NewEncoder(w).Encode(map[string]map[string]string{
"data": map[string]string{
"filePath": path.Join("/api/v0/files/", server.urlFileId(id)),
},
})
}
func (server *Server) apiV0SearchHandler(w http.ResponseWriter, r *http.Request) error {