refactor(agent): move agent pool to agent package, rename route.Agent() to route.GetAgent()

This commit is contained in:
yusing
2025-06-14 20:04:39 +08:00
parent cabb840a91
commit 7d17a01de1
13 changed files with 23 additions and 55 deletions

View File

@@ -5,18 +5,18 @@ import (
"time"
"github.com/gorilla/websocket"
config "github.com/yusing/go-proxy/internal/config/types"
"github.com/yusing/go-proxy/agent/pkg/agent"
"github.com/yusing/go-proxy/internal/net/gphttp"
"github.com/yusing/go-proxy/internal/net/gphttp/gpwebsocket"
"github.com/yusing/go-proxy/internal/net/gphttp/httpheaders"
)
func ListAgents(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
func ListAgents(w http.ResponseWriter, r *http.Request) {
if httpheaders.IsWebsocket(r.Header) {
gpwebsocket.Periodic(w, r, 10*time.Second, func(conn *websocket.Conn) error {
return conn.WriteJSON(cfg.ListAgents())
return conn.WriteJSON(agent.ListAgents())
})
} else {
gphttp.RespondJSON(w, r, cfg.ListAgents())
gphttp.RespondJSON(w, r, agent.ListAgents())
}
}

View File

@@ -7,6 +7,7 @@ import (
"time"
"github.com/gorilla/websocket"
"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"
@@ -43,7 +44,7 @@ func getDockerClients() (DockerClients, gperr.Error) {
dockerClients[name] = dockerClient
}
for _, agent := range cfg.ListAgents() {
for _, agent := range agent.ListAgents() {
dockerClient, err := docker.NewClient(agent.FakeDockerHost())
if err != nil {
connErrs.Add(err)
@@ -65,7 +66,7 @@ func getDockerClient(server string) (*docker.SharedClient, bool, error) {
}
}
if host == "" {
for _, agent := range cfg.ListAgents() {
for _, agent := range agent.ListAgents() {
if agent.Name() == server {
host = agent.FakeDockerHost()
break

View File

@@ -39,7 +39,7 @@ func NewAgent(w http.ResponseWriter, r *http.Request) {
return
}
hostport := fmt.Sprintf("%s:%d", host, port)
if _, ok := config.GetInstance().GetAgent(hostport); ok {
if _, ok := agent.GetAgent(hostport); ok {
gphttp.KeyAlreadyExists(w, "agent", hostport)
return
}

View File

@@ -4,7 +4,6 @@ import (
"net/http"
agentPkg "github.com/yusing/go-proxy/agent/pkg/agent"
config "github.com/yusing/go-proxy/internal/config/types"
"github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/metrics/systeminfo"
"github.com/yusing/go-proxy/internal/net/gphttp"
@@ -13,7 +12,7 @@ import (
nettypes "github.com/yusing/go-proxy/internal/net/types"
)
func SystemInfo(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
func SystemInfo(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
agentAddr := query.Get("agent_addr")
query.Del("agent_addr")
@@ -22,7 +21,7 @@ func SystemInfo(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Reques
return
}
agent, ok := cfg.GetAgent(agentAddr)
agent, ok := agentPkg.GetAgent(agentAddr)
if !ok {
gphttp.NotFound(w, "agent_addr")
return