change mux to http, parse the url by handler

This commit is contained in:
Xiang Li 2013-06-10 15:09:01 -07:00
parent 2e679d257c
commit fdd6873768
2 changed files with 27 additions and 25 deletions

View File

@ -5,7 +5,6 @@ import (
"net/http" "net/http"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/gorilla/mux"
"io/ioutil" "io/ioutil"
"bytes" "bytes"
) )
@ -88,9 +87,9 @@ func SnapshotHttpHandler(w http.ResponseWriter, req *http.Request) {
} }
func SetHttpHandler(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) content, err := ioutil.ReadAll(req.Body)
if err != nil { if err != nil {
@ -100,7 +99,7 @@ func SetHttpHandler(w http.ResponseWriter, req *http.Request) {
} }
command := &SetCommand{} command := &SetCommand{}
command.Key = vars["key"] command.Key = key
command.Value = string(content) command.Value = string(content)
Dispatch(server, command, w) Dispatch(server, command, w)
@ -108,24 +107,24 @@ func SetHttpHandler(w http.ResponseWriter, req *http.Request) {
} }
func GetHttpHandler(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 := &GetCommand{}
command.Key = vars["key"] command.Key = key
Dispatch(server, command, w) Dispatch(server, command, w)
} }
func DeleteHttpHandler(w http.ResponseWriter, req *http.Request) { 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 := &DeleteCommand{}
command.Key = vars["key"] command.Key = key
Dispatch(server, command, w) Dispatch(server, command, w)
@ -133,12 +132,12 @@ func DeleteHttpHandler(w http.ResponseWriter, req *http.Request) {
func WatchHttpHandler(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 := &WatchCommand{}
command.Key = vars["key"] command.Key = key
Dispatch(server, command, w) Dispatch(server, command, w)

View File

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