From b46fd86c8a678ceb7a68ad8a02093120f0a3eb2d Mon Sep 17 00:00:00 2001 From: Sean DuBois Date: Mon, 23 Jun 2014 06:53:42 +0000 Subject: [PATCH] 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 --- fail2rest.go | 4 ++++ jail.go | 14 +++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/fail2rest.go b/fail2rest.go index 331677c..73f2ebd 100644 --- a/fail2rest.go +++ b/fail2rest.go @@ -14,6 +14,10 @@ type Configuration struct { Fail2banSocket string } +type ErrorBody struct { + Error string +} + var fail2goConn *fail2go.Fail2goConn func main() { diff --git a/jail.go b/jail.go index 0b64081..85eba0f 100644 --- a/jail.go +++ b/jail.go @@ -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 { }