mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 17:28:53 +02:00
api: implement several docker apis
This commit is contained in:
57
internal/api/v1/dockerapi/info.go
Normal file
57
internal/api/v1/dockerapi/info.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package dockerapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
dockerSystem "github.com/docker/docker/api/types/system"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||
)
|
||||
|
||||
type DockerInfo dockerSystem.Info
|
||||
|
||||
func (d *DockerInfo) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(map[string]any{
|
||||
"host": d.Name,
|
||||
"containers": map[string]int{
|
||||
"total": d.Containers,
|
||||
"running": d.ContainersRunning,
|
||||
"paused": d.ContainersPaused,
|
||||
"stopped": d.ContainersStopped,
|
||||
},
|
||||
"images": d.Images,
|
||||
"n_cpu": d.NCPU,
|
||||
"memory": strutils.FormatByteSizeWithUnit(d.MemTotal),
|
||||
"version": d.ServerVersion,
|
||||
})
|
||||
}
|
||||
|
||||
func Info(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), reqTimeout)
|
||||
defer cancel()
|
||||
|
||||
dockerClients, ok := getDockerClientsWithErrHandling(w)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
defer closeAllClients(dockerClients)
|
||||
|
||||
errs := gperr.NewBuilder("failed to get docker info")
|
||||
|
||||
dockerInfos := make([]DockerInfo, len(dockerClients))
|
||||
i := 0
|
||||
for name, dockerClient := range dockerClients {
|
||||
info, err := dockerClient.Info(ctx)
|
||||
if err != nil {
|
||||
errs.Add(err)
|
||||
continue
|
||||
}
|
||||
info.Name = name
|
||||
dockerInfos[i] = DockerInfo(info)
|
||||
i++
|
||||
}
|
||||
|
||||
handleResult(w, errs.Error(), dockerInfos)
|
||||
}
|
||||
Reference in New Issue
Block a user