Move router HTTP handlers into router/http.go
This commit is contained in:
1 parent
8bce900170
commit
278807a199
2 files changed
+38
-16
No files matched your search
+2
-16
@@ -315,6 +315,8 @@ func handleHTTP(router *weave.Router, httpAddr string, allocator *ipam.Allocator
|
|||||||
ns.HandleHTTP(muxRouter)
|
ns.HandleHTTP(muxRouter)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
router.HandleHTTP(muxRouter)
|
||||||
|
|
||||||
muxRouter.Methods("GET").Path("/status").Headers("Accept", "application/json").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
muxRouter.Methods("GET").Path("/status").Headers("Accept", "application/json").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
json, _ := router.StatusJSON(version)
|
json, _ := router.StatusJSON(version)
|
||||||
w.Header().Set("Content-Type", "application/json")
|
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)
|
http.Handle("/", muxRouter)
|
||||||
|
|
||||||
protocol := "tcp"
|
protocol := "tcp"
|
||||||
|
|||||||
@@ -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"])
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user