Merge pull request #34 from rahonavis/2078-bindjson

#2078 - fix BindJSON calls
This commit is contained in:
Dane Everitt 2020-05-30 10:21:11 -07:00 committed by GitHub
commit 00a026c2a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 11 deletions

View File

@ -46,7 +46,10 @@ func postServerPower(c *gin.Context) {
s := GetServer(c.Param("server")) s := GetServer(c.Param("server"))
var data server.PowerAction var data server.PowerAction
c.BindJSON(&data) // BindJSON sends 400 if the request fails, all we need to do is return
if err := c.BindJSON(&data); err != nil {
return
}
if !data.IsValid() { if !data.IsValid() {
c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{ c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{
@ -98,8 +101,13 @@ func postServerCommands(c *gin.Context) {
return return
} }
var data struct{ Commands []string `json:"commands"` } var data struct {
c.BindJSON(&data) Commands []string `json:"commands"`
}
// BindJSON sends 400 if the request fails, all we need to do is return
if err := c.BindJSON(&data); err != nil {
return
}
for _, command := range data.Commands { for _, command := range data.Commands {
if err := s.Environment.SendCommand(command); err != nil { if err := s.Environment.SendCommand(command); err != nil {

View File

@ -15,7 +15,10 @@ func postServerBackup(c *gin.Context) {
s := GetServer(c.Param("server")) s := GetServer(c.Param("server"))
data := &backup.Request{} data := &backup.Request{}
c.BindJSON(&data) // BindJSON sends 400 if the request fails, all we need to do is return
if err := c.BindJSON(&data); err != nil {
return
}
var adapter backup.BackupInterface var adapter backup.BackupInterface
var err error var err error
@ -41,7 +44,6 @@ func postServerBackup(c *gin.Context) {
} }
}(adapter, s) }(adapter, s)
c.Status(http.StatusAccepted) c.Status(http.StatusAccepted)
} }

View File

@ -89,7 +89,10 @@ func putServerRenameFile(c *gin.Context) {
RenameFrom string `json:"rename_from"` RenameFrom string `json:"rename_from"`
RenameTo string `json:"rename_to"` RenameTo string `json:"rename_to"`
} }
c.BindJSON(&data) // BindJSON sends 400 if the request fails, all we need to do is return
if err := c.BindJSON(&data); err != nil {
return
}
if data.RenameFrom == "" || data.RenameTo == "" { if data.RenameFrom == "" || data.RenameTo == "" {
c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{ c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{
@ -113,7 +116,10 @@ func postServerCopyFile(c *gin.Context) {
var data struct { var data struct {
Location string `json:"location"` Location string `json:"location"`
} }
c.BindJSON(&data) // BindJSON sends 400 if the request fails, all we need to do is return
if err := c.BindJSON(&data); err != nil {
return
}
if err := s.Filesystem.Copy(data.Location); err != nil { if err := s.Filesystem.Copy(data.Location); err != nil {
TrackedServerError(err, s).AbortWithServerError(c) TrackedServerError(err, s).AbortWithServerError(c)
@ -130,7 +136,10 @@ func postServerDeleteFile(c *gin.Context) {
var data struct { var data struct {
Location string `json:"location"` Location string `json:"location"`
} }
c.BindJSON(&data) // BindJSON sends 400 if the request fails, all we need to do is return
if err := c.BindJSON(&data); err != nil {
return
}
if err := s.Filesystem.Delete(data.Location); err != nil { if err := s.Filesystem.Delete(data.Location); err != nil {
TrackedServerError(err, s).AbortWithServerError(c) TrackedServerError(err, s).AbortWithServerError(c)
@ -167,7 +176,10 @@ func postServerCreateDirectory(c *gin.Context) {
Name string `json:"name"` Name string `json:"name"`
Path string `json:"path"` Path string `json:"path"`
} }
c.BindJSON(&data) // BindJSON sends 400 if the request fails, all we need to do is return
if err := c.BindJSON(&data); err != nil {
return
}
if err := s.Filesystem.CreateDirectory(data.Name, data.Path); err != nil { if err := s.Filesystem.CreateDirectory(data.Name, data.Path); err != nil {
TrackedServerError(err, s).AbortWithServerError(c) TrackedServerError(err, s).AbortWithServerError(c)

View File

@ -77,7 +77,10 @@ func postUpdateConfiguration(c *gin.Context) {
// A copy of the configuration we're using to bind the data recevied into. // A copy of the configuration we're using to bind the data recevied into.
cfg := *config.Get() cfg := *config.Get()
c.BindJSON(&cfg) // BindJSON sends 400 if the request fails, all we need to do is return
if err := c.BindJSON(&cfg); err != nil {
return
}
config.Set(&cfg) config.Set(&cfg)
if err := config.Get().WriteToDisk(); err != nil { if err := config.Get().WriteToDisk(); err != nil {