Пример #1
0
func CountHandler(store kv.Store) http.HandlerFunc {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		id := getId(r.URL.Path)

		switch r.Method {
		case exercise.GET:
			{
				v := store.Count(id)
				w.Write([]byte(strconv.Itoa(v)))
			}

		}
	})
}
Пример #2
0
func CrudHandler(store kv.Store) http.HandlerFunc {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		content, _ := ioutil.ReadAll(r.Body)
		id := getId(r.URL.Path)

		switch r.Method {
		case exercise.PUT:
			{
				kvStr := strings.Split(string(content), ":")
				store.Put(kvStr[0], kvStr[1])
			}
		case exercise.GET:
			{
				v, _ := store.Get(id)
				w.Write([]byte(v))
			}
		case exercise.DELETE:
			{
				store.Delete(id)
			}
		}
	})
}