refactor(api): restructured API for type safety, maintainability and docs generation

- These changes makes the API incombatible with previous versions
- Added new types for error handling, success responses, and health checks.
- Updated health check logic to utilize the new types for better clarity and structure.
- Refactored existing handlers to improve response consistency and error handling.
- Updated Makefile to include a new target for generating API types from Swagger.
- Updated "new agent" API to respond an encrypted cert pair
This commit is contained in:
yusing
2025-08-16 13:04:05 +08:00
parent fce9ce21c9
commit 35a3e3fef6
149 changed files with 13173 additions and 2173 deletions

View File

@@ -3,18 +3,17 @@ package monitor
import (
"github.com/docker/docker/api/types/container"
"github.com/yusing/go-proxy/internal/docker"
"github.com/yusing/go-proxy/internal/watcher/health"
"github.com/yusing/go-proxy/internal/types"
)
type DockerHealthMonitor struct {
*monitor
client *docker.SharedClient
containerID string
fallback health.HealthChecker
fallback types.HealthChecker
}
func NewDockerHealthMonitor(client *docker.SharedClient, containerID, alias string, config *health.HealthCheckConfig, fallback health.HealthChecker) *DockerHealthMonitor {
func NewDockerHealthMonitor(client *docker.SharedClient, containerID, alias string, config *types.HealthCheckConfig, fallback types.HealthChecker) *DockerHealthMonitor {
mon := new(DockerHealthMonitor)
mon.client = client
mon.containerID = containerID
@@ -24,7 +23,7 @@ func NewDockerHealthMonitor(client *docker.SharedClient, containerID, alias stri
return mon
}
func (mon *DockerHealthMonitor) CheckHealth() (result *health.HealthCheckResult, err error) {
func (mon *DockerHealthMonitor) CheckHealth() (result *types.HealthCheckResult, err error) {
ctx, cancel := mon.ContextWithTimeout("docker health check timed out")
defer cancel()
cont, err := mon.client.ContainerInspect(ctx, mon.containerID)
@@ -34,12 +33,12 @@ func (mon *DockerHealthMonitor) CheckHealth() (result *health.HealthCheckResult,
status := cont.State.Status
switch status {
case "dead", "exited", "paused", "restarting", "removing":
return &health.HealthCheckResult{
return &types.HealthCheckResult{
Healthy: false,
Detail: "container is " + status,
}, nil
case "created":
return &health.HealthCheckResult{
return &types.HealthCheckResult{
Healthy: false,
Detail: "container is not started",
}, nil
@@ -47,7 +46,7 @@ func (mon *DockerHealthMonitor) CheckHealth() (result *health.HealthCheckResult,
if cont.State.Health == nil {
return mon.fallback.CheckHealth()
}
result = new(health.HealthCheckResult)
result = new(types.HealthCheckResult)
result.Healthy = cont.State.Health.Status == container.Healthy
if len(cont.State.Health.Log) > 0 {
lastLog := cont.State.Health.Log[len(cont.State.Health.Log)-1]