refactor(server): Remove ServerConfig struct

This commit is contained in:
Brian Waldon 2014-01-20 19:17:12 -08:00
parent 0abd860f7e
commit a7d9efa900
3 changed files with 19 additions and 30 deletions

View File

@ -155,11 +155,7 @@ func main() {
ps.SetRaftServer(raftServer) ps.SetRaftServer(raftServer)
// Create client server. // Create client server.
sConfig := server.ServerConfig{ s := server.New(info.Name, info.EtcdURL, ps, registry, store, &mb)
Name: info.Name,
URL: info.EtcdURL,
}
s := server.New(sConfig, ps, registry, store, &mb)
if config.Trace() { if config.Trace() {
s.EnableTracing() s.EnableTracing()
@ -186,7 +182,7 @@ func main() {
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.Config.Name, sListener.Addr(), s.Config.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, corsInfo}
log.Fatal(http.Serve(sListener, sHTTP)) log.Fatal(http.Serve(sListener, sHTTP))
} }

View File

@ -21,14 +21,10 @@ import (
_ "github.com/coreos/etcd/store/v2" _ "github.com/coreos/etcd/store/v2"
) )
type ServerConfig struct {
Name string
URL string
}
// This is the default implementation of the Server interface. // This is the default implementation of the Server interface.
type Server struct { type Server struct {
Config ServerConfig Name string
url string
handler http.Handler handler http.Handler
peerServer *PeerServer peerServer *PeerServer
registry *Registry registry *Registry
@ -39,9 +35,10 @@ type Server struct {
} }
// Creates a new Server. // Creates a new Server.
func New(sConfig ServerConfig, peerServer *PeerServer, registry *Registry, store store.Store, mb *metrics.Bucket) *Server { func New(name, url string, peerServer *PeerServer, registry *Registry, store store.Store, mb *metrics.Bucket) *Server {
s := &Server{ s := &Server{
Config: sConfig, Name: name,
url: url,
store: store, store: store,
registry: registry, registry: registry,
peerServer: peerServer, peerServer: peerServer,
@ -79,7 +76,7 @@ func (s *Server) Term() uint64 {
// The server URL. // The server URL.
func (s *Server) URL() string { func (s *Server) URL() string {
return s.Config.URL return s.url
} }
// Retrives the Peer URL for a given node name. // Retrives the Peer URL for a given node name.
@ -125,7 +122,7 @@ func (s *Server) installV2(r *mux.Router) {
} }
func (s *Server) installMod(r *mux.Router) { func (s *Server) installMod(r *mux.Router) {
r.PathPrefix("/mod").Handler(http.StripPrefix("/mod", mod.HttpHandler(s.Config.URL))) r.PathPrefix("/mod").Handler(http.StripPrefix("/mod", mod.HttpHandler(s.URL())))
} }
func (s *Server) installDebug(r *mux.Router) { func (s *Server) installDebug(r *mux.Router) {
@ -157,7 +154,7 @@ func (s *Server) handleFunc(r *mux.Router, path string, f func(http.ResponseWrit
// Wrap the standard HandleFunc interface to pass in the server reference. // Wrap the standard HandleFunc interface to pass in the server reference.
return r.HandleFunc(path, func(w http.ResponseWriter, req *http.Request) { return r.HandleFunc(path, func(w http.ResponseWriter, req *http.Request) {
// Log request. // Log request.
log.Debugf("[recv] %s %s %s [%s]", req.Method, s.Config.URL, req.URL.Path, req.RemoteAddr) log.Debugf("[recv] %s %s %s [%s]", req.Method, s.URL(), req.URL.Path, req.RemoteAddr)
// Execute handler function and return error if necessary. // Execute handler function and return error if necessary.
if err := f(w, req); err != nil { if err := f(w, req); err != nil {
@ -280,7 +277,7 @@ func (s *Server) GetLeaderHandler(w http.ResponseWriter, req *http.Request) erro
// Handler to return all the known peers in the current cluster. // Handler to return all the known peers in the current cluster.
func (s *Server) GetPeersHandler(w http.ResponseWriter, req *http.Request) error { func (s *Server) GetPeersHandler(w http.ResponseWriter, req *http.Request) error {
peers := s.registry.ClientURLs(s.peerServer.RaftServer().Leader(), s.Config.Name) peers := s.registry.ClientURLs(s.peerServer.RaftServer().Leader(), s.Name)
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write([]byte(strings.Join(peers, ", "))) w.Write([]byte(strings.Join(peers, ", ")))
return nil return nil

View File

@ -59,11 +59,7 @@ func RunServer(f func(*server.Server)) {
raftServer.SetHeartbeatTimeout(testHeartbeatTimeout) raftServer.SetHeartbeatTimeout(testHeartbeatTimeout)
ps.SetRaftServer(raftServer) ps.SetRaftServer(raftServer)
sConfig := server.ServerConfig{ s := server.New(testName, "http://"+testClientURL, ps, registry, store, nil)
Name: testName,
URL: "http://"+testClientURL,
}
s := server.New(sConfig, ps, registry, store, nil)
sListener, err := server.NewListener(testClientURL) sListener, err := server.NewListener(testClientURL)
if err != nil { if err != nil {
panic(err) panic(err)