265 lines
7.8 KiB
Go
265 lines
7.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"gobeyhan/app/settings/services"
|
|
"gobeyhan/database/models"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type SettingsHandler struct {
|
|
service *services.SettingsService
|
|
}
|
|
|
|
func NewSettingsHandler(service *services.SettingsService) *SettingsHandler {
|
|
return &SettingsHandler{service: service}
|
|
}
|
|
|
|
// GetAllWhitelist godoc
|
|
// @Summary Get all CORS whitelist entries (Admin)
|
|
// @Description Get all CORS whitelist origins
|
|
// @Tags admin,settings
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Success 200 {array} models.CorsWhitelist
|
|
// @Router /api/v1/admin/cors/whitelist [get]
|
|
func (h *SettingsHandler) GetAllWhitelist(c *gin.Context) {
|
|
whitelist, err := h.service.GetAllCorsWhitelist()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"data": whitelist})
|
|
}
|
|
|
|
// CreateWhitelist godoc
|
|
// @Summary Create CORS whitelist entry (Admin)
|
|
// @Description Add a new origin to CORS whitelist
|
|
// @Tags admin,settings
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param whitelist body models.CorsWhitelist true "Whitelist object"
|
|
// @Success 201 {object} models.CorsWhitelist
|
|
// @Router /api/v1/admin/cors/whitelist [post]
|
|
func (h *SettingsHandler) CreateWhitelist(c *gin.Context) {
|
|
var input models.CorsWhitelist
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if err := h.service.CreateCorsWhitelist(&input); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, gin.H{"data": input})
|
|
}
|
|
|
|
// UpdateWhitelist godoc
|
|
// @Summary Update CORS whitelist entry (Admin)
|
|
// @Description Update an existing CORS whitelist entry
|
|
// @Tags admin,settings
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Whitelist ID"
|
|
// @Param whitelist body models.CorsWhitelist true "Whitelist object"
|
|
// @Success 200 {object} map[string]string
|
|
// @Router /api/v1/admin/cors/whitelist/{id} [put]
|
|
func (h *SettingsHandler) UpdateWhitelist(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
var input map[string]interface{}
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if err := h.service.UpdateCorsWhitelist(id, input); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Whitelist updated successfully"})
|
|
}
|
|
|
|
// DeleteWhitelist godoc
|
|
// @Summary Delete CORS whitelist entry (Admin)
|
|
// @Description Delete a CORS whitelist entry
|
|
// @Tags admin,settings
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Whitelist ID"
|
|
// @Success 200 {object} map[string]string
|
|
// @Router /api/v1/admin/cors/whitelist/{id} [delete]
|
|
func (h *SettingsHandler) DeleteWhitelist(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
if err := h.service.DeleteCorsWhitelist(id); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Whitelist deleted successfully"})
|
|
}
|
|
|
|
// GetAllBlacklist godoc
|
|
// @Summary Get all CORS blacklist entries (Admin)
|
|
// @Description Get all CORS blacklist origins
|
|
// @Tags admin,settings
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Success 200 {array} models.CorsBlacklist
|
|
// @Router /api/v1/admin/cors/blacklist [get]
|
|
func (h *SettingsHandler) GetAllBlacklist(c *gin.Context) {
|
|
blacklist, err := h.service.GetAllCorsBlacklist()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"data": blacklist})
|
|
}
|
|
|
|
// CreateBlacklist godoc
|
|
// @Summary Create CORS blacklist entry (Admin)
|
|
// @Description Add a new origin to CORS blacklist
|
|
// @Tags admin,settings
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param blacklist body models.CorsBlacklist true "Blacklist object"
|
|
// @Success 201 {object} models.CorsBlacklist
|
|
// @Router /api/v1/admin/cors/blacklist [post]
|
|
func (h *SettingsHandler) CreateBlacklist(c *gin.Context) {
|
|
var input models.CorsBlacklist
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if err := h.service.CreateCorsBlacklist(&input); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, gin.H{"data": input})
|
|
}
|
|
|
|
// UpdateBlacklist godoc
|
|
// @Summary Update CORS blacklist entry (Admin)
|
|
// @Description Update an existing CORS blacklist entry
|
|
// @Tags admin,settings
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Blacklist ID"
|
|
// @Param blacklist body models.CorsBlacklist true "Blacklist object"
|
|
// @Success 200 {object} map[string]string
|
|
// @Router /api/v1/admin/cors/blacklist/{id} [put]
|
|
func (h *SettingsHandler) UpdateBlacklist(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
var input map[string]interface{}
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if err := h.service.UpdateCorsBlacklist(id, input); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Blacklist updated successfully"})
|
|
}
|
|
|
|
// DeleteBlacklist godoc
|
|
// @Summary Delete CORS blacklist entry (Admin)
|
|
// @Description Delete a CORS blacklist entry
|
|
// @Tags admin,settings
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Blacklist ID"
|
|
// @Success 200 {object} map[string]string
|
|
// @Router /api/v1/admin/cors/blacklist/{id} [delete]
|
|
func (h *SettingsHandler) DeleteBlacklist(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
if err := h.service.DeleteCorsBlacklist(id); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Blacklist deleted successfully"})
|
|
}
|
|
|
|
// GetAllRateLimits godoc
|
|
// @Summary Get all rate limit settings (Admin)
|
|
// @Description Get all rate limit configurations
|
|
// @Tags admin,settings
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Success 200 {array} models.RateLimitSetting
|
|
// @Router /api/v1/admin/rate-limits [get]
|
|
func (h *SettingsHandler) GetAllRateLimits(c *gin.Context) {
|
|
settings, err := h.service.GetAllRateLimitSettings()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"data": settings})
|
|
}
|
|
|
|
// UpdateRateLimit godoc
|
|
// @Summary Update rate limit setting (Admin)
|
|
// @Description Update an existing rate limit configuration
|
|
// @Tags admin,settings
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Rate Limit ID"
|
|
// @Param setting body models.RateLimitSetting true "Rate limit object"
|
|
// @Success 200 {object} map[string]string
|
|
// @Router /api/v1/admin/rate-limits/{id} [put]
|
|
func (h *SettingsHandler) UpdateRateLimit(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
var input map[string]interface{}
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if err := h.service.UpdateRateLimitSetting(id, input); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Rate limit updated successfully"})
|
|
}
|
|
|
|
// InvalidateCorsCache godoc
|
|
// @Summary Invalidate CORS cache (Admin)
|
|
// @Description Clear the CORS cache to force reload from database
|
|
// @Tags admin,settings
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Success 200 {object} map[string]string
|
|
// @Router /api/v1/admin/cors/cache/invalidate [post]
|
|
func (h *SettingsHandler) InvalidateCorsCache(c *gin.Context) {
|
|
h.service.InvalidateCorsCache()
|
|
c.JSON(http.StatusOK, gin.H{"message": "CORS cache invalidated successfully"})
|
|
}
|