etcd_server.go 844 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package main
  2. import (
  3. "net/http"
  4. "net/url"
  5. )
  6. type etcdServer struct {
  7. name string
  8. url string
  9. tlsConf *TLSConfig
  10. tlsInfo *TLSInfo
  11. }
  12. var e *etcdServer
  13. func newEtcdServer(name string, url string, tlsConf *TLSConfig, tlsInfo *TLSInfo) *etcdServer {
  14. return &etcdServer{
  15. name: name,
  16. url: url,
  17. tlsConf: tlsConf,
  18. tlsInfo: tlsInfo,
  19. }
  20. }
  21. // Start to listen and response etcd client command
  22. func (e *etcdServer) start() {
  23. u, err := url.Parse(e.url)
  24. if err != nil {
  25. fatalf("invalid url '%s': %s", e.url, err)
  26. }
  27. infof("etcd server [%s:%s]", e.name, u)
  28. server := http.Server{
  29. Handler: NewEtcdMuxer(),
  30. TLSConfig: &e.tlsConf.Server,
  31. Addr: u.Host,
  32. }
  33. if e.tlsConf.Scheme == "http" {
  34. fatal(server.ListenAndServe())
  35. } else {
  36. fatal(server.ListenAndServeTLS(e.tlsInfo.CertFile, e.tlsInfo.KeyFile))
  37. }
  38. }