|
|
|
@ -20,30 +20,23 @@ func (r *Server) isAdminMiddleware(c *fiber.Ctx) error {
|
|
|
|
|
return c.Next()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Server) Api(api fiber.Router) {
|
|
|
|
|
isAPIKeyMiddleware := func(c *fiber.Ctx) error {
|
|
|
|
|
if _, isAdmin := r.adminSessions[c.Cookies("sid")]; !isAdmin { // if admin continue
|
|
|
|
|
func (s *Server) isAPIKeyMiddleware(c *fiber.Ctx) error {
|
|
|
|
|
if _, isAdmin := s.adminSessions[c.Cookies("sid")]; !isAdmin { // if admin continue
|
|
|
|
|
token := c.Cookies("token")
|
|
|
|
|
if 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 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)
|
|
|
|
|
return c.Next()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
Password string `form:"password"`
|
|
|
|
|
}
|
|
|
|
@ -57,7 +50,7 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token := utils.GenerateRandomString(32)
|
|
|
|
|
r.adminSessions[token] = struct{}{}
|
|
|
|
|
s.adminSessions[token] = struct{}{}
|
|
|
|
|
|
|
|
|
|
c.Cookie(&fiber.Cookie{
|
|
|
|
|
Name: "sid",
|
|
|
|
@ -69,23 +62,38 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
|
return c.Redirect("/")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
api.Get("/status", func(c *fiber.Ctx) error {
|
|
|
|
|
return c.JSON("ok")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
api.Get("/current-user",
|
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
|
if _, found := r.adminSessions[c.Cookies("sid")]; !found {
|
|
|
|
|
r.Get("/current-user", func(c *fiber.Ctx) error {
|
|
|
|
|
if _, found := s.adminSessions[c.Cookies("sid")]; !found {
|
|
|
|
|
return c.JSON("anonymous")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.JSON("admin")
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Server) Api(r fiber.Router) {
|
|
|
|
|
|
|
|
|
|
api.Get("/dashboard-state",
|
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
|
r.Get("/status", func(c *fiber.Ctx) error {
|
|
|
|
|
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 {
|
|
|
|
|
state, err := r.Database.GetDashboardState()
|
|
|
|
|
state, err := s.Database.GetDashboardState()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -93,8 +101,8 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
|
return c.JSON(state)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
api.Post("/dashboard-state",
|
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
|
r.Post("/dashboard-state",
|
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
|
|
|
|
|
|
var state database.DashboardState
|
|
|
|
@ -103,17 +111,19 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := r.Database.SetDashboardState(state); err != nil {
|
|
|
|
|
if err := s.Database.SetDashboardState(state); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.JSON("ok")
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
api.Get("/buckets",
|
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
|
func (s *Server) ApiBucket(r fiber.Router) {
|
|
|
|
|
r.Get("/",
|
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
|
buckets, err := r.Database.AllBuckets()
|
|
|
|
|
buckets, err := s.Database.AllBuckets()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -121,8 +131,8 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
|
return c.JSON(buckets)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
api.Post("/buckets",
|
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
|
r.Post("/",
|
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
|
var req struct {
|
|
|
|
|
Bucket string `json:"bucket"`
|
|
|
|
@ -140,19 +150,21 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
|
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 c.JSON("ok")
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
api.Get("/buckets/:bucket",
|
|
|
|
|
isAPIKeyMiddleware,
|
|
|
|
|
func (s *Server) ApiBucketObjects(r fiber.Router) {
|
|
|
|
|
r.Get("/",
|
|
|
|
|
s.isAPIKeyMiddleware,
|
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
|
bucket := c.Params("bucket")
|
|
|
|
|
|
|
|
|
|
objects, err := r.Database.AllBucketObjects(bucket)
|
|
|
|
|
objects, err := s.Database.AllBucketObjects(bucket)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -160,12 +172,12 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
|
return c.JSON(objects)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
api.Get("/buckets/:bucket/settings",
|
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
|
r.Get("/settings",
|
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
|
bucket := c.Params("bucket")
|
|
|
|
|
|
|
|
|
|
settings, err := r.Database.GetBucketSettings(bucket)
|
|
|
|
|
settings, err := s.Database.GetBucketSettings(bucket)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -173,8 +185,8 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
|
return c.JSON(settings)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
api.Post("/buckets/:bucket/settings",
|
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
|
r.Post("/settings",
|
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
|
bucket := c.Params("bucket")
|
|
|
|
|
|
|
|
|
@ -184,15 +196,15 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := r.Database.SetBucketSettings(bucket, settings); err != nil {
|
|
|
|
|
if err := s.Database.SetBucketSettings(bucket, settings); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.JSON("ok")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
api.Post("/buckets/:bucket",
|
|
|
|
|
isAPIKeyMiddleware,
|
|
|
|
|
r.Post("/",
|
|
|
|
|
s.isAPIKeyMiddleware,
|
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
|
bucket := c.Params("bucket")
|
|
|
|
|
|
|
|
|
@ -206,7 +218,7 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
id, err := r.Database.CreateBucketObject(bucket, mf)
|
|
|
|
|
id, err := s.Database.CreateBucketObject(bucket, mf)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -217,42 +229,41 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
api.Get("/buckets/:bucket/:id",
|
|
|
|
|
isAPIKeyMiddleware,
|
|
|
|
|
r.Get("/:id",
|
|
|
|
|
s.isAPIKeyMiddleware,
|
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
|
bucket := c.Params("bucket")
|
|
|
|
|
id := c.Params("id")
|
|
|
|
|
|
|
|
|
|
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 c.SendStream(buf)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
api.Delete("/buckets/:bucket/:id",
|
|
|
|
|
isAPIKeyMiddleware,
|
|
|
|
|
r.Delete("/:id",
|
|
|
|
|
s.isAPIKeyMiddleware,
|
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
|
bucket := c.Params("bucket")
|
|
|
|
|
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 c.JSON("ok")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// API Keys
|
|
|
|
|
//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
api.Get("/api-keys",
|
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
|
func (s *Server) ApiKeys(r fiber.Router) {
|
|
|
|
|
r.Get("/",
|
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
|
apiKeys, err := r.Database.AllAPIKeys()
|
|
|
|
|
apiKeys, err := s.Database.AllAPIKeys()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -260,10 +271,10 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
|
return c.JSON(apiKeys)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
api.Post("/api-keys",
|
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
|
r.Post("/",
|
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
|
func(c *fiber.Ctx) error {
|
|
|
|
|
key, err := r.Database.CreateAPIKey()
|
|
|
|
|
key, err := s.Database.CreateAPIKey()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -271,20 +282,20 @@ func (r *Server) Api(api fiber.Router) {
|
|
|
|
|
return c.JSON(key)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
api.Delete("/api-keys/:key",
|
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
|
r.Delete("/:key",
|
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
|
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 c.JSON("ok")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
api.Get("/api-keys/:key",
|
|
|
|
|
r.isAdminMiddleware,
|
|
|
|
|
r.Get("/:key",
|
|
|
|
|
s.isAdminMiddleware,
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|