Skip to content

Commit

Permalink
Added delete endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLovesDoggo committed Mar 17, 2024
1 parent 6be04ba commit 50f7ed1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func main() {
r.POST("/create/:namespace/*key", CreateView)
r.GET("/create/:namespace/*key", CreateView)
r.GET("/info/:namespace/*key", InfoView)
r.POST("/delete/:namespace/*key", DeleteView)

// Run the server
_ = r.Run("0.0.0.0:" + os.Getenv("PORT"))
Expand Down
30 changes: 30 additions & 0 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -104,3 +105,32 @@ func InfoView(c *gin.Context) { // todo: write docs on what negative values mean
}
c.JSON(http.StatusOK, gin.H{"value": count, "full_key": dbKey, "is_genuine": isGenuine, "expires": timeToLive, "exists": exists})
}

func DeleteView(c *gin.Context) {
namespace, key := utils.GetNamespaceKey(c)
if namespace == "" || key == "" {
return
}
authToken := c.DefaultQuery("token", "")
if authToken == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "token is required, please provide a token in the fmt of ?token=ADMIN_TOKEN"})
return
}
createKey, err := utils.CreateKey(namespace, key, true)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
adminDBKey := utils.CreateAdminKey(createKey)
adminKey, err := Client.Get(context.Background(), adminDBKey).Result()
if errors.Is(err, redis.Nil) {
c.JSON(http.StatusBadRequest, gin.H{"error": "This entry is genuine and does not have an admin key. You cannot delete it. If you wanted to delete it, you should have created it with the /create endpoint."})

} else if adminKey != authToken {
c.JSON(http.StatusUnauthorized, gin.H{"error": "token is invalid"})
} else {
Client.Del(context.Background(), createKey) // Delete the normal key
Client.Del(context.Background(), adminDBKey) // delete the admin key as it's now useless
c.JSON(http.StatusOK, gin.H{"status": "ok", "message": "Deleted key: " + createKey})
}
}
6 changes: 4 additions & 2 deletions tests/Tests.http
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
@base=https://localhost:8080
@base=http://localhost:8080
@token="REPLACE_ME"
@key=jvc


GET {{base}}/hit/jasoncameron.dev/portfolio
POST {{base}}/delete/{{key}}?token=e7fad266-b34f-4fc3-8788-a6f78f71af1c

#GET http://localhost:8080/info/jasoncameron.dev/portfolio

Expand All @@ -15,3 +16,4 @@ POST {{base}}/delete/jasoncameron.dev/portfolio
Authorization: Bearer {{token}}



0 comments on commit 50f7ed1

Please sign in to comment.