first commit
This commit is contained in:
308
internal/handler/admin/settings_handler.go
Normal file
308
internal/handler/admin/settings_handler.go
Normal file
@@ -0,0 +1,308 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"gobeyhan/app/settings/services"
|
||||
"gobeyhan/database/models"
|
||||
"gobeyhan/views/admin/settings" // We will create this package
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type SettingsHandler struct {
|
||||
service *services.SettingsService
|
||||
}
|
||||
|
||||
func NewSettingsHandler() *SettingsHandler {
|
||||
return &SettingsHandler{
|
||||
service: services.NewSettingsService(),
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== WHITELIST ====================
|
||||
|
||||
func (h *SettingsHandler) ListWhitelist(c *gin.Context) {
|
||||
items, err := h.service.GetAllCorsWhitelist()
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Error fetching whitelist")
|
||||
return
|
||||
}
|
||||
settings.WhitelistList(items).Render(c.Request.Context(), c.Writer)
|
||||
}
|
||||
|
||||
func (h *SettingsHandler) NewWhitelist(c *gin.Context) {
|
||||
settings.WhitelistCreate(nil).Render(c.Request.Context(), c.Writer)
|
||||
}
|
||||
|
||||
func (h *SettingsHandler) CreateWhitelist(c *gin.Context) {
|
||||
origin := c.PostForm("origin")
|
||||
description := c.PostForm("description")
|
||||
|
||||
// Basic Validation
|
||||
errors := make(map[string]string)
|
||||
if origin == "" {
|
||||
errors["origin"] = "Origin is required"
|
||||
}
|
||||
|
||||
if len(errors) > 0 {
|
||||
settings.WhitelistCreate(errors).Render(c.Request.Context(), c.Writer)
|
||||
return
|
||||
}
|
||||
|
||||
item := &models.CorsWhitelist{
|
||||
Origin: origin,
|
||||
Description: description,
|
||||
IsActive: true,
|
||||
}
|
||||
|
||||
if err := h.service.CreateCorsWhitelist(item); err != nil {
|
||||
errors["origin"] = "Error creating whitelist entry: " + err.Error()
|
||||
settings.WhitelistCreate(errors).Render(c.Request.Context(), c.Writer)
|
||||
return
|
||||
}
|
||||
|
||||
c.Redirect(http.StatusSeeOther, "/admin/settings/whitelist")
|
||||
}
|
||||
|
||||
func (h *SettingsHandler) EditWhitelist(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
c.String(http.StatusBadRequest, "Invalid ID")
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.service.GetCorsWhitelistByID(id)
|
||||
if err != nil {
|
||||
c.String(http.StatusNotFound, "Item not found")
|
||||
return
|
||||
}
|
||||
|
||||
settings.WhitelistEdit(item, nil).Render(c.Request.Context(), c.Writer)
|
||||
}
|
||||
|
||||
func (h *SettingsHandler) UpdateWhitelist(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
origin := c.PostForm("origin")
|
||||
description := c.PostForm("description")
|
||||
|
||||
// Basic Validation
|
||||
errors := make(map[string]string)
|
||||
if origin == "" {
|
||||
errors["origin"] = "Origin is required"
|
||||
}
|
||||
|
||||
if len(errors) > 0 {
|
||||
// Fetch item again to display form with errors
|
||||
idUint, _ := strconv.ParseUint(id, 10, 64)
|
||||
item, _ := h.service.GetCorsWhitelistByID(idUint)
|
||||
if item == nil {
|
||||
c.String(http.StatusNotFound, "Item not found")
|
||||
return
|
||||
}
|
||||
// Preserve user input
|
||||
item.Origin = origin
|
||||
item.Description = description
|
||||
settings.WhitelistEdit(item, errors).Render(c.Request.Context(), c.Writer)
|
||||
return
|
||||
}
|
||||
|
||||
updates := map[string]interface{}{
|
||||
"origin": origin,
|
||||
"description": description,
|
||||
}
|
||||
|
||||
if err := h.service.UpdateCorsWhitelist(id, updates); err != nil {
|
||||
idUint, _ := strconv.ParseUint(id, 10, 64)
|
||||
item, _ := h.service.GetCorsWhitelistByID(idUint)
|
||||
settings.WhitelistEdit(item, map[string]string{"origin": "Error updating: " + err.Error()}).Render(c.Request.Context(), c.Writer)
|
||||
return
|
||||
}
|
||||
|
||||
c.Redirect(http.StatusSeeOther, "/admin/settings/whitelist")
|
||||
}
|
||||
|
||||
func (h *SettingsHandler) DeleteWhitelist(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
if err := h.service.DeleteCorsWhitelist(id); err != nil { // Service takes string ID
|
||||
c.String(http.StatusInternalServerError, "Error deleting item")
|
||||
return
|
||||
}
|
||||
c.Redirect(http.StatusSeeOther, "/admin/settings/whitelist")
|
||||
}
|
||||
|
||||
// ==================== BLACKLIST ====================
|
||||
|
||||
func (h *SettingsHandler) ListBlacklist(c *gin.Context) {
|
||||
items, err := h.service.GetAllCorsBlacklist()
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Error fetching blacklist")
|
||||
return
|
||||
}
|
||||
settings.BlacklistList(items).Render(c.Request.Context(), c.Writer)
|
||||
}
|
||||
|
||||
func (h *SettingsHandler) NewBlacklist(c *gin.Context) {
|
||||
settings.BlacklistCreate(nil).Render(c.Request.Context(), c.Writer)
|
||||
}
|
||||
|
||||
func (h *SettingsHandler) CreateBlacklist(c *gin.Context) {
|
||||
origin := c.PostForm("origin")
|
||||
description := c.PostForm("description")
|
||||
|
||||
errors := make(map[string]string)
|
||||
if origin == "" {
|
||||
errors["origin"] = "Origin is required"
|
||||
}
|
||||
|
||||
if len(errors) > 0 {
|
||||
settings.BlacklistCreate(errors).Render(c.Request.Context(), c.Writer)
|
||||
return
|
||||
}
|
||||
|
||||
item := &models.CorsBlacklist{
|
||||
Origin: origin,
|
||||
Reason: description,
|
||||
IsActive: true,
|
||||
}
|
||||
|
||||
if err := h.service.CreateCorsBlacklist(item); err != nil {
|
||||
errors["origin"] = "Error creating entry: " + err.Error()
|
||||
settings.BlacklistCreate(errors).Render(c.Request.Context(), c.Writer)
|
||||
return
|
||||
}
|
||||
|
||||
c.Redirect(http.StatusSeeOther, "/admin/settings/blacklist")
|
||||
}
|
||||
|
||||
func (h *SettingsHandler) DeleteBlacklist(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
if err := h.service.DeleteCorsBlacklist(id); err != nil {
|
||||
c.String(http.StatusInternalServerError, "Error deleting item")
|
||||
return
|
||||
}
|
||||
c.Redirect(http.StatusSeeOther, "/admin/settings/blacklist")
|
||||
}
|
||||
|
||||
func (h *SettingsHandler) EditBlacklist(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
c.String(http.StatusBadRequest, "Invalid ID")
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.service.GetCorsBlacklistByID(id)
|
||||
if err != nil {
|
||||
c.String(http.StatusNotFound, "Item not found")
|
||||
return
|
||||
}
|
||||
|
||||
settings.BlacklistEdit(item, nil).Render(c.Request.Context(), c.Writer)
|
||||
}
|
||||
|
||||
func (h *SettingsHandler) UpdateBlacklist(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
origin := c.PostForm("origin")
|
||||
reason := c.PostForm("reason")
|
||||
|
||||
errors := make(map[string]string)
|
||||
if origin == "" {
|
||||
errors["origin"] = "Origin is required"
|
||||
}
|
||||
|
||||
if len(errors) > 0 {
|
||||
idUint, _ := strconv.ParseUint(id, 10, 64)
|
||||
item, _ := h.service.GetCorsBlacklistByID(idUint)
|
||||
if item == nil {
|
||||
c.String(http.StatusNotFound, "Item not found")
|
||||
return
|
||||
}
|
||||
item.Origin = origin
|
||||
item.Reason = reason
|
||||
settings.BlacklistEdit(item, errors).Render(c.Request.Context(), c.Writer)
|
||||
return
|
||||
}
|
||||
|
||||
updates := map[string]interface{}{
|
||||
"origin": origin,
|
||||
"reason": reason,
|
||||
}
|
||||
|
||||
if err := h.service.UpdateCorsBlacklist(id, updates); err != nil {
|
||||
idUint, _ := strconv.ParseUint(id, 10, 64)
|
||||
item, _ := h.service.GetCorsBlacklistByID(idUint)
|
||||
settings.BlacklistEdit(item, map[string]string{"origin": "Error updating: " + err.Error()}).Render(c.Request.Context(), c.Writer)
|
||||
return
|
||||
}
|
||||
|
||||
c.Redirect(http.StatusSeeOther, "/admin/settings/blacklist")
|
||||
}
|
||||
|
||||
// ==================== RATE LIMITS ====================
|
||||
|
||||
func (h *SettingsHandler) ListRateLimits(c *gin.Context) {
|
||||
items, err := h.service.GetAllRateLimitSettings()
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Error fetching rate limits")
|
||||
return
|
||||
}
|
||||
settings.RateLimitList(items).Render(c.Request.Context(), c.Writer)
|
||||
}
|
||||
|
||||
func (h *SettingsHandler) EditRateLimit(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
c.String(http.StatusBadRequest, "Invalid ID")
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.service.GetRateLimitSettingByID(id)
|
||||
if err != nil {
|
||||
c.String(http.StatusNotFound, "Item not found")
|
||||
return
|
||||
}
|
||||
|
||||
settings.RateLimitEdit(item, nil).Render(c.Request.Context(), c.Writer)
|
||||
}
|
||||
|
||||
func (h *SettingsHandler) UpdateRateLimit(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
limitStr := c.PostForm("max_requests")
|
||||
windowStr := c.PostForm("window_seconds")
|
||||
description := c.PostForm("description")
|
||||
|
||||
limit, _ := strconv.ParseInt(limitStr, 10, 64)
|
||||
window, _ := strconv.Atoi(windowStr)
|
||||
|
||||
updates := map[string]interface{}{
|
||||
"description": description,
|
||||
}
|
||||
if limit > 0 {
|
||||
updates["max_requests"] = limit
|
||||
}
|
||||
if window > 0 {
|
||||
updates["window_seconds"] = window
|
||||
}
|
||||
|
||||
if err := h.service.UpdateRateLimitSetting(id, updates); err != nil {
|
||||
// Handle error (redisplay form)
|
||||
idUint, _ := strconv.ParseUint(id, 10, 64)
|
||||
item, _ := h.service.GetRateLimitSettingByID(idUint)
|
||||
settings.RateLimitEdit(item, map[string]string{"general": "Error updating: " + err.Error()}).Render(c.Request.Context(), c.Writer)
|
||||
return
|
||||
}
|
||||
|
||||
c.Redirect(http.StatusSeeOther, "/admin/settings/rate-limits")
|
||||
}
|
||||
|
||||
func (h *SettingsHandler) DeleteRateLimit(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
if err := h.service.DeleteRateLimitSetting(id); err != nil {
|
||||
c.String(http.StatusInternalServerError, "Error deleting item")
|
||||
return
|
||||
}
|
||||
c.Redirect(http.StatusSeeOther, "/admin/settings/rate-limits")
|
||||
}
|
||||
Reference in New Issue
Block a user