mirror of
https://codeberg.org/scip/ephemerup.git
synced 2025-12-17 12:40:57 +01:00
implemented various things
This commit is contained in:
@@ -23,20 +23,24 @@ import (
|
||||
"github.com/gofiber/fiber/v2/middleware/requestid"
|
||||
"github.com/tlinden/up/upd/cfg"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Runserver(cfg *cfg.Config, args []string) error {
|
||||
dst := cfg.StorageDir
|
||||
router := fiber.New()
|
||||
router := fiber.New(fiber.Config{
|
||||
CaseSensitive: true,
|
||||
StrictRouting: true,
|
||||
Immutable: true,
|
||||
Prefork: cfg.Prefork,
|
||||
ServerHeader: "upd",
|
||||
AppName: cfg.AppName,
|
||||
BodyLimit: cfg.BodyLimit,
|
||||
Network: cfg.Network,
|
||||
})
|
||||
|
||||
router.Use(requestid.New())
|
||||
router.Use(logger.New(logger.Config{
|
||||
// For more options, see the Config section
|
||||
Format: "${pid} ${locals:requestid} ${status} - ${method} ${path}\n",
|
||||
}))
|
||||
api := router.Group(cfg.ApiPrefix + ApiVersion)
|
||||
|
||||
db, err := bolt.Open(cfg.DbFile, 0600, nil)
|
||||
if err != nil {
|
||||
@@ -44,42 +48,27 @@ func Runserver(cfg *cfg.Config, args []string) error {
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
api := router.Group(cfg.ApiPrefix + ApiVersion)
|
||||
{
|
||||
api.Post("/file/put", func(c *fiber.Ctx) error {
|
||||
uri, err := Putfile(c, cfg, db)
|
||||
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(Result{
|
||||
Code: fiber.StatusBadRequest,
|
||||
Message: err.Error(),
|
||||
Success: false,
|
||||
})
|
||||
} else {
|
||||
return c.Status(fiber.StatusOK).JSON(Result{
|
||||
Code: fiber.StatusOK,
|
||||
Message: uri,
|
||||
Success: true,
|
||||
})
|
||||
}
|
||||
api.Post("/file/", func(c *fiber.Ctx) error {
|
||||
msg, err := FilePut(c, cfg, db)
|
||||
return SendResponse(c, msg, err)
|
||||
})
|
||||
|
||||
api.Get("/file/get/:id/:file", func(c *fiber.Ctx) error {
|
||||
// deliver a file and delete it after a delay (FIXME: check
|
||||
// when gin has done delivering it?). Redirect to the static
|
||||
// handler for actual delivery.
|
||||
id := c.Params("id")
|
||||
file := c.Params("file")
|
||||
api.Get("/file/:id/:file", func(c *fiber.Ctx) error {
|
||||
return FileGet(c, cfg, db)
|
||||
})
|
||||
|
||||
filename := filepath.Join(dst, id, file)
|
||||
api.Get("/file/:id/", func(c *fiber.Ctx) error {
|
||||
return FileGet(c, cfg, db)
|
||||
})
|
||||
|
||||
if _, err := os.Stat(filename); err == nil {
|
||||
go func() {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
cleanup(filepath.Join(dst, id))
|
||||
}()
|
||||
}
|
||||
api.Delete("/file/:id/", func(c *fiber.Ctx) error {
|
||||
return FileDelete(c, cfg, db)
|
||||
})
|
||||
|
||||
return c.Download(filename, file)
|
||||
api.Delete("/file/", func(c *fiber.Ctx) error {
|
||||
return FileDelete(c, cfg, db)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -87,7 +76,22 @@ func Runserver(cfg *cfg.Config, args []string) error {
|
||||
return c.Send([]byte("welcome to upload api, use /api enpoint!"))
|
||||
})
|
||||
|
||||
router.Listen(cfg.Listen)
|
||||
return router.Listen(cfg.Listen)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func SendResponse(c *fiber.Ctx, msg string, err error) error {
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(Result{
|
||||
Code: fiber.StatusBadRequest,
|
||||
Message: err.Error(),
|
||||
Success: false,
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(Result{
|
||||
Code: fiber.StatusOK,
|
||||
Message: msg,
|
||||
Success: true,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user