- Add ProxyHost, CaddyConfig, RemoteServer, SSL, AccessList, User, Setting, ImportSession models - Implement ProxyHostService and RemoteServerService with domain/name uniqueness validation - Add Caddyfile import handler with conflict detection and user review workflow - Create ProxyHostHandler and RemoteServerHandler with full CRUD operations - Wire up Gin/GORM/SQLite dependencies and create missing internal packages - Add database connection layer, server routing, and version info - Update routes to register all new handlers and auto-migrate models - Configure import environment variables and mount points Addresses Issue #5 (data persistence) and Issue #43 (remote servers) Backend now compiles cleanly with go build
124 lines
2.9 KiB
Go
124 lines
2.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/Wikid82/CaddyProxyManagerPlus/backend/internal/models"
|
|
"github.com/Wikid82/CaddyProxyManagerPlus/backend/internal/services"
|
|
)
|
|
|
|
// ProxyHostHandler handles CRUD operations for proxy hosts.
|
|
type ProxyHostHandler struct {
|
|
service *services.ProxyHostService
|
|
}
|
|
|
|
// NewProxyHostHandler creates a new proxy host handler.
|
|
func NewProxyHostHandler(db *gorm.DB) *ProxyHostHandler {
|
|
return &ProxyHostHandler{
|
|
service: services.NewProxyHostService(db),
|
|
}
|
|
}
|
|
|
|
// RegisterRoutes registers proxy host routes.
|
|
func (h *ProxyHostHandler) RegisterRoutes(router *gin.RouterGroup) {
|
|
router.GET("/proxy-hosts", h.List)
|
|
router.POST("/proxy-hosts", h.Create)
|
|
router.GET("/proxy-hosts/:uuid", h.Get)
|
|
router.PUT("/proxy-hosts/:uuid", h.Update)
|
|
router.DELETE("/proxy-hosts/:uuid", h.Delete)
|
|
}
|
|
|
|
// List retrieves all proxy hosts.
|
|
func (h *ProxyHostHandler) List(c *gin.Context) {
|
|
hosts, err := h.service.List()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, hosts)
|
|
}
|
|
|
|
// Create creates a new proxy host.
|
|
func (h *ProxyHostHandler) Create(c *gin.Context) {
|
|
var host models.ProxyHost
|
|
if err := c.ShouldBindJSON(&host); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
host.UUID = uuid.NewString()
|
|
|
|
if err := h.service.Create(&host); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, host)
|
|
}
|
|
|
|
// Get retrieves a proxy host by UUID.
|
|
func (h *ProxyHostHandler) Get(c *gin.Context) {
|
|
uuid := c.Param("uuid")
|
|
|
|
host, err := h.service.GetByUUID(uuid)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "proxy host not found"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, host)
|
|
}
|
|
|
|
// Update updates an existing proxy host.
|
|
func (h *ProxyHostHandler) Update(c *gin.Context) {
|
|
uuid := c.Param("uuid")
|
|
|
|
host, err := h.service.GetByUUID(uuid)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "proxy host not found"})
|
|
return
|
|
}
|
|
|
|
if err := c.ShouldBindJSON(host); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if err := h.service.Update(host); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, host)
|
|
}
|
|
|
|
// Delete removes a proxy host.
|
|
func (h *ProxyHostHandler) Delete(c *gin.Context) {
|
|
uuid := c.Param("uuid")
|
|
|
|
host, err := h.service.GetByUUID(uuid)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "proxy host not found"})
|
|
return
|
|
}
|
|
|
|
if err := h.service.Delete(host.ID); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "proxy host deleted"})
|
|
}
|
|
|
|
// HealthHandler returns a simple health check response.
|
|
func HealthHandler(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"status": "ok",
|
|
})
|
|
}
|