예제 #1
0
파일: api.go 프로젝트: drtoful/gifttt
func postVar(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()

	vars := mux.Vars(r)
	varname := vars["var"]

	for _, c := range internalVars {
		if c == varname {
			w.WriteHeader(http.StatusForbidden)
			return
		}
	}

	var value Value
	if err := json.NewDecoder(r.Body).Decode(&value); err != nil {
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte(err.Error()))
		return
	}

	vm := GetManager()
	if err := vm.Set(varname, value.Value); err != nil {
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte(err.Error()))
		return
	}

	w.WriteHeader(http.StatusOK)
}
예제 #2
0
파일: api.go 프로젝트: drtoful/gifttt
func getVar(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	varname := vars["var"]

	vm := GetManager()
	v, err := vm.Get(varname)
	if err != nil {
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte(err.Error()))
		return
	}

	val := &Value{Value: v}
	b, err := json.Marshal(val)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		w.Write([]byte(err.Error()))
		return
	}

	w.WriteHeader(http.StatusOK)
	w.Write(b)
}