Browse Source

fix(web): don't share the DefaultMux

All of the web handlers were sharing one mux. Separate them out into
individual muxes.
Brandon Philips 12 years ago
parent
commit
0e5ee2742d
2 changed files with 51 additions and 35 deletions
  1. 37 30
      etcd.go
  2. 14 5
      web/web.go

+ 37 - 30
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))
 	}
 }

+ 14 - 5
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()
 }