Repository: incubator-htrace
Updated Branches:
  refs/heads/master 78542437f -> 16fc1bf75


HTRACE-49 Make HTraced's urls more restful


Project: http://git-wip-us.apache.org/repos/asf/incubator-htrace/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-htrace/commit/16fc1bf7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-htrace/tree/16fc1bf7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-htrace/diff/16fc1bf7

Branch: refs/heads/master
Commit: 16fc1bf759c48a74aa5fe6b8f40e81a665cb32d7
Parents: 7854243
Author: Elliott Clark <[email protected]>
Authored: Fri Jan 2 14:34:45 2015 -0800
Committer: Elliott Clark <[email protected]>
Committed: Fri Jan 2 17:15:30 2015 -0800

----------------------------------------------------------------------
 .gitignore                                      |  2 ++
 NOTICE.txt                                      |  3 ++
 .../go/src/org/apache/htrace/htraced/rest.go    | 38 ++++++++++----------
 pom.xml                                         |  1 +
 4 files changed, 24 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/16fc1bf7/.gitignore
----------------------------------------------------------------------
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

http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/16fc1bf7/NOTICE.txt
----------------------------------------------------------------------
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 

http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/16fc1bf7/htrace-core/src/go/src/org/apache/htrace/htraced/rest.go
----------------------------------------------------------------------
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)
-
-       defaultServeH := &defaultServeHandler{}
-       mux.Handle("/", defaultServeH)
+       span.Handle("/{id}/children", findChildrenH).Methods("GET")
 
-       http.ListenAndServe(cnf.Get(conf.HTRACE_WEB_ADDRESS), mux)
+       http.ListenAndServe(cnf.Get(conf.HTRACE_WEB_ADDRESS), r)
        log.Println("Started REST server...")
 }

http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/16fc1bf7/pom.xml
----------------------------------------------------------------------
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. -->
               <!-- Pulled down sources -->
               <exclude>**/gopkg.in/**</exclude>
               <exclude>**/github.com/**</exclude>
+              <exclude>**/golang.org/**</exclude>
             </excludes>
           </configuration>
         </plugin>

Reply via email to