add(backend): support paste deletion

This commit is contained in:
Ada 2023-10-12 21:46:30 +02:00
parent a5c940f245
commit b21d9c9c52

21
main.go
View file

@ -32,6 +32,7 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
if urlExist(clearPath) { if urlExist(clearPath) {
if strings.HasSuffix(path, "/raw") { if strings.HasSuffix(path, "/raw") {
pasteContent := db.HGet(ctx, clearPath, "content").Val() pasteContent := db.HGet(ctx, clearPath, "content").Val()
w.Header().Set("Content-Type", "text/plain")
_, err := io.WriteString(w, pasteContent) _, err := io.WriteString(w, pasteContent)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -62,6 +63,26 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
} else { } else {
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
} }
case "DELETE":
if strings.HasPrefix(path, "/delete") {
urlItem := strings.Split(path, "/")
if urlExist("/" + urlItem[2]) {
secret := r.URL.Query().Get("secret")
if secret == db.HGet(ctx, "/"+urlItem[2], "secret").Val() {
err := db.Del(ctx, "/"+urlItem[2])
if err != nil {
log.Println(err)
}
w.WriteHeader(http.StatusNoContent)
} else {
w.WriteHeader(http.StatusForbidden)
}
} else {
w.WriteHeader(http.StatusNotFound)
}
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
}
} }
} }