feat: add Docker container management functionality
- Implement DockerHandler to handle API requests for listing Docker containers. - Create DockerService to interact with Docker API and retrieve container information. - Add routes for Docker container management in the API. - Introduce frontend API integration for Docker container listing. - Enhance ProxyHostForm to allow quick selection of Docker containers. - Update Docker-related tests to ensure functionality and error handling. - Modify Docker Compose files to enable Docker socket access for local and remote environments. - Add TypeScript configurations for improved build processes.
This commit is contained in:
31
backend/internal/api/handlers/docker_handler.go
Normal file
31
backend/internal/api/handlers/docker_handler.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/Wikid82/CaddyProxyManagerPlus/backend/internal/services"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type DockerHandler struct {
|
||||
dockerService *services.DockerService
|
||||
}
|
||||
|
||||
func NewDockerHandler(dockerService *services.DockerService) *DockerHandler {
|
||||
return &DockerHandler{dockerService: dockerService}
|
||||
}
|
||||
|
||||
func (h *DockerHandler) RegisterRoutes(r *gin.RouterGroup) {
|
||||
r.GET("/docker/containers", h.ListContainers)
|
||||
}
|
||||
|
||||
func (h *DockerHandler) ListContainers(c *gin.Context) {
|
||||
host := c.Query("host")
|
||||
containers, err := h.dockerService.ListContainers(c.Request.Context(), host)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list containers: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, containers)
|
||||
}
|
||||
40
backend/internal/api/handlers/docker_handler_test.go
Normal file
40
backend/internal/api/handlers/docker_handler_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/Wikid82/CaddyProxyManagerPlus/backend/internal/services"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDockerHandler_ListContainers(t *testing.T) {
|
||||
// We can't easily mock the DockerService without an interface,
|
||||
// and the DockerService depends on the real Docker client.
|
||||
// So we'll just test that the handler is wired up correctly,
|
||||
// even if it returns an error because Docker isn't running in the test env.
|
||||
|
||||
svc, _ := services.NewDockerService()
|
||||
// svc might be nil if docker is not available, but NewDockerHandler handles nil?
|
||||
// Actually NewDockerHandler just stores it.
|
||||
// If svc is nil, ListContainers will panic.
|
||||
// So we only run this if svc is not nil.
|
||||
|
||||
if svc == nil {
|
||||
t.Skip("Docker not available")
|
||||
}
|
||||
|
||||
h := NewDockerHandler(svc)
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
h.RegisterRoutes(r.Group("/"))
|
||||
|
||||
req, _ := http.NewRequest("GET", "/docker/containers", nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
// It might return 200 or 500 depending on if ListContainers succeeds
|
||||
assert.Contains(t, []int{http.StatusOK, http.StatusInternalServerError}, w.Code)
|
||||
}
|
||||
@@ -93,6 +93,15 @@ func Register(router *gin.Engine, db *gorm.DB, cfg config.Config) error {
|
||||
protected.POST("/notifications/:id/read", notificationHandler.MarkAsRead)
|
||||
protected.POST("/notifications/read-all", notificationHandler.MarkAllAsRead)
|
||||
|
||||
// Docker
|
||||
dockerService, err := services.NewDockerService()
|
||||
if err == nil { // Only register if Docker is available
|
||||
dockerHandler := handlers.NewDockerHandler(dockerService)
|
||||
dockerHandler.RegisterRoutes(protected)
|
||||
} else {
|
||||
fmt.Printf("Warning: Docker service unavailable: %v\n", err)
|
||||
}
|
||||
|
||||
// Uptime Service
|
||||
uptimeService := services.NewUptimeService(db, notificationService)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user