2023-02-24 12:16:03 +01:00
|
|
|
|
/*
|
|
|
|
|
|
Copyright © 2023 Thomas von Dein
|
|
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2023-03-07 20:06:13 +01:00
|
|
|
|
"errors"
|
2023-02-24 12:16:03 +01:00
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2023-03-08 19:31:42 +01:00
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/compress"
|
|
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
2023-02-24 12:16:03 +01:00
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
|
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/requestid"
|
2023-03-07 20:06:13 +01:00
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/session"
|
2023-03-06 19:46:06 +01:00
|
|
|
|
"github.com/gofiber/keyauth/v2"
|
2023-02-24 12:16:03 +01:00
|
|
|
|
"github.com/tlinden/up/upd/cfg"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2023-03-07 20:06:13 +01:00
|
|
|
|
// sessions are context specific and can be global savely
|
|
|
|
|
|
var Sessionstore *session.Store
|
|
|
|
|
|
|
2023-03-08 19:31:42 +01:00
|
|
|
|
const shallExpire = true
|
2023-02-28 19:05:09 +01:00
|
|
|
|
|
2023-03-08 19:31:42 +01:00
|
|
|
|
func Runserver(conf *cfg.Config, args []string) error {
|
|
|
|
|
|
// required for authenticated routes, used to store the api context
|
|
|
|
|
|
Sessionstore = session.New()
|
2023-02-24 12:16:03 +01:00
|
|
|
|
|
2023-03-08 19:31:42 +01:00
|
|
|
|
// bbolt db setup
|
2023-03-16 15:04:03 +01:00
|
|
|
|
db, err := NewDb(conf)
|
2023-02-24 12:16:03 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
2023-03-08 19:31:42 +01:00
|
|
|
|
// setup authenticated endpoints
|
|
|
|
|
|
auth := SetupAuthStore(conf)
|
2023-03-06 19:46:06 +01:00
|
|
|
|
|
2023-03-08 19:31:42 +01:00
|
|
|
|
// setup api server
|
|
|
|
|
|
router := SetupServer(conf)
|
2023-03-06 19:46:06 +01:00
|
|
|
|
|
2023-03-08 19:31:42 +01:00
|
|
|
|
// authenticated routes
|
|
|
|
|
|
api := router.Group(conf.ApiPrefix + ApiVersion)
|
2023-02-24 12:16:03 +01:00
|
|
|
|
{
|
2023-03-17 13:04:52 +01:00
|
|
|
|
// upload
|
2023-03-06 19:46:06 +01:00
|
|
|
|
api.Post("/file/", auth, func(c *fiber.Ctx) error {
|
2023-03-13 18:48:38 +01:00
|
|
|
|
return FilePut(c, conf, db)
|
2023-02-24 12:16:03 +01:00
|
|
|
|
})
|
|
|
|
|
|
|
2023-03-17 13:04:52 +01:00
|
|
|
|
// download w/o expire
|
2023-03-06 19:46:06 +01:00
|
|
|
|
api.Get("/file/:id/:file", auth, func(c *fiber.Ctx) error {
|
2023-03-08 19:31:42 +01:00
|
|
|
|
return FileGet(c, conf, db)
|
2023-02-28 19:05:09 +01:00
|
|
|
|
})
|
2023-03-06 19:46:06 +01:00
|
|
|
|
api.Get("/file/:id/", auth, func(c *fiber.Ctx) error {
|
2023-03-08 19:31:42 +01:00
|
|
|
|
return FileGet(c, conf, db)
|
2023-02-28 19:05:09 +01:00
|
|
|
|
})
|
2023-02-24 12:16:03 +01:00
|
|
|
|
|
2023-03-17 13:04:52 +01:00
|
|
|
|
// remove
|
2023-03-06 19:46:06 +01:00
|
|
|
|
api.Delete("/file/:id/", auth, func(c *fiber.Ctx) error {
|
2023-03-08 19:31:42 +01:00
|
|
|
|
err := DeleteUpload(c, conf, db)
|
|
|
|
|
|
return SendResponse(c, "", err)
|
2023-02-28 19:05:09 +01:00
|
|
|
|
})
|
2023-03-07 20:06:13 +01:00
|
|
|
|
|
2023-03-17 13:04:52 +01:00
|
|
|
|
// listing
|
2023-03-07 20:06:13 +01:00
|
|
|
|
api.Get("/list/", auth, func(c *fiber.Ctx) error {
|
2023-03-08 19:31:42 +01:00
|
|
|
|
return List(c, conf, db)
|
2023-03-07 20:06:13 +01:00
|
|
|
|
})
|
2023-03-09 20:24:20 +01:00
|
|
|
|
|
2023-03-17 13:04:52 +01:00
|
|
|
|
// info
|
2023-03-09 20:24:20 +01:00
|
|
|
|
api.Get("/upload/:id/", auth, func(c *fiber.Ctx) error {
|
|
|
|
|
|
return Describe(c, conf, db)
|
|
|
|
|
|
})
|
2023-02-24 12:16:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-06 19:46:06 +01:00
|
|
|
|
// public routes
|
2023-03-08 19:31:42 +01:00
|
|
|
|
{
|
|
|
|
|
|
router.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
|
|
return c.Send([]byte("welcome to upload api, use /api enpoint!"))
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
router.Get("/download/:id/:file", func(c *fiber.Ctx) error {
|
|
|
|
|
|
return FileGet(c, conf, db, shallExpire)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
router.Get("/download/:id/", func(c *fiber.Ctx) error {
|
|
|
|
|
|
return FileGet(c, conf, db, shallExpire)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// setup cleaner
|
|
|
|
|
|
quitcleaner := BackgroundCleaner(conf, db)
|
|
|
|
|
|
|
|
|
|
|
|
router.Hooks().OnShutdown(func() error {
|
|
|
|
|
|
Log("Shutting down cleaner")
|
|
|
|
|
|
close(quitcleaner)
|
|
|
|
|
|
return nil
|
2023-02-24 12:16:03 +01:00
|
|
|
|
})
|
|
|
|
|
|
|
2023-03-08 19:31:42 +01:00
|
|
|
|
return router.Listen(conf.Listen)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func SetupAuthStore(conf *cfg.Config) func(*fiber.Ctx) error {
|
|
|
|
|
|
AuthSetEndpoints(conf.ApiPrefix, ApiVersion, []string{"/file"})
|
|
|
|
|
|
AuthSetApikeys(conf.Apicontext)
|
|
|
|
|
|
|
|
|
|
|
|
return keyauth.New(keyauth.Config{
|
|
|
|
|
|
Validator: AuthValidateAPIKey,
|
|
|
|
|
|
ErrorHandler: AuthErrHandler,
|
2023-03-06 19:46:06 +01:00
|
|
|
|
})
|
2023-03-08 19:31:42 +01:00
|
|
|
|
}
|
2023-03-06 19:46:06 +01:00
|
|
|
|
|
2023-03-08 19:31:42 +01:00
|
|
|
|
func SetupServer(conf *cfg.Config) *fiber.App {
|
|
|
|
|
|
router := fiber.New(fiber.Config{
|
|
|
|
|
|
CaseSensitive: true,
|
|
|
|
|
|
StrictRouting: true,
|
|
|
|
|
|
Immutable: true,
|
|
|
|
|
|
Prefork: conf.Prefork,
|
|
|
|
|
|
ServerHeader: "upd",
|
|
|
|
|
|
AppName: conf.AppName,
|
|
|
|
|
|
BodyLimit: conf.BodyLimit,
|
|
|
|
|
|
Network: conf.Network,
|
2023-03-06 19:46:06 +01:00
|
|
|
|
})
|
|
|
|
|
|
|
2023-03-08 19:31:42 +01:00
|
|
|
|
router.Use(requestid.New())
|
2023-02-24 12:16:03 +01:00
|
|
|
|
|
2023-03-08 19:31:42 +01:00
|
|
|
|
router.Use(logger.New(logger.Config{
|
|
|
|
|
|
Format: "${pid} ${locals:requestid} ${status} - ${method} ${path}\n",
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
|
router.Use(cors.New(cors.Config{
|
|
|
|
|
|
AllowMethods: "GET,PUT,POST,DELETE",
|
|
|
|
|
|
ExposeHeaders: "Content-Type,Authorization,Accept",
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
|
router.Use(compress.New(compress.Config{
|
|
|
|
|
|
Level: compress.LevelBestSpeed,
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
|
return router
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
Wrapper to respond with proper json status, message and code,
|
|
|
|
|
|
shall be prepared and called by the handlers directly.
|
|
|
|
|
|
*/
|
|
|
|
|
|
func JsonStatus(c *fiber.Ctx, code int, msg string) error {
|
|
|
|
|
|
success := true
|
|
|
|
|
|
|
|
|
|
|
|
if code != fiber.StatusOK {
|
|
|
|
|
|
success = false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return c.Status(code).JSON(Result{
|
|
|
|
|
|
Code: code,
|
|
|
|
|
|
Message: msg,
|
|
|
|
|
|
Success: success,
|
|
|
|
|
|
})
|
2023-02-28 19:05:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-08 19:31:42 +01:00
|
|
|
|
/*
|
|
|
|
|
|
Used for non json-aware handlers, called by server
|
|
|
|
|
|
*/
|
2023-02-28 19:05:09 +01:00
|
|
|
|
func SendResponse(c *fiber.Ctx, msg string, err error) error {
|
|
|
|
|
|
if err != nil {
|
2023-03-07 20:06:13 +01:00
|
|
|
|
code := fiber.StatusInternalServerError
|
|
|
|
|
|
|
|
|
|
|
|
var e *fiber.Error
|
|
|
|
|
|
if errors.As(err, &e) {
|
|
|
|
|
|
code = e.Code
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return c.Status(code).JSON(Result{
|
|
|
|
|
|
Code: code,
|
2023-02-28 19:05:09 +01:00
|
|
|
|
Message: err.Error(),
|
|
|
|
|
|
Success: false,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return c.Status(fiber.StatusOK).JSON(Result{
|
|
|
|
|
|
Code: fiber.StatusOK,
|
|
|
|
|
|
Message: msg,
|
|
|
|
|
|
Success: true,
|
|
|
|
|
|
})
|
2023-02-24 12:16:03 +01:00
|
|
|
|
}
|