You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
1.5 KiB
Go

2 years ago
package routes
import (
"bytes"
"log"
"github.com/gofiber/fiber/v2"
)
2 years ago
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)
})
2 years ago
}