mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-26 18:28:30 +02:00
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:
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user