tidying and beginning deletion functionality

This commit is contained in:
Seraphim R. Pardee 2024-09-07 15:22:42 -04:00
parent d2adac78eb
commit 279db23b42

View File

@ -106,15 +106,14 @@ func SetupRoutes(app *fiber.App, db *badger.DB) {
} }
queries := c.Queries() queries := c.Queries()
potentialLang := ""
if len(queries) == 1 { if len(queries) == 1 {
potentialLang = slices.Collect(maps.Keys(queries))[0] lang := slices.Collect(maps.Keys(queries))[0]
fmt.Println(potentialLang)
var outputHtml bytes.Buffer var outputHtml bytes.Buffer
err = quick.Highlight(&outputHtml, string(itemValue), potentialLang, "html", "gruvbox") err = quick.Highlight(&outputHtml, string(itemValue), lang, "html", "gruvbox")
handleErr(err, fmt.Sprintf("unable to syntax highlight %s\n", id)) handleErr(err, fmt.Sprintf("unable to syntax highlight %s\n", id))
cfmt.Infof("👀 [info] reading %s with '%s' syntax\n", id, potentialLang) cfmt.Infof("👀 [info] reading %s with '%s' syntax\n", id, lang)
return c.Render("render_syntax", fiber.Map{ return c.Render("render_syntax", fiber.Map{
"Code": outputHtml.String(), "Code": outputHtml.String(),
}) })
@ -124,7 +123,6 @@ func SetupRoutes(app *fiber.App, db *badger.DB) {
"Code": string(itemValue), "Code": string(itemValue),
}) })
} }
} else { } else {
ua := useragent.Parse(string(c.Context().UserAgent())) ua := useragent.Parse(string(c.Context().UserAgent()))
if ua.IsUnknown() { if ua.IsUnknown() {
@ -142,6 +140,56 @@ https://sr.ht/~seraphimrp/bingo`)
return c.Render("index", nil) return c.Render("index", nil)
} }
}) })
app.Delete(("/:id"), func(c *fiber.Ctx) error {
id := c.Params("id")
idNotFound := false
var itemValue []byte
err := db.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(c.Params("id")))
if err != nil && err != badger.ErrKeyNotFound {
return err
} else if err == badger.ErrKeyNotFound {
idNotFound = true
return nil
}
itemValue, err = item.ValueCopy(nil)
return err
})
handleErr(err, fmt.Sprintf("unable to fetch %s from db", id))
if idNotFound {
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))
}
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 {
cfmt.Infof("👀 [info] reading %s\n", id)
return c.Render("render", fiber.Map{
"Code": string(itemValue),
})
}
})
} }
func generateId() string { func generateId() string {