mirror of
https://github.com/Sean-Der/fail2rest.git
synced 2024-12-22 05:32:20 +01:00
Implement single error handler, start moving functions that discarded errors over to it
This commit is contained in:
parent
f861fc22a8
commit
36cfd63495
3 changed files with 31 additions and 6 deletions
22
error.go
Normal file
22
error.go
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ErrorBody struct {
|
||||||
|
Error string
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeHTTPError(res http.ResponseWriter, err error) {
|
||||||
|
res.WriteHeader(400)
|
||||||
|
encodedOutput, err := json.Marshal(ErrorBody{Error: err.Error()})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Failed to generate HTTP error: " + err.Error())
|
||||||
|
} else {
|
||||||
|
res.Write(encodedOutput)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -15,10 +15,6 @@ type Configuration struct {
|
||||||
Fail2banSocket string
|
Fail2banSocket string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ErrorBody struct {
|
|
||||||
Error string
|
|
||||||
}
|
|
||||||
|
|
||||||
var fail2goConn *fail2go.Conn
|
var fail2goConn *fail2go.Conn
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
11
global.go
11
global.go
|
@ -8,10 +8,17 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func globalStatusHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
|
func globalStatusHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
|
||||||
globalStatus, _ := fail2goConn.GlobalStatus()
|
globalStatus, err := fail2goConn.GlobalStatus()
|
||||||
|
|
||||||
encodedOutput, err := json.Marshal(globalStatus)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
writeHTTPError(res, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
encodedOutput, err := json.Marshal(globalStatus)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
writeHTTPError(res, err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
res.Write(encodedOutput)
|
res.Write(encodedOutput)
|
||||||
|
|
Loading…
Reference in a new issue