224a53975d
- Introduced isolated coverage tests for ProxyHosts with various scenarios including rendering, bulk apply, and link behavior. - Enhanced existing ProxyHosts coverage tests to include additional assertions and error handling. - Added tests for Uptime component to verify rendering and monitoring toggling functionality. - Created utility functions for setting labels and help texts related to proxy host settings. - Implemented bulk settings application logic with progress tracking and error handling. - Added toast utility tests to ensure callback functionality and ID incrementing. - Improved type safety in test files by using appropriate TypeScript types.
74 lines
1.9 KiB
Go
74 lines
1.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/Wikid82/charon/backend/internal/services"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type UptimeHandler struct {
|
|
service *services.UptimeService
|
|
}
|
|
|
|
func NewUptimeHandler(service *services.UptimeService) *UptimeHandler {
|
|
return &UptimeHandler{service: service}
|
|
}
|
|
|
|
func (h *UptimeHandler) List(c *gin.Context) {
|
|
monitors, err := h.service.ListMonitors()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list monitors"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, monitors)
|
|
}
|
|
|
|
func (h *UptimeHandler) GetHistory(c *gin.Context) {
|
|
id := c.Param("id")
|
|
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "50"))
|
|
|
|
history, err := h.service.GetMonitorHistory(id, limit)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get history"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, history)
|
|
}
|
|
|
|
func (h *UptimeHandler) Update(c *gin.Context) {
|
|
id := c.Param("id")
|
|
var updates map[string]interface{}
|
|
if err := c.ShouldBindJSON(&updates); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
monitor, err := h.service.UpdateMonitor(id, updates)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, monitor)
|
|
}
|
|
|
|
func (h *UptimeHandler) Sync(c *gin.Context) {
|
|
if err := h.service.SyncMonitors(); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync monitors"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "Sync started"})
|
|
}
|
|
|
|
// Delete removes a monitor and its associated data
|
|
func (h *UptimeHandler) Delete(c *gin.Context) {
|
|
id := c.Param("id")
|
|
if err := h.service.DeleteMonitor(id); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete monitor"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "Monitor deleted"})
|
|
}
|