Example #1
0
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)
}
Example #2
0
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)
}