fix redirect after seprate client and server pot

This commit is contained in:
Xiang Li 2013-06-28 15:37:29 -07:00
parent 430f5d50e3
commit c2f436a58b
6 changed files with 63 additions and 42 deletions

10
etcd.go
View File

@ -99,7 +99,6 @@ type Info struct {
ServerPort int `json:"serverPort"` ServerPort int `json:"serverPort"`
ClientPort int `json:"clientPort"` ClientPort int `json:"clientPort"`
WebPort int `json:"webPort"` WebPort int `json:"webPort"`
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -109,6 +108,7 @@ type Info struct {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
var server *raft.Server var server *raft.Server
var serverTransHandler transHandler
var logger *log.Logger var logger *log.Logger
var storeMsg chan string var storeMsg chan string
@ -152,13 +152,13 @@ func main() {
panic("ERROR type") panic("ERROR type")
} }
t := createTranHandler(st) serverTransHandler = createTranHandler(st)
// Setup new raft server. // Setup new raft server.
s := store.GetStore() s := store.GetStore()
// create raft server // create raft server
server, err = raft.NewServer(name, dirPath, t, s, nil) server, err = raft.NewServer(name, dirPath, serverTransHandler, s, nil)
if err != nil { if err != nil {
fatal("%v", err) fatal("%v", err)
@ -262,7 +262,7 @@ func startServTransport(port int, st int) {
http.HandleFunc("/log", GetLogHttpHandler) http.HandleFunc("/log", GetLogHttpHandler)
http.HandleFunc("/log/append", AppendEntriesHttpHandler) http.HandleFunc("/log/append", AppendEntriesHttpHandler)
http.HandleFunc("/snapshot", SnapshotHttpHandler) http.HandleFunc("/snapshot", SnapshotHttpHandler)
http.HandleFunc("/client", clientHttpHandler)
switch st { switch st {
@ -351,7 +351,6 @@ func startClientTransport(port int, st int) {
} }
} }
//-------------------------------------- //--------------------------------------
// Config // Config
//-------------------------------------- //--------------------------------------
@ -480,4 +479,3 @@ func Join(s *raft.Server, serverName string) error {
} }
return fmt.Errorf("Unable to join: %v", err) return fmt.Errorf("Unable to join: %v", err)
} }

View File

@ -13,7 +13,7 @@ import (
) )
//-------------------------------------- //--------------------------------------
// HTTP Handlers // Internal HTTP Handlers via server port
//-------------------------------------- //--------------------------------------
// Get all the current logs // Get all the current logs
@ -72,6 +72,13 @@ func SnapshotHttpHandler(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
} }
func clientHttpHandler(w http.ResponseWriter, req *http.Request) {
debug("[recv] Get http://%v/client/ ", server.Name())
w.WriteHeader(http.StatusOK)
client := address + ":" + strconv.Itoa(clientPort)
w.Write([]byte(client))
}
func JoinHttpHandler(w http.ResponseWriter, req *http.Request) { func JoinHttpHandler(w http.ResponseWriter, req *http.Request) {
command := &JoinCommand{} command := &JoinCommand{}
@ -85,6 +92,9 @@ func JoinHttpHandler(w http.ResponseWriter, req *http.Request) {
} }
} }
//--------------------------------------
// external HTTP Handlers via client port
//--------------------------------------
func SetHttpHandler(w http.ResponseWriter, req *http.Request) { func SetHttpHandler(w http.ResponseWriter, req *http.Request) {
key := req.URL.Path[len("/set/"):] key := req.URL.Path[len("/set/"):]
@ -164,7 +174,7 @@ func excute(c Command, w *http.ResponseWriter, req *http.Request) {
scheme = "http://" scheme = "http://"
} }
url := scheme + server.Leader() + path url := scheme + leaderClient() + path
debug("redirect to ", url) debug("redirect to ", url)
http.Redirect(*w, req, url, http.StatusTemporaryRedirect) http.Redirect(*w, req, url, http.StatusTemporaryRedirect)

19
util.go
View File

@ -1,13 +1,15 @@
package main package main
import ( import (
"net/http"
"io"
"fmt"
"encoding/json" "encoding/json"
"fmt"
"github.com/xiangli-cmu/raft-etcd/web" "github.com/xiangli-cmu/raft-etcd/web"
"io"
"io/ioutil"
"net/http"
"os" "os"
) )
//-------------------------------------- //--------------------------------------
// Web Helper // Web Helper
//-------------------------------------- //--------------------------------------
@ -63,6 +65,17 @@ func Get(t *transHandler, path string) (*http.Response, error) {
} }
} }
func leaderClient() string {
resp, _ := Get(&serverTransHandler, server.Leader()+"/client")
if resp != nil {
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return string(body)
}
return ""
}
//-------------------------------------- //--------------------------------------
// Log // Log
//-------------------------------------- //--------------------------------------