refactor(servers): emit http.Handlers from *Server

This commit is contained in:
Brian Waldon
2014-01-21 08:08:20 -08:00
parent 089021ca6d
commit 2fe22f1890
4 changed files with 6 additions and 19 deletions

View File

@ -180,11 +180,11 @@ func main() {
// Run peer server in separate thread while the client server blocks. // Run peer server in separate thread while the client server blocks.
go func() { go func() {
log.Infof("raft server [name %s, listen on %s, advertised url %s]", ps.Config.Name, psListener.Addr(), ps.Config.URL) log.Infof("raft server [name %s, listen on %s, advertised url %s]", ps.Config.Name, psListener.Addr(), ps.Config.URL)
sHTTP := &ehttp.CORSHandler{ps, corsInfo} sHTTP := &ehttp.CORSHandler{ps.HTTPHandler(), corsInfo}
log.Fatal(http.Serve(psListener, sHTTP)) log.Fatal(http.Serve(psListener, sHTTP))
}() }()
log.Infof("etcd server [name %s, listen on %s, advertised url %s]", s.Name, sListener.Addr(), s.URL()) log.Infof("etcd server [name %s, listen on %s, advertised url %s]", s.Name, sListener.Addr(), s.URL())
sHTTP := &ehttp.CORSHandler{s, corsInfo} sHTTP := &ehttp.CORSHandler{s.HTTPHandler(), corsInfo}
log.Fatal(http.Serve(sListener, sHTTP)) log.Fatal(http.Serve(sListener, sHTTP))
} }

View File

@ -35,7 +35,6 @@ type PeerServerConfig struct {
type PeerServer struct { type PeerServer struct {
Config PeerServerConfig Config PeerServerConfig
handler http.Handler
raftServer raft.Server raftServer raft.Server
server *Server server *Server
joinIndex uint64 joinIndex uint64
@ -77,8 +76,6 @@ func NewPeerServer(psConfig PeerServerConfig, registry *Registry, store store.St
metrics: mb, metrics: mb,
} }
s.handler = s.buildHTTPHandler()
return s return s
} }
@ -164,7 +161,7 @@ func (s *PeerServer) Stop() {
} }
} }
func (s *PeerServer) buildHTTPHandler() http.Handler { func (s *PeerServer) HTTPHandler() http.Handler {
router := mux.NewRouter() router := mux.NewRouter()
// internal commands // internal commands
@ -184,10 +181,6 @@ func (s *PeerServer) buildHTTPHandler() http.Handler {
return router return router
} }
func (s *PeerServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.handler.ServeHTTP(w, r)
}
// Retrieves the underlying Raft server. // Retrieves the underlying Raft server.
func (s *PeerServer) RaftServer() raft.Server { func (s *PeerServer) RaftServer() raft.Server {
return s.raftServer return s.raftServer

View File

@ -45,8 +45,6 @@ func New(name, url string, peerServer *PeerServer, registry *Registry, store sto
metrics: mb, metrics: mb,
} }
s.handler = s.buildHTTPHandler()
return s return s
} }
@ -169,7 +167,7 @@ func (s *Server) handleFunc(r *mux.Router, path string, f func(http.ResponseWrit
}) })
} }
func (s *Server) buildHTTPHandler() http.Handler { func (s *Server) HTTPHandler() http.Handler {
router := mux.NewRouter() router := mux.NewRouter()
// Install the routes. // Install the routes.
@ -185,10 +183,6 @@ func (s *Server) buildHTTPHandler() http.Handler {
return router return router
} }
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.handler.ServeHTTP(w, r)
}
// Dispatch command to the current leader // Dispatch command to the current leader
func (s *Server) Dispatch(c raft.Command, w http.ResponseWriter, req *http.Request) error { func (s *Server) Dispatch(c raft.Command, w http.ResponseWriter, req *http.Request) error {
ps := s.peerServer ps := s.peerServer

View File

@ -70,14 +70,14 @@ func RunServer(f func(*server.Server)) {
go func() { go func() {
c <- true c <- true
ps.Start(false, []string{}) ps.Start(false, []string{})
http.Serve(psListener, ps) http.Serve(psListener, ps.HTTPHandler())
}() }()
<-c <-c
// Start up etcd server. // Start up etcd server.
go func() { go func() {
c <- true c <- true
http.Serve(sListener, s) http.Serve(sListener, s.HTTPHandler())
}() }()
<-c <-c