diff --git a/global.go b/global.go new file mode 100644 index 0000000..53eca14 --- /dev/null +++ b/global.go @@ -0,0 +1,50 @@ +package main + +import ( + "encoding/json" + "github.com/gorilla/mux" + "net/http" + "strings" +) + +func GlobalStatusHandler(res http.ResponseWriter, req *http.Request) { + fail2banInput := make([]string, 1) + fail2banInput[0] = "status" + + output, err := fail2banRequest(fail2banInput) + if err != nil { + } + + //TODO use reflection to assert data structures and give proper errors + jails := output.([]interface{})[1].([]interface{})[1].([]interface{})[1] + jails = strings.Split(jails.(string), ",") + + encodedOutput, err := json.Marshal(jails) + if err != nil { + } + + res.Write(encodedOutput) +} + +func GlobalPingHandler(res http.ResponseWriter, req *http.Request) { + fail2banInput := make([]string, 1) + fail2banInput[0] = "ping" + + output, err := fail2banRequest(fail2banInput) + if err != nil { + } + + //TODO use reflection to assert data structures and give proper errors + output = output.([]interface{})[1] + + encodedOutput, err := json.Marshal(output) + if err != nil { + } + + res.Write(encodedOutput) +} + +func GlobalHandler(globalRouter *mux.Router) { + globalRouter.HandleFunc("/status", GlobalStatusHandler).Methods("GET") + globalRouter.HandleFunc("/ping", GlobalPingHandler).Methods("GET") +} diff --git a/main.go b/main.go index 6799d90..3acac9d 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,13 @@ package main -import ("fmt") +import ( + "github.com/gorilla/mux" + "net/http" +) func main() { - x := make([]string, 0) - x = append(x, "status") - output, err := fail2banRequest(x) - - fmt.Println(output) - fmt.Println(err) - + r := mux.NewRouter() + GlobalHandler(r.PathPrefix("/global").Subrouter()) + http.Handle("/", r) + http.ListenAndServe(":5000", nil) }