You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.5 KiB

  1. package weed_server
  2. import (
  3. "code.google.com/p/weed-fs/go/glog"
  4. "encoding/json"
  5. "github.com/goraft/raft"
  6. "net/http"
  7. )
  8. // Handles incoming RAFT joins.
  9. func (s *RaftServer) joinHandler(w http.ResponseWriter, req *http.Request) {
  10. glog.V(0).Infoln("Processing incoming join. Current Leader", s.raftServer.Leader(), "Self", s.raftServer.Name(), "Peers", s.raftServer.Peers())
  11. command := &raft.DefaultJoinCommand{}
  12. if err := json.NewDecoder(req.Body).Decode(&command); err != nil {
  13. glog.V(0).Infoln("Error decoding json message:", err)
  14. http.Error(w, err.Error(), http.StatusInternalServerError)
  15. return
  16. }
  17. glog.V(0).Infoln("join command from Name", command.Name, "Connection", command.ConnectionString)
  18. if _, err := s.raftServer.Do(command); err != nil {
  19. switch err {
  20. case raft.NotLeaderError:
  21. s.redirectToLeader(w, req)
  22. default:
  23. glog.V(0).Infoln("Error processing join:", err)
  24. http.Error(w, err.Error(), http.StatusInternalServerError)
  25. }
  26. }
  27. }
  28. func (s *RaftServer) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
  29. s.router.HandleFunc(pattern, handler)
  30. }
  31. func (s *RaftServer) redirectToLeader(w http.ResponseWriter, req *http.Request) {
  32. if s.Leader() != "" {
  33. glog.V(0).Infoln("Redirecting to", "http://"+s.Leader()+req.URL.Path)
  34. http.Redirect(w, req, "http://"+s.Leader()+req.URL.Path, http.StatusMovedPermanently)
  35. } else {
  36. glog.V(0).Infoln("Error: Leader Unknown")
  37. http.Error(w, "Leader unknown", http.StatusInternalServerError)
  38. }
  39. }