Skip to content

Commit

Permalink
change mux to http, parse the url by handler
Browse files Browse the repository at this point in the history
  • Loading branch information
xiang90 committed Jun 10, 2013
1 parent 2e679d2 commit fdd6873
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
25 changes: 12 additions & 13 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"io/ioutil"
"bytes"
)
Expand Down Expand Up @@ -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 {
Expand All @@ -100,45 +99,45 @@ 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)

}

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)

}


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)

Expand Down
27 changes: 15 additions & 12 deletions raftd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"flag"
"fmt"
"github.com/benbjohnson/go-raft"
"github.com/gorilla/mux"
//"github.com/gorilla/mux"
"log"
"io"
"io/ioutil"
Expand Down Expand Up @@ -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))
}

Expand Down

0 comments on commit fdd6873

Please sign in to comment.