Skip to content

Commit

Permalink
Implement accepting querystring parameters for ip:port endpoint (#28)
Browse files Browse the repository at this point in the history
Refactor getting addresses logic
  • Loading branch information
Ethan Kaley authored and jfrey committed Aug 26, 2016
1 parent 73a5650 commit 0ad0bf5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
6 changes: 2 additions & 4 deletions rackhd/cmd/utils/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (e *Server) HandleServeObject(w http.ResponseWriter, r *http.Request) {
func (e *Server) Register(datacenter, serviceName string) {
rGen := rand.New(rand.NewSource(time.Now().UnixNano()))
n := fmt.Sprintf("%d", rGen.Int())
err := e.Store.Register(&regStore.CatalogRegistration{
if err := e.Store.Register(&regStore.CatalogRegistration{
Node: n,
Address: e.Address,
Datacenter: datacenter,
Expand All @@ -122,9 +122,7 @@ func (e *Server) Register(datacenter, serviceName string) {
ServiceID: serviceName,
ServiceName: serviceName,
},
}, nil)

if err != nil {
}, nil); err != nil {
log.Printf("Error registering serviceName: %s\n", err)
}

Expand Down
49 changes: 43 additions & 6 deletions rackhd/proxy/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"log"
"net"
"net/http"
"sync"

Expand Down Expand Up @@ -52,13 +53,9 @@ func (p *Server) HandleTest(w http.ResponseWriter, r *http.Request) {
// HandleNodes sends, recieves, and processes all the data
func (p *Server) HandleNodes(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
addresses, err := p.Store.GetAddresses()
if err != nil {
log.Printf("Did not get IP List ==> %s\n", err)
}
cr, _ := p.GetResp(r, addresses)
addrMap := p.GetAddresses(w, r)
cr, _ := p.GetResp(r, addrMap)
p.RespCheck(w, cr)
return
}

// GetResp makes channels for the response and errors from http.Get.
Expand Down Expand Up @@ -93,6 +90,46 @@ func (p *Server) GetResp(r *http.Request, addrs map[string]struct{}) (chan *Resp
return cr, errs
}

// GetAddresses decides from where to retrieve the addresses
func (p *Server) GetAddresses(w http.ResponseWriter, r *http.Request) map[string]struct{} {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
querySlice := r.URL.Query()
if len(querySlice["ip"]) > 0 {
addrMap := p.GetQueryAddresses(querySlice["ip"])
return addrMap
}
addrMap := p.GetStoredAddresses()
return addrMap
}

// GetStoredAddresses calls GetAddresses and returns a map of addresses
func (p *Server) GetStoredAddresses() map[string]struct{} {
addresses, err := p.Store.GetAddresses()
if err != nil {
log.Printf("Did not get IP List ==> %s\n", err)
}
return addresses
}

// GetQueryAddresses retrives a url flag and returns a map of address(es)
func (p *Server) GetQueryAddresses(querySlice []string) map[string]struct{} {
fmt.Println(querySlice)
queryMap := make(map[string]struct{})
for _, elem := range querySlice {
ip, port, err := net.SplitHostPort(elem)
if err != nil {
log.Printf("Invalid port => %s\n", err)
return nil
}
if net.ParseIP(ip) != nil {
queryMap[ip+":"+port] = struct{}{}
}
}
return queryMap
}

// RespCheck identifies the type of initialResp.Body and calls the appropriate
// helper function to write to the ResponseWriter.
func (p *Server) RespCheck(w http.ResponseWriter, cr chan *Response) {
Expand Down

0 comments on commit 0ad0bf5

Please sign in to comment.