Complete delete functionality, update feature roadmap

This commit is contained in:
Seraphim R. Pardee 2024-09-11 23:49:55 -04:00
parent 279db23b42
commit dfc8ff001e
2 changed files with 22 additions and 36 deletions

View File

@ -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. 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 - [x] Post/read functionality
- [ ] Delete (and edit?) functionality - [x] Delete functionality
- [x] Syntax highlighting - [x] Syntax highlighting
- [ ] Server configuration file (via viper)
- [ ] API key authentication / loose user management - [ ] API key authentication / loose user management
- [ ] CLI upload / management functionality - [ ] CLI upload / management functionality
- [ ] Edit/revise/replace content at ID

View File

@ -141,58 +141,42 @@ https://sr.ht/~seraphimrp/bingo`)
} }
}) })
app.Delete(("/:id"), func(c *fiber.Ctx) error { app.Delete("/:id", func(c *fiber.Ctx) error {
id := c.Params("id") potentialId := c.Params("id")
idNotFound := false keyNotFound := false
var itemValue []byte
err := db.View(func(txn *badger.Txn) error { 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 { if err != nil && err != badger.ErrKeyNotFound {
return err return err
} else if err == badger.ErrKeyNotFound { } else if err == badger.ErrKeyNotFound {
idNotFound = true keyNotFound = true
return nil
} }
itemValue, err = item.ValueCopy(nil) return nil
return err
}) })
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) c.SendStatus(404)
return c.SendString("did not receive valid parameter") return c.SendString("did not receive valid parameter")
} }
ua := useragent.Parse(string(c.Context().UserAgent())) err = db.Update(func(txn *badger.Txn) error {
if ua.IsUnknown() { cfmt.Infof("🗑️ [info] deleting entry at %s\n", potentialId)
return c.SendString(string(itemValue)) err := txn.Delete([]byte(potentialId))
} return err
queries := c.Queries()
if len(queries) == 1 {
lang := slices.Collect(maps.Keys(queries))[0]
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 { handleErr(err, "encountered an error when deleting an entry in the db")
cfmt.Infof("👀 [info] reading %s\n", id)
return c.Render("render", fiber.Map{ cfmt.Infof("🗑️ [info] entry deleted at %s\n", potentialId)
"Code": string(itemValue),
}) return c.SendString("success")
}
}) })
} }
func generateId() string { func generateId() string {
// TODO: optimize via https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go
id := "" id := ""
characters := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" characters := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"