Browse Source

change mux to http, parse the url by handler

Xiang Li 12 years ago
parent
commit
fdd6873768
2 changed files with 27 additions and 25 deletions
  1. 12 13
      handlers.go
  2. 15 12
      raftd.go

+ 12 - 13
handlers.go

@@ -5,7 +5,6 @@ import (
 	"net/http"
 	"encoding/json"
 	"fmt"
-	"github.com/gorilla/mux"
 	"io/ioutil"
 	"bytes"
 	)
@@ -88,9 +87,9 @@ func SnapshotHttpHandler(w http.ResponseWriter, req *http.Request) {
 }
 
 func SetHttpHandler(w http.ResponseWriter, req *http.Request) {
-	vars := mux.Vars(req)
+	key := req.URL.Path[len("/set/"):]
 
-	debug("[recv] POST http://%v/set/%s", server.Name(), vars["key"])
+	debug("[recv] POST http://%v/set/%s", server.Name(), key)
 
 	content, err := ioutil.ReadAll(req.Body)
 	if err != nil {
@@ -100,7 +99,7 @@ func SetHttpHandler(w http.ResponseWriter, req *http.Request) {
 	}
 
 	command := &SetCommand{}
-	command.Key = vars["key"]
+	command.Key = key
 	command.Value = string(content)
 
 	Dispatch(server, command, w)
@@ -108,24 +107,24 @@ func SetHttpHandler(w http.ResponseWriter, req *http.Request) {
 }
 
 func GetHttpHandler(w http.ResponseWriter, req *http.Request) {
-	vars := mux.Vars(req)
+	key := req.URL.Path[len("/get/"):]
 
-	debug("[recv] GET http://%v/get/%s", server.Name(), vars["key"])
+	debug("[recv] GET http://%v/get/%s", server.Name(), key)
 
 	command := &GetCommand{}
-	command.Key = vars["key"]
+	command.Key = key
 
 	Dispatch(server, command, w)
 
 }
 
 func DeleteHttpHandler(w http.ResponseWriter, req *http.Request) {
-	vars := mux.Vars(req)
+	key := req.URL.Path[len("/delete/"):]
 
-	debug("[recv] GET http://%v/delete/%s", server.Name(), vars["key"])
+	debug("[recv] GET http://%v/delete/%s", server.Name(), key)
 
 	command := &DeleteCommand{}
-	command.Key = vars["key"]
+	command.Key = key
 
 	Dispatch(server, command, w)
 
@@ -133,12 +132,12 @@ func DeleteHttpHandler(w http.ResponseWriter, req *http.Request) {
 
 
 func WatchHttpHandler(w http.ResponseWriter, req *http.Request) {
-	vars := mux.Vars(req)
+	key := req.URL.Path[len("/watch/"):]
 
-	debug("[recv] GET http://%v/watch/%s", server.Name(), vars["key"])
+	debug("[recv] GET http://%v/watch/%s", server.Name(), key)
 
 	command := &WatchCommand{}
-	command.Key = vars["key"]
+	command.Key = key
 
 	Dispatch(server, command, w)
 

+ 15 - 12
raftd.go

@@ -6,7 +6,7 @@ import (
 	"flag"
 	"fmt"
 	"github.com/benbjohnson/go-raft"
-	"github.com/gorilla/mux"
+	//"github.com/gorilla/mux"
 	"log"
 	"io"
 	"io/ioutil"
@@ -125,22 +125,25 @@ func main() {
 	//go server.Snapshot()
 	
 	// Create HTTP interface.
-    r := mux.NewRouter()
+    //r := mux.NewRouter()
 
     // internal commands
-    r.HandleFunc("/join", JoinHttpHandler).Methods("POST")
-    r.HandleFunc("/vote", VoteHttpHandler).Methods("POST")
-    r.HandleFunc("/log", GetLogHttpHandler).Methods("GET")
-    r.HandleFunc("/log/append", AppendEntriesHttpHandler).Methods("POST")
-    r.HandleFunc("/snapshot", SnapshotHttpHandler).Methods("POST")
+    http.HandleFunc("/join", JoinHttpHandler)
+    http.HandleFunc("/vote", VoteHttpHandler)
+    http.HandleFunc("/log", GetLogHttpHandler)
+    http.HandleFunc("/log/append", AppendEntriesHttpHandler)
+    http.HandleFunc("/snapshot", SnapshotHttpHandler)
 
     // external commands
-    r.HandleFunc("/set/{key}", SetHttpHandler).Methods("POST")
-    r.HandleFunc("/get/{key}", GetHttpHandler).Methods("GET")
-    r.HandleFunc("/delete/{key}", DeleteHttpHandler).Methods("GET")
-    r.HandleFunc("/watch/{key}", WatchHttpHandler).Methods("GET")
+    http.HandleFunc("/set/", SetHttpHandler)
+    //r.HandleFunc("/get/{key}", GetHttpHandler).Methods("GET")
+    http.HandleFunc("/delete/", DeleteHttpHandler)
+    http.HandleFunc("/watch/", WatchHttpHandler)
+
+    //http.Handle("/", r)
+
+    http.HandleFunc("/get/", GetHttpHandler)
 
-    http.Handle("/", r)
 	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", info.Port), nil))
 }