Start modeling REST calls off of the fail2ban-client, add ping and status

This commit is contained in:
Sean DuBois 2014-06-14 17:27:18 +00:00
parent 28316de576
commit de70086c1b
2 changed files with 58 additions and 8 deletions

50
global.go Normal file
View file

@ -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")
}

16
main.go
View file

@ -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)
}