Files

35 lines
965 B
Go

package handlers
import (
"github.com/gin-gonic/gin"
"net/http"
"mengyastore-backend/internal/config"
"mengyastore-backend/internal/storage"
)
// AdminHandler holds dependencies for all admin-related routes.
type AdminHandler struct {
store *storage.JSONStore
cfg *config.Config
siteStore *storage.SiteStore
orderStore *storage.OrderStore
chatStore *storage.ChatStore
}
func NewAdminHandler(store *storage.JSONStore, cfg *config.Config, siteStore *storage.SiteStore, orderStore *storage.OrderStore, chatStore *storage.ChatStore) *AdminHandler {
return &AdminHandler{store: store, cfg: cfg, siteStore: siteStore, orderStore: orderStore, chatStore: chatStore}
}
func (h *AdminHandler) requireAdmin(c *gin.Context) bool {
token := c.Query("token")
if token == "" {
token = c.GetHeader("Authorization")
}
if token == h.cfg.AdminToken {
return true
}
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return false
}