forked from baron/baron-sso
68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"baron-sso-backend/internal/service"
|
|
"runtime"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
type AdminHandler struct {
|
|
Keto service.KetoService
|
|
}
|
|
|
|
func NewAdminHandler(keto service.KetoService) *AdminHandler {
|
|
return &AdminHandler{Keto: keto}
|
|
}
|
|
|
|
func (h *AdminHandler) CheckAuth(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{"status": "ok"})
|
|
}
|
|
|
|
func (h *AdminHandler) CheckPermission(c *fiber.Ctx) error {
|
|
namespace := c.Query("namespace")
|
|
object := c.Query("object")
|
|
relation := c.Query("relation")
|
|
subject := c.Query("subject")
|
|
|
|
if namespace == "" || object == "" || relation == "" || subject == "" {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "namespace, object, relation, and subject are required"})
|
|
}
|
|
|
|
allowed, err := h.Keto.CheckPermission(c.Context(), subject, namespace, object, relation)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
|
|
return c.JSON(fiber.Map{
|
|
"allowed": allowed,
|
|
"query": fiber.Map{
|
|
"namespace": namespace,
|
|
"object": object,
|
|
"relation": relation,
|
|
"subject": subject,
|
|
},
|
|
})
|
|
}
|
|
|
|
// GetSystemStats returns runtime statistics for monitoring
|
|
func (h *AdminHandler) GetSystemStats(c *fiber.Ctx) error {
|
|
var m runtime.MemStats
|
|
runtime.ReadMemStats(&m)
|
|
|
|
stats := fiber.Map{
|
|
"goroutines": runtime.NumGoroutine(),
|
|
"cpus": runtime.NumCPU(),
|
|
"memory": fiber.Map{
|
|
"alloc": m.Alloc,
|
|
"totalAlign": m.TotalAlloc,
|
|
"sys": m.Sys,
|
|
"numGC": m.NumGC,
|
|
},
|
|
"timestamp": time.Now(),
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(stats)
|
|
}
|