package routes import ( "bytes" "log" "github.com/gofiber/fiber/v2" ) func (r *Router) Api(api fiber.Router) { api.Get("/status", func(c *fiber.Ctx) error { return c.JSON("ok") }) api.Get("/buckets", func(c *fiber.Ctx) error { buckets, err := r.Database.Buckets() if err != nil { return err } return c.JSON(buckets) }) api.Post("/buckets", func(c *fiber.Ctx) error { var req struct { Bucket string `json:"bucket"` Path string `json:"path"` } log.Printf("%v", string(c.Body())) if err := c.BodyParser(&req); err != nil { return err } opts := []any{} if req.Path != "" { opts = append(opts, req.Path) } if err := r.Database.CreateBucket(req.Bucket, opts...); err != nil { return err } return c.JSON("ok") }) api.Post("/buckets/:bucket", func(c *fiber.Ctx) error { bucket := c.Params("bucket") b, err := r.Database.Bucket(bucket) if err != nil { return err } ff, err := c.FormFile("file") if err != nil { return err } mf, err := ff.Open() if err != nil { return err } id, err := b.Create(mf) if err != nil { return err } return c.JSON(fiber.Map{ "bucket": bucket, "id": id, }) }) api.Get("/buckets/:bucket/:id", func(c *fiber.Ctx) error { bucket := c.Params("bucket") id := c.Params("id") buf := &bytes.Buffer{} b, err := r.Database.Bucket(bucket) if err != nil { return err } if err := b.Read(id, buf); err != nil { return err } return c.SendStream(buf) }) }