From dfc8ff001e6b5c30da8d6117e21b4332f7986cc6 Mon Sep 17 00:00:00 2001 From: "Seraphim R. Pardee" Date: Wed, 11 Sep 2024 23:49:55 -0400 Subject: [PATCH] Complete delete functionality, update feature roadmap --- README.md | 8 +++++--- routes/routes.go | 50 ++++++++++++++++-------------------------------- 2 files changed, 22 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index bd3ca34..25041d6 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,11 @@ A lightweight {paste,file}bin alternative written in Golang. Overall goal is to create a service that works well as a single-user self-hosted instance, which can trivially upload files/text. -## Feature List (so far...) +## Feature Roadmap (so far...; from most important to least) - [x] Post/read functionality -- [ ] Delete (and edit?) functionality +- [x] Delete functionality - [x] Syntax highlighting +- [ ] Server configuration file (via viper) - [ ] API key authentication / loose user management -- [ ] CLI upload / management functionality \ No newline at end of file +- [ ] CLI upload / management functionality +- [ ] Edit/revise/replace content at ID \ No newline at end of file diff --git a/routes/routes.go b/routes/routes.go index 8e8f73f..b13e099 100755 --- a/routes/routes.go +++ b/routes/routes.go @@ -141,58 +141,42 @@ https://sr.ht/~seraphimrp/bingo`) } }) - app.Delete(("/:id"), func(c *fiber.Ctx) error { - id := c.Params("id") - idNotFound := false - - var itemValue []byte + app.Delete("/:id", func(c *fiber.Ctx) error { + potentialId := c.Params("id") + keyNotFound := false err := db.View(func(txn *badger.Txn) error { - item, err := txn.Get([]byte(c.Params("id"))) + _, err := txn.Get([]byte(potentialId)) if err != nil && err != badger.ErrKeyNotFound { return err } else if err == badger.ErrKeyNotFound { - idNotFound = true - return nil + keyNotFound = true } - itemValue, err = item.ValueCopy(nil) - return err + return nil }) - handleErr(err, fmt.Sprintf("unable to fetch %s from db", id)) + handleErr(err, "encountered an error when searching for existing keys") - if idNotFound { + if keyNotFound { c.SendStatus(404) return c.SendString("did not receive valid parameter") } - ua := useragent.Parse(string(c.Context().UserAgent())) - if ua.IsUnknown() { - return c.SendString(string(itemValue)) - } + err = db.Update(func(txn *badger.Txn) error { + cfmt.Infof("🗑️ [info] deleting entry at %s\n", potentialId) + err := txn.Delete([]byte(potentialId)) + return err + }) + handleErr(err, "encountered an error when deleting an entry in the db") - queries := c.Queries() - if len(queries) == 1 { - lang := slices.Collect(maps.Keys(queries))[0] + cfmt.Infof("🗑️ [info] entry deleted at %s\n", potentialId) - var outputHtml bytes.Buffer - err = quick.Highlight(&outputHtml, string(itemValue), lang, "html", "gruvbox") - handleErr(err, fmt.Sprintf("unable to syntax highlight %s\n", id)) - - cfmt.Infof("👀 [info] reading %s with '%s' syntax\n", id, lang) - return c.Render("render_syntax", fiber.Map{ - "Code": outputHtml.String(), - }) - } else { - cfmt.Infof("👀 [info] reading %s\n", id) - return c.Render("render", fiber.Map{ - "Code": string(itemValue), - }) - } + return c.SendString("success") }) } func generateId() string { + // TODO: optimize via https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go id := "" characters := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"