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