/* 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 . */ package api import ( "errors" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/requestid" "github.com/gofiber/fiber/v2/middleware/session" "github.com/gofiber/keyauth/v2" "github.com/tlinden/up/upd/cfg" ) // sessions are context specific and can be global savely var Sessionstore *session.Store func Runserver(cfg *cfg.Config, args []string) error { Sessionstore = session.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{ Format: "${pid} ${locals:requestid} ${status} - ${method} ${path}​\n", })) db, err := NewDb(cfg.DbFile) if err != nil { return err } defer db.Close() AuthSetEndpoints(cfg.ApiPrefix, ApiVersion, []string{"/file"}) AuthSetApikeys(cfg.Apicontext) auth := keyauth.New(keyauth.Config{ Validator: AuthValidateAPIKey, ErrorHandler: AuthErrHandler, }) shallExpire := true api := router.Group(cfg.ApiPrefix + ApiVersion) { // authenticated routes api.Post("/file/", auth, func(c *fiber.Ctx) error { msg, err := FilePut(c, cfg, db) return SendResponse(c, msg, err) }) api.Get("/file/:id/:file", auth, func(c *fiber.Ctx) error { return FileGet(c, cfg, db) }) api.Get("/file/:id/", auth, func(c *fiber.Ctx) error { return FileGet(c, cfg, db) }) api.Delete("/file/:id/", auth, func(c *fiber.Ctx) error { return FileDelete(c, cfg, db) }) api.Get("/list/", auth, func(c *fiber.Ctx) error { msg, err := List(c, cfg, db) return SendResponse(c, msg, err) }) } // public routes 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, cfg, db, shallExpire) }) router.Get("/download/:id/", func(c *fiber.Ctx) error { return FileGet(c, cfg, db, shallExpire) }) return router.Listen(cfg.Listen) } func SendResponse(c *fiber.Ctx, msg string, err error) error { if err != nil { code := fiber.StatusInternalServerError var e *fiber.Error if errors.As(err, &e) { code = e.Code } return c.Status(code).JSON(Result{ Code: code, Message: err.Error(), Success: false, }) } return c.Status(fiber.StatusOK).JSON(Result{ Code: fiber.StatusOK, Message: msg, Success: true, }) }