diff --git a/routes/routes.go b/routes/routes.go index 1c1c87c..8e8f73f 100755 --- a/routes/routes.go +++ b/routes/routes.go @@ -106,15 +106,14 @@ func SetupRoutes(app *fiber.App, db *badger.DB) { } queries := c.Queries() - potentialLang := "" if len(queries) == 1 { - potentialLang = slices.Collect(maps.Keys(queries))[0] - fmt.Println(potentialLang) + lang := slices.Collect(maps.Keys(queries))[0] + 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)) - 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{ "Code": outputHtml.String(), }) @@ -124,7 +123,6 @@ func SetupRoutes(app *fiber.App, db *badger.DB) { "Code": string(itemValue), }) } - } else { ua := useragent.Parse(string(c.Context().UserAgent())) if ua.IsUnknown() { @@ -142,6 +140,56 @@ https://sr.ht/~seraphimrp/bingo`) 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 {