From 6116f8b8914c6268b7a5bf8116f21293c4b75730 Mon Sep 17 00:00:00 2001 From: Dan Esparza Date: Wed, 12 Jul 2017 19:14:28 -0400 Subject: [PATCH] Add support for batch config update --- api/config.go | 39 +++++++++++++++++++++++++++++++++++++++ cmd/start.go | 3 ++- config.db | Bin 32768 -> 32768 bytes 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/api/config.go b/api/config.go index a79d2a2..655da54 100644 --- a/api/config.go +++ b/api/config.go @@ -104,3 +104,42 @@ func SetConfigItem(rw http.ResponseWriter, req *http.Request) { rw.Header().Set("Content-Type", "application/json; charset=utf-8") json.NewEncoder(rw).Encode(response) } + +// SetAllConfigItems adds or updates multiple config items and returns all config items in JSON format +func SetAllConfigItems(rw http.ResponseWriter, req *http.Request) { + // req.Body is a ReadCloser -- we need to remember to close it: + defer req.Body.Close() + + // Decode the request if it was a POST: + request := []data.ConfigItem{} + err := json.NewDecoder(req.Body).Decode(&request) + if err != nil { + sendErrorResponse(rw, err, http.StatusBadRequest) + return + } + + // Get the config datastore: + configDB := data.ConfigDB{ + Database: viper.GetString("datastore.config")} + + // Send each request to the datastore and get a response: + for c := 0; c < len(request); c++ { + // Set the config item: + _, err := configDB.Set(request[c]) + if err != nil { + sendErrorResponse(rw, err, http.StatusInternalServerError) + return + } + } + + // Get the (updated) full list of config items: + response, err := configDB.GetAll() + if err != nil { + sendErrorResponse(rw, err, http.StatusInternalServerError) + return + } + + // Serialize to JSON & return the response: + rw.Header().Set("Content-Type", "application/json; charset=utf-8") + json.NewEncoder(rw).Encode(response) +} diff --git a/cmd/start.go b/cmd/start.go index 6e53daf..a3b429c 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -53,8 +53,9 @@ func start(cmd *cobra.Command, args []string) { // Config Router.HandleFunc("/config", api.GetAllConfig).Methods("GET") + Router.HandleFunc("/config", api.SetAllConfigItems).Methods("POST") Router.HandleFunc("/config/{name}", api.GetConfigItem).Methods("GET") - Router.HandleFunc("/config", api.SetConfigItem).Methods("POST") + Router.HandleFunc("/config/{name}", api.SetConfigItem).Methods("POST") Router.HandleFunc("/config/{name}", api.RemoveConfigItem).Methods("DELETE") // System information diff --git a/config.db b/config.db index b1649188d47ffbe48fee146c68c544129a69aa72..aae2e0c6c4b07e6455bf8171ec671f293b03c7ca 100644 GIT binary patch literal 32768 zcmeI&O-{l<6u|L{A`39#04~fV?tn~7`Dhb2#+bO4oht&>G?CUIh{kvUw;n)G;z3-x z=L9ZvN@o-Z8WR&E=1)>)rXR12~~77-y1xady8xY-M$4UAG0@K5S?2 zUT$}H%z3m3Abv1l>dH?*_ z*`GRQ3fB*srAbl~T zllq%+#xg-HYfJ6iP}`9{c&Xat(?Uyay*LU7aeov>-FS2@!mjXZB@wlIf$+u5K~WMH z?bBhh-*&2MG(79J2Z6dkIMQyEWut66E!*>L>Dx}FS#_ILyH=L2FQvFiBBtT%@8KtR z`G~sLzY6+a#kGCuRb1DTbx++cjjJDt1_1;RKmY**5I_I{1Q0*~f#nl0_57kv;>|h$ zv(A6{o`@MEfB*srAbi@a97~iS?AF69+O8mc4 z|8M-D{_naK&+(+^QU0HM-wY8z009ILKmY**5I_I{1eQ?1)cH;Q-z?=f>jIYWk(e?9 z2q1s}0tg_000IagfB*srAbj