refactor(Server): Use a config struct in Server

This commit is contained in:
Brian Waldon
2014-01-15 23:24:14 -08:00
parent 9c8a23c333
commit c47760382e
3 changed files with 29 additions and 13 deletions

View File

@@ -117,7 +117,12 @@ func main() {
ps := server.NewPeerServer(psConfig, &peerTLSConfig, &info.RaftTLS, registry, store, &mb) ps := server.NewPeerServer(psConfig, &peerTLSConfig, &info.RaftTLS, registry, store, &mb)
// Create client server. // Create client server.
s := server.New(info.Name, info.EtcdURL, info.EtcdListenHost, &tlsConfig, &info.EtcdTLS, ps, registry, store, &mb) sConfig := server.ServerConfig{
Name: info.Name,
URL: info.EtcdURL,
BindAddr: info.EtcdListenHost,
}
s := server.New(sConfig, &tlsConfig, &info.EtcdTLS, ps, registry, store, &mb)
if err := s.AllowOrigins(config.CorsOrigins); err != nil { if err := s.AllowOrigins(config.CorsOrigins); err != nil {
panic(err) panic(err)
} }

View File

@@ -22,15 +22,20 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
type ServerConfig struct {
Name string
URL string
BindAddr 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 {
http.Server http.Server
Config ServerConfig
peerServer *PeerServer peerServer *PeerServer
registry *Registry registry *Registry
listener net.Listener listener net.Listener
store store.Store store store.Store
name string
url string
tlsConf *TLSConfig tlsConf *TLSConfig
tlsInfo *TLSInfo tlsInfo *TLSInfo
router *mux.Router router *mux.Router
@@ -39,20 +44,19 @@ type Server struct {
} }
// Creates a new Server. // Creates a new Server.
func New(name string, urlStr string, bindAddr string, tlsConf *TLSConfig, tlsInfo *TLSInfo, peerServer *PeerServer, registry *Registry, store store.Store, mb *metrics.Bucket) *Server { func New(sConfig ServerConfig, tlsConf *TLSConfig, tlsInfo *TLSInfo, peerServer *PeerServer, registry *Registry, store store.Store, mb *metrics.Bucket) *Server {
r := mux.NewRouter() r := mux.NewRouter()
cors := &corsHandler{router: r} cors := &corsHandler{router: r}
s := &Server{ s := &Server{
Config: sConfig,
Server: http.Server{ Server: http.Server{
Handler: cors, Handler: cors,
TLSConfig: &tlsConf.Server, TLSConfig: &tlsConf.Server,
Addr: bindAddr, Addr: sConfig.BindAddr,
}, },
name: name,
store: store, store: store,
registry: registry, registry: registry,
url: urlStr,
tlsConf: tlsConf, tlsConf: tlsConf,
tlsInfo: tlsInfo, tlsInfo: tlsInfo,
peerServer: peerServer, peerServer: peerServer,
@@ -96,7 +100,7 @@ func (s *Server) Term() uint64 {
// The server URL. // The server URL.
func (s *Server) URL() string { func (s *Server) URL() string {
return s.url return s.Config.URL
} }
// Retrives the Peer URL for a given node name. // Retrives the Peer URL for a given node name.
@@ -143,7 +147,7 @@ func (s *Server) installV2() {
func (s *Server) installMod() { func (s *Server) installMod() {
r := s.router r := s.router
r.PathPrefix("/mod").Handler(http.StripPrefix("/mod", mod.HttpHandler(s.url))) r.PathPrefix("/mod").Handler(http.StripPrefix("/mod", mod.HttpHandler(s.Config.URL)))
} }
func (s *Server) installDebug() { func (s *Server) installDebug() {
@@ -176,7 +180,7 @@ func (s *Server) handleFunc(path string, f func(http.ResponseWriter, *http.Reque
// 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.url, req.URL.Path, req.RemoteAddr) log.Debugf("[recv] %s %s %s [%s]", req.Method, s.Config.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 {
@@ -193,7 +197,7 @@ func (s *Server) handleFunc(path string, f func(http.ResponseWriter, *http.Reque
// Start to listen and response etcd client command // Start to listen and response etcd client command
func (s *Server) ListenAndServe() error { func (s *Server) ListenAndServe() error {
log.Infof("etcd server [name %s, listen on %s, advertised url %s]", s.name, s.Server.Addr, s.url) log.Infof("etcd server [name %s, listen on %s, advertised url %s]", s.Config.Name, s.Server.Addr, s.Config.URL)
if s.tlsConf.Scheme == "http" { if s.tlsConf.Scheme == "http" {
return s.listenAndServe() return s.listenAndServe()
@@ -353,7 +357,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.name) peers := s.registry.ClientURLs(s.peerServer.RaftServer().Leader(), s.Config.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

@@ -37,7 +37,14 @@ func RunServer(f func(*server.Server)) {
MaxClusterSize: 9, MaxClusterSize: 9,
} }
ps := server.NewPeerServer(psConfig, &server.TLSConfig{Scheme: "http"}, &server.TLSInfo{}, registry, store, nil) ps := server.NewPeerServer(psConfig, &server.TLSConfig{Scheme: "http"}, &server.TLSInfo{}, registry, store, nil)
s := server.New(testName, "http://"+testClientURL, testClientURL, &server.TLSConfig{Scheme: "http"}, &server.TLSInfo{}, ps, registry, store, nil)
sConfig := server.ServerConfig{
Name: testName,
URL: "http://"+testClientURL,
BindAddr: testClientURL,
}
s := server.New(sConfig, &server.TLSConfig{Scheme: "http"}, &server.TLSInfo{}, ps, registry, store, nil)
ps.SetServer(s) ps.SetServer(s)
// Start up peer server. // Start up peer server.