mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 04:42:33 +02:00

* feat: idle sleep for proxmox LXCs * refactor: replace deprecated docker api types * chore(api): remove debug task list endpoint * refactor: move servemux to gphttp/servemux; favicon.go to v1/favicon * refactor: introduce Pool interface, move agent_pool to agent module * refactor: simplify api code * feat: introduce debug api * refactor: remove net.URL and net.CIDR types, improved unmarshal handling * chore: update Makefile for debug build tag, update README * chore: add gperr.Unwrap method * feat: relative time and duration formatting * chore: add ROOT_DIR environment variable, refactor * migration: move homepage override and icon cache to $BASE_DIR/data, add migration code * fix: nil dereference on marshalling service health * fix: wait for route deletion * chore: enhance tasks debuggability * feat: stdout access logger and MultiWriter * fix(agent): remove agent properly on verify error * fix(metrics): disk exclusion logic and added corresponding tests * chore: update schema and prettify, fix package.json and Makefile * fix: I/O buffer not being shrunk before putting back to pool * feat: enhanced error handling module * chore: deps upgrade * feat: better value formatting and handling --------- Co-authored-by: yusing <yusing@6uo.me>
125 lines
3.2 KiB
Go
125 lines
3.2 KiB
Go
package dockerapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/coder/websocket"
|
|
"github.com/coder/websocket/wsjson"
|
|
"github.com/yusing/go-proxy/agent/pkg/agent"
|
|
config "github.com/yusing/go-proxy/internal/config/types"
|
|
"github.com/yusing/go-proxy/internal/docker"
|
|
"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 (
|
|
DockerClients map[string]*docker.SharedClient
|
|
ResultType[T any] interface {
|
|
map[string]T | []T
|
|
}
|
|
)
|
|
|
|
// getDockerClients returns a map of docker clients for the current config.
|
|
//
|
|
// Returns a map of docker clients by server name and an error if any.
|
|
//
|
|
// Even if there are errors, the map of docker clients might not be empty.
|
|
func getDockerClients() (DockerClients, gperr.Error) {
|
|
cfg := config.GetInstance()
|
|
|
|
dockerHosts := cfg.Value().Providers.Docker
|
|
dockerClients := make(DockerClients)
|
|
|
|
connErrs := gperr.NewBuilder("failed to connect to docker")
|
|
|
|
for name, host := range dockerHosts {
|
|
dockerClient, err := docker.NewClient(host)
|
|
if err != nil {
|
|
connErrs.Add(err)
|
|
continue
|
|
}
|
|
dockerClients[name] = dockerClient
|
|
}
|
|
|
|
for _, agent := range agent.Agents.Iter {
|
|
dockerClient, err := docker.NewClient(agent.FakeDockerHost())
|
|
if err != nil {
|
|
connErrs.Add(err)
|
|
continue
|
|
}
|
|
dockerClients[agent.Name()] = dockerClient
|
|
}
|
|
|
|
return dockerClients, connErrs.Error()
|
|
}
|
|
|
|
func getDockerClient(server string) (*docker.SharedClient, bool, error) {
|
|
cfg := config.GetInstance()
|
|
var host string
|
|
for name, h := range cfg.Value().Providers.Docker {
|
|
if name == server {
|
|
host = h
|
|
break
|
|
}
|
|
}
|
|
for _, agent := range agent.Agents.Iter {
|
|
if agent.Name() == server {
|
|
host = agent.FakeDockerHost()
|
|
break
|
|
}
|
|
}
|
|
if host == "" {
|
|
return nil, false, nil
|
|
}
|
|
dockerClient, err := docker.NewClient(host)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
return dockerClient, true, nil
|
|
}
|
|
|
|
// closeAllClients closes all docker clients after a delay.
|
|
//
|
|
// This is used to ensure that all docker clients are closed after the http handler returns.
|
|
func closeAllClients(dockerClients DockerClients) {
|
|
for _, dockerClient := range dockerClients {
|
|
dockerClient.Close()
|
|
}
|
|
}
|
|
|
|
func handleResult[V any, T ResultType[V]](w http.ResponseWriter, errs error, result T) {
|
|
if errs != nil {
|
|
gperr.LogError("docker errors", errs)
|
|
if len(result) == 0 {
|
|
http.Error(w, "docker errors", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
json.NewEncoder(w).Encode(result)
|
|
}
|
|
|
|
func serveHTTP[V any, T ResultType[V]](w http.ResponseWriter, r *http.Request, getResult func(ctx context.Context, dockerClients DockerClients) (T, gperr.Error)) {
|
|
dockerClients, err := getDockerClients()
|
|
if err != nil {
|
|
handleResult[V, T](w, err, nil)
|
|
return
|
|
}
|
|
defer closeAllClients(dockerClients)
|
|
|
|
if httpheaders.IsWebsocket(r.Header) {
|
|
gpwebsocket.Periodic(w, r, 5*time.Second, func(conn *websocket.Conn) error {
|
|
result, err := getResult(r.Context(), dockerClients)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return wsjson.Write(r.Context(), conn, result)
|
|
})
|
|
} else {
|
|
result, err := getResult(r.Context(), dockerClients)
|
|
handleResult[V](w, err, result)
|
|
}
|
|
}
|