Remove boilerplate and condense the http handler down to one function.
Also fixed indenting on one line in shell script.
This commit is contained in:
1 parent
3f9eadda70
commit
3942f19897
3 files changed
+12
-37
No files matched your search
@@ -1,34 +0,0 @@
|
||||
package weave
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
/* A simple http responder that will show the status of the router when asked,
|
||||
e.g. via http://172.17.0.2/status
|
||||
*/
|
||||
type HttpStatusResponder struct {
|
||||
router *Router
|
||||
}
|
||||
|
||||
func NewHttpStatusResponder(router *Router) *HttpStatusResponder {
|
||||
resp := &HttpStatusResponder{router}
|
||||
return resp
|
||||
}
|
||||
|
||||
func (resp *HttpStatusResponder) SetRouter(newRouter *Router) {
|
||||
resp.router = newRouter
|
||||
}
|
||||
|
||||
func (resp *HttpStatusResponder) ListenAndServe() {
|
||||
http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
|
||||
if resp.router != nil {
|
||||
fmt.Fprint(w, resp.router.Status())
|
||||
} else {
|
||||
fmt.Fprintf(w, "Not initialized")
|
||||
}
|
||||
})
|
||||
address := fmt.Sprintf(":%d", StatusPort)
|
||||
http.ListenAndServe(address, nil)
|
||||
}
|
||||
+11
-2
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/davecheney/profile"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
@@ -123,11 +124,19 @@ func main() {
|
||||
}
|
||||
}()
|
||||
}
|
||||
httpResponder := weave.NewHttpStatusResponder(router)
|
||||
go httpResponder.ListenAndServe()
|
||||
go handleHttp(router)
|
||||
handleSignals(router)
|
||||
}
|
||||
|
||||
func handleHttp(router *weave.Router) {
|
||||
http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprint(w, router.Status())
|
||||
})
|
||||
address := fmt.Sprintf(":%d", weave.StatusPort)
|
||||
http.ListenAndServe(address, nil)
|
||||
}
|
||||
|
||||
|
||||
func handleSignals(router *weave.Router) {
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, syscall.SIGQUIT, syscall.SIGUSR1)
|
||||
|
||||
+1
-1
@@ -19,7 +19,7 @@ usage() {
|
||||
|
||||
if [ "$#" -lt 1 ]; then
|
||||
usage
|
||||
exit 1
|
||||
exit 1
|
||||
fi
|
||||
|
||||
[ `id -u` = 0 ] || {
|
||||
|
||||
Reference in new issue
Block a user