Add ErrorBody struct so all errors will have the same JSON format. If a failregex is malformed send a 400 error code with the fail message

This commit is contained in:
Sean DuBois 2014-06-23 06:53:42 +00:00
parent 66961603d6
commit b46fd86c8a
2 changed files with 15 additions and 3 deletions

View File

@ -14,6 +14,10 @@ type Configuration struct {
Fail2banSocket string
}
type ErrorBody struct {
Error string
}
var fail2goConn *fail2go.Fail2goConn
func main() {

14
jail.go
View File

@ -18,7 +18,7 @@ func jailGetHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fai
"currentlyBanned": currentlyBanned,
"totalBanned": totalBanned,
"IPList": IPList,
"failRegexes": failRegexes})
"failRegexes": failRegexes})
if err != nil {
}
@ -66,13 +66,21 @@ type jailFailRegexBody struct {
func jailAddFailRegexHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Fail2goConn) {
var input jailFailRegexBody
var encodedOutput []byte
err := json.NewDecoder(req.Body).Decode(&input)
if err != nil {
}
output, _ := fail2goConn.JailAddFailRegex(mux.Vars(req)["jail"], input.FailRegex)
output, err := fail2goConn.JailAddFailRegex(mux.Vars(req)["jail"], input.FailRegex)
if err != nil {
res.WriteHeader(400)
encodedOutput, err = json.Marshal(ErrorBody{Error: "Invalid Regex"})
} else {
encodedOutput, err = json.Marshal(map[string]interface{}{"FailRegex": output})
}
encodedOutput, err := json.Marshal(map[string]interface{}{"FailRegex": output})
if err != nil {
}