server.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package server
  2. import (
  3. "github.com/gorilla/mux"
  4. "net/http"
  5. )
  6. // The Server provides an HTTP interface to the underlying data store.
  7. type Server struct {
  8. http.Server
  9. raftServer *raftServer
  10. name string
  11. url string
  12. tlsConf *TLSConfig
  13. tlsInfo *TLSInfo
  14. corsOrigins map[string]bool
  15. }
  16. // Creates a new Server.
  17. func New(name string, urlStr string, listenHost string, tlsConf *TLSConfig, tlsInfo *TLSInfo, raftServer *raftServer) *Server {
  18. s := &etcdServer{
  19. Server: http.Server{
  20. Handler: mux.NewRouter(),
  21. TLSConfig: &tlsConf.Server,
  22. Addr: listenHost,
  23. },
  24. name: name,
  25. url: urlStr,
  26. tlsConf: tlsConf,
  27. tlsInfo: tlsInfo,
  28. raftServer: raftServer,
  29. }
  30. // TODO: Move to main.go.
  31. // Install the routes for each version of the API.
  32. // v1.Install(s)
  33. // v2.Install(s)
  34. return s
  35. }
  36. // Adds a server handler to the router.
  37. func (s *Server) HandleFunc(path string, f func(http.ResponseWriter, *http.Request, *server.Server) error) *mux.Route {
  38. r := s.Handler.(*mux.Router)
  39. // Wrap the standard HandleFunc interface to pass in the server reference.
  40. return r.HandleFunc(path, func(w http.ResponseWriter, req *http.Request) {
  41. // Write CORS header.
  42. if s.OriginAllowed("*") {
  43. w.Header().Add("Access-Control-Allow-Origin", "*")
  44. } else if s.OriginAllowed(r.Header.Get("Origin")) {
  45. w.Header().Add("Access-Control-Allow-Origin", r.Header.Get("Origin"))
  46. }
  47. // Execute handler function and return error if necessary.
  48. if err := f(w, req, s); err != nil {
  49. if etcdErr, ok := err.(*etcdErr.Error); ok {
  50. debug("Return error: ", (*etcdErr).Error())
  51. etcdErr.Write(w)
  52. } else {
  53. http.Error(w, e.Error(), http.StatusInternalServerError)
  54. }
  55. }
  56. })
  57. }
  58. // Start to listen and response etcd client command
  59. func (s *Server) ListenAndServe() {
  60. infof("etcd server [name %s, listen on %s, advertised url %s]", s.name, s.Server.Addr, s.url)
  61. if s.tlsConf.Scheme == "http" {
  62. fatal(s.Server.ListenAndServe())
  63. } else {
  64. fatal(s.Server.ListenAndServeTLS(s.tlsInfo.CertFile, s.tlsInfo.KeyFile))
  65. }
  66. }
  67. // Sets a comma-delimited list of origins that are allowed.
  68. func (s *Server) AllowOrigins(origins string) error {
  69. // Construct a lookup of all origins.
  70. m := make(map[string]bool)
  71. for _, v := range strings.Split(cors, ",") {
  72. if v != "*" {
  73. if _, err := url.Parse(v); err != nil {
  74. return fmt.Errorf("Invalid CORS origin: %s", err)
  75. }
  76. }
  77. m[v] = true
  78. }
  79. s.origins = m
  80. return nil
  81. }
  82. // Determines whether the server will allow a given CORS origin.
  83. func (s *Server) OriginAllowed(origin string) {
  84. return s.origins["*"] || s.origins[origin]
  85. }