Example #1
0
func (host *ServiceHost) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		w.WriteHeader(http.StatusMethodNotAllowed)
		return
	}

	if r.URL.Path == "" {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	name := counter.Name(r.URL.Path[1:])

	if err := host.service.Increase(name); err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	value, err := host.service.Get(name)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	w.Header().Add("Content-Type", "application/json")
	encoder := json.NewEncoder(w)
	encoder.Encode(struct {
		Name  counter.Name `json:"name"`
		Value int          `json:"value"`
	}{
		Name:  name,
		Value: value,
	})
}
Example #2
0
		}

		service, err = mongo.NewCountService(fmt.Sprintf("mongodb://%v:%v/counter_test", host, port))

		Expect(err).ToNot(HaveOccured())
		Expect(service).ToNot(BeNil())
	})

	Context("when increasing a counter", func() {
		var name counter.Name
		var err error
		var beforeIncrease int
		var afterIncrease int

		BeforeEach(func() {
			name = counter.Name("my-counter")

			beforeIncrease, _ = service.Get(name)
			err = service.Increase(name)
			afterIncrease, _ = service.Get(name)
		})

		It("should not error", func() {
			Expect(err).NotTo(HaveOccured())
		})

		It("should have increased by one", func() {
			Expect(afterIncrease - beforeIncrease).To(Equal(1))
		})
	})