Add simple request counters to apid’s expvar api
diff --git a/api/api.go b/api/api.go index 4940a0e..2543e57 100644 --- a/api/api.go +++ b/api/api.go
@@ -18,6 +18,7 @@ var log apid.LogService var config apid.ConfigService +var requests *expvar.Map = expvar.NewMap("requests") func CreateService() apid.APIService { config = apid.Config() @@ -26,38 +27,35 @@ config.SetDefault(configAPIPort, 9000) r := mux.NewRouter() - return &service{r} + rw := &router{r} + return &service{rw} } type service struct { - router *mux.Router + *router } func (s *service) Listen() error { port := config.GetString(configAPIPort) log.Infof("opening api port %s", port) + s.InitExpVar() + + apid.Events().Emit(apid.SystemEventsSelector, apid.APIListeningEvent) // todo: run after successful listen? + return http.ListenAndServe(":"+port, s.r) +} + +func (s *service) InitExpVar() { if config.IsSet(configExpVarPath) { log.Infof("expvar available on path: %s", config.Get(configExpVarPath)) s.HandleFunc(config.GetString(configExpVarPath), expvarHandler) } - - apid.Events().Emit(apid.SystemEventsSelector, apid.APIListeningEvent) // todo: run after successful listen? - return http.ListenAndServe(":"+port, s.router) } -func (s *service) Handle(path string, handler http.Handler) apid.Route { - log.Infof("handle %s: %v", path, handler) - return s.Router().Handle(path, handler) -} - -func (s *service) HandleFunc(path string, handlerFunc http.HandlerFunc) apid.Route { - log.Infof("handle %s: %v", path, handlerFunc) - return s.Router().HandleFunc(path, handlerFunc) -} - +// for testing func (s *service) Router() apid.Router { - return &router{s.router} + s.InitExpVar() + return s } func (s *service) Vars(r *http.Request) map[string]string { @@ -83,14 +81,17 @@ } func (r *router) Handle(path string, handler http.Handler) apid.Route { + log.Infof("Handle %s: %v", path, handler) return &route{r.r.Handle(path, handler)} } -func (r *router) HandleFunc(path string, f func(http.ResponseWriter, *http.Request)) apid.Route { - return &route{r.r.HandleFunc(path, f)} +func (r *router) HandleFunc(path string, handlerFunc http.HandlerFunc) apid.Route { + log.Infof("Handle %s: %v", path, handlerFunc) + return &route{r.r.HandleFunc(path, handlerFunc)} } func (r *router) ServeHTTP(w http.ResponseWriter, req *http.Request) { + requests.Add(req.URL.Path, 1) r.r.ServeHTTP(w, req) }
diff --git a/api/api_suite_test.go b/api/api_suite_test.go new file mode 100644 index 0000000..8a93089 --- /dev/null +++ b/api/api_suite_test.go
@@ -0,0 +1,46 @@ +package api_test + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "testing" +) + +import ( + "github.com/30x/apid" + "github.com/30x/apid/factory" + "net/http/httptest" + "os" +) + +var ( + testDir string + testServer *httptest.Server +) + +var _ = BeforeSuite(func() { + apid.Initialize(factory.DefaultServicesFactory()) + + apid.Config().Set("api_expvar_path", "/exp/vars") + + // get the router - this will have the /exp/vars route registered + router := apid.API().Router() + + // create our test server + testServer = httptest.NewServer(router) + +}) + +var _ = AfterSuite(func() { + apid.Events().Close() + if testServer != nil { + testServer.Close() + } + os.RemoveAll(testDir) +}) + +func TestApi(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Api Suite") +}
diff --git a/api/api_test.go b/api/api_test.go new file mode 100644 index 0000000..66c91e0 --- /dev/null +++ b/api/api_test.go
@@ -0,0 +1,33 @@ +package api_test + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "net/url" + "net/http" + "io/ioutil" + "encoding/json" +) + +var _ = Describe("API Service", func() { + + It("should return vars from /exp/vars with request counter", func() { + + uri, err := url.Parse(testServer.URL) + Expect(err).NotTo(HaveOccurred()) + uri.Path = "/exp/vars" + + resp, err := http.Get(uri.String()) + defer resp.Body.Close() + Expect(err).ShouldNot(HaveOccurred()) + Expect(resp.StatusCode).Should(Equal(http.StatusOK)) + + body, err := ioutil.ReadAll(resp.Body) + var m map[string]interface{} + err = json.Unmarshal(body, &m) + Expect(err).ShouldNot(HaveOccurred()) + + requests := m["requests"].(map[string]interface{}) + Expect(requests["/exp/vars"]).Should(Equal(float64(1))) + }) +})
diff --git a/api_service.go b/api_service.go index 73b5a24..b44f3eb 100644 --- a/api_service.go +++ b/api_service.go
@@ -7,15 +7,18 @@ Handle(path string, handler http.Handler) Route HandleFunc(path string, handlerFunc http.HandlerFunc) Route Vars(r *http.Request) map[string]string - Router() Router -} -type Router interface { - Handle(path string, handler http.Handler) Route - HandleFunc(path string, f func(http.ResponseWriter, *http.Request)) Route - ServeHTTP(w http.ResponseWriter, req *http.Request) + // for testing + Router() Router } type Route interface { Methods(methods ...string) Route } + +// for testing +type Router interface { + Handle(path string, handler http.Handler) Route + HandleFunc(path string, handlerFunc http.HandlerFunc) Route + ServeHTTP(w http.ResponseWriter, req *http.Request) +}