internal/agent/agent_endpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package agent
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"git.j3s.sh/cascade/api"
"github.com/hashicorp/serf/coordinate"
"github.com/hashicorp/serf/serf"
)
type Self struct {
Config interface{}
DebugConfig map[string]interface{}
Coord *coordinate.Coordinate
Member serf.Member
Stats map[string]map[string]string
Meta map[string]string
}
func (s *HTTPHandlers) agentSelf(w http.ResponseWriter, r *http.Request) {
config := struct {
NodeName string
Version string
BuildDate string
DNSBindAddr string
HTTPBindAddr string
SerfBindAddr string
}{
NodeName: s.agent.Config.NodeName,
Version: s.agent.Config.VersionWithMetadata(),
DNSBindAddr: s.agent.Config.DNSBindAddr.String(),
HTTPBindAddr: s.agent.Config.HTTPBindAddr.String(),
SerfBindAddr: s.agent.Config.SerfBindAddr.String(),
}
self := Self{
Config: config,
}
json, err := s.marshalJSON(r, self)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
fmt.Fprintf(w, string(json))
}
// return s.agent.Members()
// func (s *HTTPHandlers) Index(resp http.ResponseWriter, req *http.Request) {
func (s *HTTPHandlers) agentMembers(w http.ResponseWriter, r *http.Request) {
members := s.agent.Members()
json, err := s.marshalJSON(r, members)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
fmt.Fprintf(w, string(json))
}
func (s *HTTPHandlers) agentServices(w http.ResponseWriter, r *http.Request) {
services := s.agent.OwnedServices()
buf, err := s.marshalJSON(r, services)
if err != nil {
s.agent.logger.Error("marshal services", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(buf)
}
func (s *HTTPHandlers) agentServiceRegister(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut && r.Method != http.MethodPost {
w.Header().Set("Allow", "PUT, POST")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
var svc api.AgentService
if err := json.NewDecoder(r.Body).Decode(&svc); err != nil {
http.Error(w, fmt.Sprintf("bad request: %s", err), http.StatusBadRequest)
return
}
if svc.ID == "" {
svc.ID = svc.Service
}
if svc.Service == "" {
http.Error(w, "Service name required", http.StatusBadRequest)
return
}
if err := s.agent.RegisterService(&svc); err != nil {
s.agent.logger.Error("register service", err, "id", svc.ID)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func (s *HTTPHandlers) agentServiceDeregister(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut && r.Method != http.MethodPost {
w.Header().Set("Allow", "PUT, POST")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
id := strings.TrimPrefix(r.URL.Path, "/v1/agent/service/deregister/")
if id == "" || strings.Contains(id, "/") {
http.Error(w, "service ID required in path", http.StatusBadRequest)
return
}
if err := s.agent.DeregisterService(id); err != nil {
s.agent.logger.Error("deregister service", err, "id", id)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}