From 0e5ee2742d3ac778ce2043d043ebfe91cada51c9 Mon Sep 17 00:00:00 2001 From: Brandon Philips Date: Sat, 10 Aug 2013 10:54:59 -0700 Subject: [PATCH] fix(web): don't share the DefaultMux All of the web handlers were sharing one mux. Separate them out into individual muxes. --- etcd.go | 67 ++++++++++++++++++++++++++++++------------------------ web/web.go | 19 ++++++++++++---- 2 files changed, 51 insertions(+), 35 deletions(-) diff --git a/etcd.go b/etcd.go index f34b3c2ac..cfdaea0b9 100644 --- a/etcd.go +++ b/etcd.go @@ -389,27 +389,30 @@ func dialTimeout(network, addr string) (net.Conn, error) { // Start to listen and response raft command func startRaftTransport(info Info, tlsConf *tls.Config) { - - // internal commands - http.HandleFunc("/name", NameHttpHandler) - http.HandleFunc("/join", JoinHttpHandler) - http.HandleFunc("/vote", VoteHttpHandler) - http.HandleFunc("/log", GetLogHttpHandler) - http.HandleFunc("/log/append", AppendEntriesHttpHandler) - http.HandleFunc("/snapshot", SnapshotHttpHandler) - http.HandleFunc("/snapshotRecovery", SnapshotRecoveryHttpHandler) - http.HandleFunc("/etcdURL", EtcdURLHttpHandler) - u, _ := url.Parse(info.RaftURL) fmt.Printf("raft server [%s] listening on %s\n", info.Name, u) + raftMux := http.NewServeMux() + + server := &http.Server{ + Handler: raftMux, + TLSConfig: tlsConf, + Addr: u.Host, + } + + // internal commands + raftMux.HandleFunc("/name", NameHttpHandler) + raftMux.HandleFunc("/join", JoinHttpHandler) + raftMux.HandleFunc("/vote", VoteHttpHandler) + raftMux.HandleFunc("/log", GetLogHttpHandler) + raftMux.HandleFunc("/log/append", AppendEntriesHttpHandler) + raftMux.HandleFunc("/snapshot", SnapshotHttpHandler) + raftMux.HandleFunc("/snapshotRecovery", SnapshotRecoveryHttpHandler) + raftMux.HandleFunc("/etcdURL", EtcdURLHttpHandler) + if tlsConf == nil { - http.ListenAndServe(u.Host, nil) + fatal(server.ListenAndServe()) } else { - server := &http.Server{ - TLSConfig: tlsConf, - Addr: u.Host, - } fatal(server.ListenAndServeTLS(info.ServerCertFile, argInfo.ServerKeyFile)) } @@ -417,25 +420,29 @@ func startRaftTransport(info Info, tlsConf *tls.Config) { // Start to listen and response client command func startEtcdTransport(info Info, tlsConf *tls.Config) { - // external commands - http.HandleFunc("/"+version+"/keys/", Multiplexer) - http.HandleFunc("/"+version+"/watch/", WatchHttpHandler) - http.HandleFunc("/leader", LeaderHttpHandler) - http.HandleFunc("/machines", MachinesHttpHandler) - http.HandleFunc("/", VersionHttpHandler) - http.HandleFunc("/stats", StatsHttpHandler) - http.HandleFunc("/test/", TestHttpHandler) - u, _ := url.Parse(info.EtcdURL) fmt.Printf("etcd server [%s] listening on %s\n", info.Name, u) + etcdMux := http.NewServeMux() + + server := &http.Server{ + Handler: etcdMux, + TLSConfig: tlsConf, + Addr: u.Host, + } + + // external commands + etcdMux.HandleFunc("/"+version+"/keys/", Multiplexer) + etcdMux.HandleFunc("/"+version+"/watch/", WatchHttpHandler) + etcdMux.HandleFunc("/leader", LeaderHttpHandler) + etcdMux.HandleFunc("/machines", MachinesHttpHandler) + etcdMux.HandleFunc("/", VersionHttpHandler) + etcdMux.HandleFunc("/stats", StatsHttpHandler) + etcdMux.HandleFunc("/test/", TestHttpHandler) + if tlsConf == nil { - fatal(http.ListenAndServe(u.Host, nil)) + fatal(server.ListenAndServe()) } else { - server := &http.Server{ - TLSConfig: tlsConf, - Addr: u.Host, - } fatal(server.ListenAndServeTLS(info.ClientCertFile, info.ClientKeyFile)) } } diff --git a/web/web.go b/web/web.go index 63086f6a6..38a272552 100644 --- a/web/web.go +++ b/web/web.go @@ -25,16 +25,25 @@ func mainHandler(c http.ResponseWriter, req *http.Request) { mainTempl.Execute(c, p) } -func Start(server *raft.Server, webURL string) { +func Start(raftServer *raft.Server, webURL string) { u, _ := url.Parse(webURL) + webMux := http.NewServeMux() + + server := &http.Server{ + Handler: webMux, + Addr: u.Host, + } + + s = raftServer + mainTempl = template.Must(template.New("index.html").Parse(index_html)) - s = server go h.run() - http.HandleFunc("/", mainHandler) - http.Handle("/ws", websocket.Handler(wsHandler)) + webMux.HandleFunc("/", mainHandler) + webMux.Handle("/ws", websocket.Handler(wsHandler)) fmt.Printf("etcd web server listening on %s\n", u) - http.ListenAndServe(u.Host, nil) + + server.ListenAndServe() }