Skip to content

Commit

Permalink
Add support for batch config update
Browse files Browse the repository at this point in the history
  • Loading branch information
danesparza committed Jul 12, 2017
1 parent 348dc2a commit 6116f8b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
39 changes: 39 additions & 0 deletions api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file modified config.db
Binary file not shown.

0 comments on commit 6116f8b

Please sign in to comment.