diff --git a/prog/weaver/main.go b/prog/weaver/main.go index ebcdba52..1ddf7b2c 100644 --- a/prog/weaver/main.go +++ b/prog/weaver/main.go @@ -315,6 +315,8 @@ func handleHTTP(router *weave.Router, httpAddr string, allocator *ipam.Allocator ns.HandleHTTP(muxRouter) } + router.HandleHTTP(muxRouter) + muxRouter.Methods("GET").Path("/status").Headers("Accept", "application/json").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { json, _ := router.StatusJSON(version) w.Header().Set("Content-Type", "application/json") @@ -337,22 +339,6 @@ func handleHTTP(router *weave.Router, httpAddr string, allocator *ipam.Allocator } }) - muxRouter.Methods("POST").Path("/connect").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if err := r.ParseForm(); err != nil { - http.Error(w, fmt.Sprint("unable to parse form: ", err), http.StatusBadRequest) - } - if errors := router.ConnectionMaker.InitiateConnections(r.Form["peer"], r.FormValue("replace") == "true"); len(errors) > 0 { - http.Error(w, errorMessages(errors), http.StatusBadRequest) - } - }) - - muxRouter.Methods("POST").Path("/forget").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if err := r.ParseForm(); err != nil { - http.Error(w, fmt.Sprint("unable to parse form: ", err), http.StatusBadRequest) - } - router.ConnectionMaker.ForgetConnections(r.Form["peer"]) - }) - http.Handle("/", muxRouter) protocol := "tcp" diff --git a/router/http.go b/router/http.go new file mode 100644 index 00000000..b541f186 --- /dev/null +++ b/router/http.go @@ -0,0 +1,36 @@ +package router + +import ( + "fmt" + "github.com/gorilla/mux" + "net/http" + "strings" +) + +func errorMessages(errors []error) string { + var result []string + for _, err := range errors { + result = append(result, err.Error()) + } + return strings.Join(result, "\n") +} + +func (router *Router) HandleHTTP(muxRouter *mux.Router) { + + muxRouter.Methods("POST").Path("/connect").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if err := r.ParseForm(); err != nil { + http.Error(w, fmt.Sprint("unable to parse form: ", err), http.StatusBadRequest) + } + if errors := router.ConnectionMaker.InitiateConnections(r.Form["peer"], r.FormValue("replace") == "true"); len(errors) > 0 { + http.Error(w, errorMessages(errors), http.StatusBadRequest) + } + }) + + muxRouter.Methods("POST").Path("/forget").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if err := r.ParseForm(); err != nil { + http.Error(w, fmt.Sprint("unable to parse form: ", err), http.StatusBadRequest) + } + router.ConnectionMaker.ForgetConnections(r.Form["peer"]) + }) + +}