|
|
@ -20,30 +20,23 @@ func (r *Server) isAdminMiddleware(c *fiber.Ctx) error {
|
|
|
|
return c.Next()
|
|
|
|
return c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *Server) Api(api fiber.Router) {
|
|
|
|
func (s *Server) isAPIKeyMiddleware(c *fiber.Ctx) error {
|
|
|
|
isAPIKeyMiddleware := func(c *fiber.Ctx) error {
|
|
|
|
if _, isAdmin := s.adminSessions[c.Cookies("sid")]; !isAdmin { // if admin continue
|
|
|
|
if _, isAdmin := r.adminSessions[c.Cookies("sid")]; !isAdmin { // if admin continue
|
|
|
|
|
|
|
|
token := c.Cookies("token")
|
|
|
|
token := c.Cookies("token")
|
|
|
|
if token == "" {
|
|
|
|
if token == "" {
|
|
|
|
return fmt.Errorf("no api token")
|
|
|
|
return fmt.Errorf("no api token")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := r.Database.CheckAPIKey(token); err != nil { // otherwise also check api token
|
|
|
|
if err := s.Database.CheckAPIKey(token); err != nil { // otherwise also check api token
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.Next()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
return c.Next()
|
|
|
|
// TODO: Change to /server-info (also in frontend)
|
|
|
|
}
|
|
|
|
// Setup "/api/monitor" routes
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
monitorRoute := api.Group("/monitor")
|
|
|
|
|
|
|
|
monitorRoute.Use(r.isAdminMiddleware)
|
|
|
|
|
|
|
|
r.ApiMonitor(monitorRoute)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api.Post("/login", func(c *fiber.Ctx) error {
|
|
|
|
func (s *Server) ApiAuth(r fiber.Router) {
|
|
|
|
|
|
|
|
r.Post("/login", func(c *fiber.Ctx) error {
|
|
|
|
var form struct {
|
|
|
|
var form struct {
|
|
|
|
Password string `form:"password"`
|
|
|
|
Password string `form:"password"`
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -57,7 +50,7 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
token := utils.GenerateRandomString(32)
|
|
|
|
token := utils.GenerateRandomString(32)
|
|
|
|
r.adminSessions[token] = struct{}{}
|
|
|
|
s.adminSessions[token] = struct{}{}
|
|
|
|
|
|
|
|
|
|
|
|
c.Cookie(&fiber.Cookie{
|
|
|
|
c.Cookie(&fiber.Cookie{
|
|
|
|
Name: "sid",
|
|
|
|
Name: "sid",
|
|
|
@ -69,23 +62,38 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
return c.Redirect("/")
|
|
|
|
return c.Redirect("/")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
api.Get("/status", func(c *fiber.Ctx) error {
|
|
|
|
r.Get("/current-user", func(c *fiber.Ctx) error {
|
|
|
|
return c.JSON("ok")
|
|
|
|
if _, found := s.adminSessions[c.Cookies("sid")]; !found {
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api.Get("/current-user",
|
|
|
|
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
|
|
|
|
if _, found := r.adminSessions[c.Cookies("sid")]; !found {
|
|
|
|
|
|
|
|
return c.JSON("anonymous")
|
|
|
|
return c.JSON("anonymous")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return c.JSON("admin")
|
|
|
|
return c.JSON("admin")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Server) Api(r fiber.Router) {
|
|
|
|
|
|
|
|
|
|
|
|
api.Get("/dashboard-state",
|
|
|
|
r.Get("/status", func(c *fiber.Ctx) error {
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
return c.JSON("ok")
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// TODO: Change "/monitor" to "/server-info" (also in frontend)
|
|
|
|
|
|
|
|
// Setup "/api/monitor" routes
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
r.Route("/", s.ApiAuth)
|
|
|
|
|
|
|
|
r.Route("/", s.ApiDashboard)
|
|
|
|
|
|
|
|
r.Route("/monitor", s.ApiMonitor)
|
|
|
|
|
|
|
|
r.Route("/buckets", s.ApiBucket)
|
|
|
|
|
|
|
|
r.Route("/buckets/:bucket", s.ApiBucketObjects)
|
|
|
|
|
|
|
|
r.Route("/api-keys", s.ApiKeys)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Server) ApiDashboard(r fiber.Router) {
|
|
|
|
|
|
|
|
r.Get("/dashboard-state",
|
|
|
|
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
state, err := r.Database.GetDashboardState()
|
|
|
|
state, err := s.Database.GetDashboardState()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -93,8 +101,8 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
return c.JSON(state)
|
|
|
|
return c.JSON(state)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
api.Post("/dashboard-state",
|
|
|
|
r.Post("/dashboard-state",
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
|
|
|
|
|
|
|
|
var state database.DashboardState
|
|
|
|
var state database.DashboardState
|
|
|
@ -103,17 +111,19 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := r.Database.SetDashboardState(state); err != nil {
|
|
|
|
if err := s.Database.SetDashboardState(state); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return c.JSON("ok")
|
|
|
|
return c.JSON("ok")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
api.Get("/buckets",
|
|
|
|
func (s *Server) ApiBucket(r fiber.Router) {
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
r.Get("/",
|
|
|
|
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
buckets, err := r.Database.AllBuckets()
|
|
|
|
buckets, err := s.Database.AllBuckets()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -121,8 +131,8 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
return c.JSON(buckets)
|
|
|
|
return c.JSON(buckets)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
api.Post("/buckets",
|
|
|
|
r.Post("/",
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
var req struct {
|
|
|
|
var req struct {
|
|
|
|
Bucket string `json:"bucket"`
|
|
|
|
Bucket string `json:"bucket"`
|
|
|
@ -140,19 +150,21 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
settings.Path = req.Path
|
|
|
|
settings.Path = req.Path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := r.Database.CreateBucket(req.Bucket, settings); err != nil {
|
|
|
|
if err := s.Database.CreateBucket(req.Bucket, settings); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return c.JSON("ok")
|
|
|
|
return c.JSON("ok")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
api.Get("/buckets/:bucket",
|
|
|
|
func (s *Server) ApiBucketObjects(r fiber.Router) {
|
|
|
|
isAPIKeyMiddleware,
|
|
|
|
r.Get("/",
|
|
|
|
|
|
|
|
s.isAPIKeyMiddleware,
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
bucket := c.Params("bucket")
|
|
|
|
bucket := c.Params("bucket")
|
|
|
|
|
|
|
|
|
|
|
|
objects, err := r.Database.AllBucketObjects(bucket)
|
|
|
|
objects, err := s.Database.AllBucketObjects(bucket)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -160,12 +172,12 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
return c.JSON(objects)
|
|
|
|
return c.JSON(objects)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
api.Get("/buckets/:bucket/settings",
|
|
|
|
r.Get("/settings",
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
bucket := c.Params("bucket")
|
|
|
|
bucket := c.Params("bucket")
|
|
|
|
|
|
|
|
|
|
|
|
settings, err := r.Database.GetBucketSettings(bucket)
|
|
|
|
settings, err := s.Database.GetBucketSettings(bucket)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -173,8 +185,8 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
return c.JSON(settings)
|
|
|
|
return c.JSON(settings)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
api.Post("/buckets/:bucket/settings",
|
|
|
|
r.Post("/settings",
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
bucket := c.Params("bucket")
|
|
|
|
bucket := c.Params("bucket")
|
|
|
|
|
|
|
|
|
|
|
@ -184,15 +196,15 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := r.Database.SetBucketSettings(bucket, settings); err != nil {
|
|
|
|
if err := s.Database.SetBucketSettings(bucket, settings); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return c.JSON("ok")
|
|
|
|
return c.JSON("ok")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
api.Post("/buckets/:bucket",
|
|
|
|
r.Post("/",
|
|
|
|
isAPIKeyMiddleware,
|
|
|
|
s.isAPIKeyMiddleware,
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
bucket := c.Params("bucket")
|
|
|
|
bucket := c.Params("bucket")
|
|
|
|
|
|
|
|
|
|
|
@ -206,7 +218,7 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
id, err := r.Database.CreateBucketObject(bucket, mf)
|
|
|
|
id, err := s.Database.CreateBucketObject(bucket, mf)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -217,42 +229,41 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
api.Get("/buckets/:bucket/:id",
|
|
|
|
r.Get("/:id",
|
|
|
|
isAPIKeyMiddleware,
|
|
|
|
s.isAPIKeyMiddleware,
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
bucket := c.Params("bucket")
|
|
|
|
bucket := c.Params("bucket")
|
|
|
|
id := c.Params("id")
|
|
|
|
id := c.Params("id")
|
|
|
|
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
|
|
|
|
|
|
|
|
if err := r.Database.GetBucketObject(bucket, id, buf); err != nil {
|
|
|
|
if err := s.Database.GetBucketObject(bucket, id, buf); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return c.SendStream(buf)
|
|
|
|
return c.SendStream(buf)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
api.Delete("/buckets/:bucket/:id",
|
|
|
|
r.Delete("/:id",
|
|
|
|
isAPIKeyMiddleware,
|
|
|
|
s.isAPIKeyMiddleware,
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
bucket := c.Params("bucket")
|
|
|
|
bucket := c.Params("bucket")
|
|
|
|
id := c.Params("id")
|
|
|
|
id := c.Params("id")
|
|
|
|
|
|
|
|
|
|
|
|
if err := r.Database.DeleteBucketObject(bucket, id); err != nil {
|
|
|
|
if err := s.Database.DeleteBucketObject(bucket, id); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return c.JSON("ok")
|
|
|
|
return c.JSON("ok")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
}
|
|
|
|
// API Keys
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api.Get("/api-keys",
|
|
|
|
func (s *Server) ApiKeys(r fiber.Router) {
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
r.Get("/",
|
|
|
|
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
apiKeys, err := r.Database.AllAPIKeys()
|
|
|
|
apiKeys, err := s.Database.AllAPIKeys()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -260,10 +271,10 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
return c.JSON(apiKeys)
|
|
|
|
return c.JSON(apiKeys)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
api.Post("/api-keys",
|
|
|
|
r.Post("/",
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
key, err := r.Database.CreateAPIKey()
|
|
|
|
key, err := s.Database.CreateAPIKey()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -271,20 +282,20 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
return c.JSON(key)
|
|
|
|
return c.JSON(key)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
api.Delete("/api-keys/:key",
|
|
|
|
r.Delete("/:key",
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
if err := r.Database.RemoveAPIKey(c.Params("key")); err != nil {
|
|
|
|
if err := s.Database.RemoveAPIKey(c.Params("key")); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return c.JSON("ok")
|
|
|
|
return c.JSON("ok")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
api.Get("/api-keys/:key",
|
|
|
|
r.Get("/:key",
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
if err := r.Database.CheckAPIKey(c.Params("key")); err != nil {
|
|
|
|
if err := s.Database.CheckAPIKey(c.Params("key")); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|