Skip to content

Commit

Permalink
Add a route for /service to return all available services. (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanschneider authored Jul 9, 2019
1 parent 5c3408f commit 235c50b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ func main() {
}
}()

router.PathPrefix("/service/").Handler(http.StripPrefix("/service/", srv.Handler()))
router.Path("/service").Handler(srv.AllServicesHandler())
router.PathPrefix("/service/").Handler(http.StripPrefix("/service/", srv.ServiceQueryHandler()))

log.Printf("[INFO] Ready to serve at %v", address)
s := http.Server{
Expand Down
23 changes: 21 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (s *server) PopulateService(name string, nodes consistent.Members) (added c
return
}

// Handler returns an http.HandlerFunc which implements the server logic
func (s *server) Handler() http.HandlerFunc {
// ServiceQueryHandler returns an http.HandlerFunc which can query a single service
func (s *server) ServiceQueryHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
serviceName := r.URL.Path
s.mu.RLock()
Expand Down Expand Up @@ -118,3 +118,22 @@ func (s *server) Handler() http.HandlerFunc {
http.Error(w, err.Error(), http.StatusBadRequest)
}
}

// AllServicesHandler returns an http.Handler that provides information about all known services
func (s *server) AllServicesHandler() http.HandlerFunc {
// For now just return the known services as a JSON array
return func(w http.ResponseWriter, r *http.Request) {
s.mu.RLock()
services := make([]string, 0, len(s.services))
for key, _ := range s.services {
services = append(services, key)
}
s.mu.RUnlock()

w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(services)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
}
}
34 changes: 30 additions & 4 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/INFURA/keyrouter/server"
)

func TestMicroservice_Handler(t *testing.T) {
func TestServer_ServiceQueryHandler(t *testing.T) {

srv := server.New()
// add some entries
Expand All @@ -29,7 +29,7 @@ func TestMicroservice_Handler(t *testing.T) {
require.NoError(t, err)
}

handler := srv.Handler()
handler := srv.ServiceQueryHandler()

// get 3 entries for foo
{
Expand Down Expand Up @@ -148,7 +148,33 @@ func TestMicroservice_Handler(t *testing.T) {
}
}

func BenchmarkMicroservice_Handler(b *testing.B) {
func TestServer_AllServicesHandler(t *testing.T) {
srv := server.New()
// add some entries
{
_, _, err := srv.PopulateService("foo", consistent.Members{"a", "b", "c"})
require.NoError(t, err)
}

{
_, _, err := srv.PopulateService("bar", consistent.Members{"1", "2", "3"})
require.NoError(t, err)
}

handler := srv.AllServicesHandler()

// do a GET on the handler
{
request, err := http.NewRequest("GET", "/", nil)
require.NoError(t, err)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, request)

require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
}
}

func BenchmarkServer_ServiceQueryHandler(b *testing.B) {
srv := server.New()

services := 10
Expand All @@ -163,7 +189,7 @@ func BenchmarkMicroservice_Handler(b *testing.B) {
_, _, err := srv.PopulateService(fmt.Sprintf("service-%d", service), members)
require.NoError(b, err)
}
handler := srv.Handler()
handler := srv.ServiceQueryHandler()
b.ResetTimer()

for n := 0; n < b.N; n++ {
Expand Down

0 comments on commit 235c50b

Please sign in to comment.