2013-12-23 19:38:51 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2013-12-23 22:26:34 +00:00
|
|
|
"time"
|
2013-12-23 19:38:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HTTPServer is used to wrap an Agent and expose various API's
|
|
|
|
// in a RESTful manner
|
|
|
|
type HTTPServer struct {
|
|
|
|
agent *Agent
|
|
|
|
mux *http.ServeMux
|
|
|
|
listener net.Listener
|
|
|
|
logger *log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer starts a new HTTP server to provide an interface to
|
|
|
|
// the agent.
|
|
|
|
func NewServer(agent *Agent, logOutput io.Writer, bind string) (*HTTPServer, error) {
|
|
|
|
// Create the mux
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
|
|
// Create listener
|
|
|
|
list, err := net.Listen("tcp", bind)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the server
|
|
|
|
srv := &HTTPServer{
|
|
|
|
agent: agent,
|
|
|
|
mux: mux,
|
|
|
|
listener: list,
|
|
|
|
logger: log.New(logOutput, "", log.LstdFlags),
|
|
|
|
}
|
|
|
|
srv.registerHandlers()
|
|
|
|
|
|
|
|
// Start the server
|
|
|
|
go http.Serve(list, mux)
|
|
|
|
return srv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown is used to shutdown the HTTP server
|
|
|
|
func (s *HTTPServer) Shutdown() {
|
|
|
|
s.listener.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// registerHandlers is used to attach our handlers to the mux
|
|
|
|
func (s *HTTPServer) registerHandlers() {
|
2013-12-25 01:09:51 +00:00
|
|
|
s.mux.HandleFunc("/", s.Index)
|
|
|
|
|
2013-12-23 19:38:51 +00:00
|
|
|
s.mux.HandleFunc("/v1/status/leader", s.wrap(s.StatusLeader))
|
|
|
|
s.mux.HandleFunc("/v1/status/peers", s.wrap(s.StatusPeers))
|
2013-12-23 22:26:34 +00:00
|
|
|
|
2013-12-24 19:55:14 +00:00
|
|
|
s.mux.HandleFunc("/v1/catalog/register", s.wrap(s.CatalogRegister))
|
|
|
|
s.mux.HandleFunc("/v1/catalog/deregister", s.wrap(s.CatalogDeregister))
|
2013-12-23 22:26:34 +00:00
|
|
|
s.mux.HandleFunc("/v1/catalog/datacenters", s.wrap(s.CatalogDatacenters))
|
2013-12-24 00:20:51 +00:00
|
|
|
s.mux.HandleFunc("/v1/catalog/nodes", s.wrap(s.CatalogNodes))
|
2013-12-24 19:55:14 +00:00
|
|
|
s.mux.HandleFunc("/v1/catalog/services", s.wrap(s.CatalogServices))
|
|
|
|
s.mux.HandleFunc("/v1/catalog/service/", s.wrap(s.CatalogServiceNodes))
|
|
|
|
s.mux.HandleFunc("/v1/catalog/node/", s.wrap(s.CatalogNodeServices))
|
2013-12-23 19:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// wrap is used to wrap functions to make them more convenient
|
2013-12-24 00:20:51 +00:00
|
|
|
func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Request) (interface{}, error)) func(resp http.ResponseWriter, req *http.Request) {
|
2013-12-23 19:38:51 +00:00
|
|
|
f := func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
// Invoke the handler
|
2013-12-23 22:26:34 +00:00
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
|
|
|
s.logger.Printf("[DEBUG] HTTP Request %v (%v)", req.URL, time.Now().Sub(start))
|
|
|
|
}()
|
2013-12-24 00:20:51 +00:00
|
|
|
obj, err := handler(resp, req)
|
2013-12-23 19:38:51 +00:00
|
|
|
|
|
|
|
// Check for an error
|
|
|
|
HAS_ERR:
|
|
|
|
if err != nil {
|
2013-12-24 20:22:42 +00:00
|
|
|
s.logger.Printf("[ERR] Request %v, error: %v", req.URL, err)
|
2013-12-23 19:38:51 +00:00
|
|
|
resp.WriteHeader(500)
|
|
|
|
resp.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write out the JSON object
|
2013-12-24 00:20:51 +00:00
|
|
|
if obj != nil {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
enc := json.NewEncoder(&buf)
|
|
|
|
if err = enc.Encode(obj); err != nil {
|
|
|
|
goto HAS_ERR
|
|
|
|
}
|
|
|
|
resp.Write(buf.Bytes())
|
2013-12-23 19:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
2013-12-24 00:20:51 +00:00
|
|
|
|
2013-12-25 01:09:51 +00:00
|
|
|
// Renders a simple index page
|
|
|
|
func (s *HTTPServer) Index(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.URL.Path == "/" {
|
|
|
|
resp.Write([]byte("Consul Agent"))
|
|
|
|
} else {
|
|
|
|
resp.WriteHeader(404)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-24 00:20:51 +00:00
|
|
|
// decodeBody is used to decode a JSON request body
|
|
|
|
func decodeBody(req *http.Request, out interface{}) error {
|
|
|
|
dec := json.NewDecoder(req.Body)
|
|
|
|
return dec.Decode(out)
|
|
|
|
}
|