From 16fc1bf759c48a74aa5fe6b8f40e81a665cb32d7 Mon Sep 17 00:00:00 2001 From: Elliott Clark Date: Fri, 2 Jan 2015 14:34:45 -0800 Subject: [PATCH] HTRACE-49 Make HTraced's urls more restful --- .gitignore | 2 ++ NOTICE.txt | 3 ++ .../src/go/src/org/apache/htrace/htraced/rest.go | 38 ++++++++++------------ pom.xml | 1 + 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index f6c1807..1c92de9 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,6 @@ htrace-core/src/go/pkg htrace-core/src/go/src/org/apache/htrace/resource htrace-core/src/go/src/github.com/ htrace-core/src/go/src/gopkg.in/ +htrace-core/src/go/src/code.google.com/ +htrace-core/src/go/src/golang.org/ htrace-core/src/go/src/org/apache/htrace/common/version.go diff --git a/NOTICE.txt b/NOTICE.txt index 19f97eb..c377812 100644 --- a/NOTICE.txt +++ b/NOTICE.txt @@ -16,3 +16,6 @@ It is by alecthomas: https://github.com/alecthomas/units Kingpin, a go command line and flag parser is licensed MIT (https://github.com/alecthomas/kingpin/blob/master/COPYING) by alecthomas + +Gorilla mux gorilla/mux implements a request router and dispatcher is BSD licensed +( https://github.com/gorilla/mux/blob/master/LICENSE diff --git a/htrace-core/src/go/src/org/apache/htrace/htraced/rest.go b/htrace-core/src/go/src/org/apache/htrace/htraced/rest.go index 73409fe..61f4a02 100644 --- a/htrace-core/src/go/src/org/apache/htrace/htraced/rest.go +++ b/htrace-core/src/go/src/org/apache/htrace/htraced/rest.go @@ -21,6 +21,7 @@ package main import ( "encoding/json" + "github.com/gorilla/mux" "log" "mime" "net/http" @@ -51,18 +52,11 @@ type dataStoreHandler struct { store *dataStore } -func (hand *dataStoreHandler) getReqField64(fieldName string, w http.ResponseWriter, - req *http.Request) (int64, bool) { - str := req.FormValue(fieldName) - if str == "" { - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("No " + fieldName + " specified.")) - return -1, false - } +func (hand *dataStoreHandler) parse64(w http.ResponseWriter, str string) (int64, bool) { val, err := strconv.ParseUint(str, 16, 64) if err != nil { w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("Error parsing " + fieldName + ": " + err.Error())) + w.Write([]byte("Error parsing : " + err.Error())) return -1, false } return int64(val), true @@ -91,7 +85,9 @@ type findSidHandler struct { func (hand *findSidHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { req.ParseForm() - sid, ok := hand.getReqField64("sid", w, req) + vars := mux.Vars(req) + stringSid := vars["id"] + sid, ok := hand.parse64(w, stringSid) if !ok { return } @@ -109,7 +105,9 @@ type findChildrenHandler struct { func (hand *findChildrenHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { req.ParseForm() - sid, ok := hand.getReqField64("sid", w, req) + vars := mux.Vars(req) + stringSid := vars["id"] + sid, ok := hand.parse64(w, stringSid) if !ok { return } @@ -149,20 +147,20 @@ func (hand *defaultServeHandler) ServeHTTP(w http.ResponseWriter, req *http.Requ } func startRestServer(cnf *conf.Config, store *dataStore) { - mux := http.NewServeMux() - serverInfoH := &serverInfoHandler{} - mux.Handle("/serverInfo", serverInfoH) + r := mux.NewRouter().StrictSlash(false) + // Default Handler. This will serve requests for static requests. + r.Handle("/", &defaultServeHandler{}) + r.Handle("/server/info", &serverInfoHandler{}).Methods("GET") + + span := r.PathPrefix("/span").Subrouter() findSidH := &findSidHandler{dataStoreHandler: dataStoreHandler{store: store}} - mux.Handle("/findSid", findSidH) + span.Handle("/{id}", findSidH).Methods("GET") findChildrenH := &findChildrenHandler{dataStoreHandler: dataStoreHandler{store: store}} - mux.Handle("/findChildren", findChildrenH) + span.Handle("/{id}/children", findChildrenH).Methods("GET") - defaultServeH := &defaultServeHandler{} - mux.Handle("/", defaultServeH) - - http.ListenAndServe(cnf.Get(conf.HTRACE_WEB_ADDRESS), mux) + http.ListenAndServe(cnf.Get(conf.HTRACE_WEB_ADDRESS), r) log.Println("Started REST server...") } diff --git a/pom.xml b/pom.xml index 5977a9e..a85149a 100644 --- a/pom.xml +++ b/pom.xml @@ -164,6 +164,7 @@ language governing permissions and limitations under the License. --> **/gopkg.in/** **/github.com/** + **/golang.org/** -- 2.2.1