mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-17 05:59:42 +02:00
refactor docker api code, deps upgrade
This commit is contained in:
@@ -4,14 +4,9 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"github.com/coder/websocket/wsjson"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
"github.com/yusing/go-proxy/internal/net/gphttp/gpwebsocket"
|
||||
"github.com/yusing/go-proxy/internal/net/gphttp/httpheaders"
|
||||
)
|
||||
|
||||
type Container struct {
|
||||
@@ -23,30 +18,10 @@ type Container struct {
|
||||
}
|
||||
|
||||
func Containers(w http.ResponseWriter, r *http.Request) {
|
||||
if httpheaders.IsWebsocket(r.Header) {
|
||||
gpwebsocket.Periodic(w, r, 5*time.Second, func(conn *websocket.Conn) error {
|
||||
containers, err := listContainers(r.Context())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return wsjson.Write(r.Context(), conn, containers)
|
||||
})
|
||||
} else {
|
||||
containers, err := listContainers(r.Context())
|
||||
handleResult(w, err, containers)
|
||||
}
|
||||
serveHTTP[Container, []Container](w, r, GetContainers)
|
||||
}
|
||||
|
||||
func listContainers(ctx context.Context) ([]Container, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, reqTimeout)
|
||||
defer cancel()
|
||||
|
||||
dockerClients, err := getDockerClients()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer closeAllClients(dockerClients)
|
||||
|
||||
func GetContainers(ctx context.Context, dockerClients DockerClients) ([]Container, gperr.Error) {
|
||||
errs := gperr.NewBuilder("failed to get containers")
|
||||
containers := make([]Container, 0)
|
||||
for server, dockerClient := range dockerClients {
|
||||
|
||||
Reference in New Issue
Block a user