Skip to content

Commit

Permalink
Add config set and remove methods to API
Browse files Browse the repository at this point in the history
Added config set and remove methods to the API.  Smoke tested
  • Loading branch information
danesparza committed Jun 12, 2017
1 parent 0ba5f00 commit 2ec6262
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
53 changes: 53 additions & 0 deletions api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"net/http"

"fmt"

"github.com/danesparza/appliance-monitor/data"
"github.com/gorilla/mux"
"github.com/spf13/viper"
Expand Down Expand Up @@ -51,3 +53,54 @@ func GetConfigItem(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(rw).Encode(response)
}

// RemoveConfigItem removes a single config item
func RemoveConfigItem(rw http.ResponseWriter, req *http.Request) {

// Get the config datastore:
configDB := data.ConfigDB{
Database: viper.GetString("datastore.config")}

// Get the config name from the request:
configName := mux.Vars(req)["name"]

// Send the request to the datastore and get a response:
err := configDB.Remove(configName)
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(fmt.Sprintf("Removed %s", configName))
}

// SetConfigItem adds or updates a single config item and returns the new item in JSON format
func SetConfigItem(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 the request to the datastore and get a response:
response, err := configDB.Set(request)
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 @@ -66,7 +66,8 @@ func serve(cmd *cobra.Command, args []string) {
// Config
Router.HandleFunc("/config", api.GetAllConfig).Methods("GET")
Router.HandleFunc("/config/{name}", api.GetConfigItem).Methods("GET")
Router.HandleFunc("/config", nil).Methods("POST")
Router.HandleFunc("/config", api.SetConfigItem).Methods("POST")
Router.HandleFunc("/config/{name}", api.RemoveConfigItem).Methods("DELETE")

// System information
Router.HandleFunc("/system/state", api.GetCurrentState).Methods("GET")
Expand Down

0 comments on commit 2ec6262

Please sign in to comment.