Adjustment to have enable/disable client on UI

This commit is contained in:
Khanh Ngo 2020-04-22 17:11:28 +07:00
parent e52ffaf686
commit dbb85cb759
No known key found for this signature in database
GPG key ID: D5FAA6A16150E49E
6 changed files with 138 additions and 4 deletions

View file

@ -139,6 +139,39 @@ func NewClient() echo.HandlerFunc {
}
}
// SetClientStatus handler to enable / disable a client
func SetClientStatus() echo.HandlerFunc {
return func(c echo.Context) error {
data := make(map[string]interface{})
err := json.NewDecoder(c.Request().Body).Decode(&data)
if err != nil {
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
}
clientID := data["id"].(string)
status := data["status"].(bool)
// initialize database directory
dir := "./db"
db, err := scribble.New(dir, nil)
if err != nil {
log.Error("Cannot initialize the database: ", err)
}
client := model.Client{}
if err := db.Read("clients", clientID, &client); err != nil {
log.Error("Cannot fetch server interface config from database: ", err)
}
client.Enabled = status
db.Write("clients", clientID, &client)
log.Infof("Change client %s to status %b", client.ID, status)
return c.JSON(http.StatusOK, jsonHTTPResponse{true, "ok"})
}
}
// RemoveClient handler
func RemoveClient() echo.HandlerFunc {
return func(c echo.Context) error {