diff options
author | Christian Pointner <equinox@spreadspace.org> | 2017-05-06 22:18:14 +0200 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2017-05-06 22:18:14 +0200 |
commit | a6e63b666798472f9ea81c028be4e4bdb2956e0e (patch) | |
tree | 94db3a516ca78f675808752bbf6673b5bc372ff9 /src/hub | |
parent | re-added hubs and source listing (diff) |
re-added handler for last update id
Diffstat (limited to 'src/hub')
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5srvWeb.go | 76 | ||||
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5store.go | 6 | ||||
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5store_test.go | 22 |
3 files changed, 76 insertions, 28 deletions
diff --git a/src/hub/src/spreadspace.org/sfive/s5srvWeb.go b/src/hub/src/spreadspace.org/sfive/s5srvWeb.go index 697780b..3593f62 100644 --- a/src/hub/src/spreadspace.org/sfive/s5srvWeb.go +++ b/src/hub/src/spreadspace.org/sfive/s5srvWeb.go @@ -40,6 +40,7 @@ import ( "time" // "strconv" "fmt" + "path" ) type webErrorResponse struct { @@ -224,26 +225,60 @@ func webSources(srv *Server, w http.ResponseWriter, r *http.Request) { // } // } -// func (srv Server) webGetLastUpdateId(c web.C, w http.ResponseWriter, r *http.Request) { -// const resourceName = "lastupdate" -// value, err := srv.store.GetLastUpdateId() -// if err != nil { -// http.Error(w, fmt.Sprintf("failed to retrieve %s: %v", resourceName, err), http.StatusInternalServerError) -// return -// } -// fmt.Fprintf(w, "%d", value) -// } +// /lastupdate -// func (srv Server) webGetLastUpdateIdForUuid(c web.C, w http.ResponseWriter, r *http.Request) { -// const resourceName = "lastupdate" -// id := c.URLParams["id"] -// value, err := srv.store.GetLastUpdateForUuid(id) -// if err != nil { -// http.Error(w, fmt.Sprintf("failed to retrieve %s: %v", resourceName, err), http.StatusInternalServerError) -// return -// } -// fmt.Fprintf(w, "%d", value) -// } +type webLastUpdateIdResponse struct { + Error string `json:"error,omitempty"` + HubUuid string `json:"hub-uuid"` + LastUpdateId int `json:"lastupdate"` +} + +func webLastUpdateId(srv *Server, w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + sendInvalidMethod(w, r.Method) + return + } + + var err error + resp := webLastUpdateIdResponse{} + resp.HubUuid = srv.GetHubId() + if resp.LastUpdateId, err = srv.store.GetLastUpdateId(); err != nil { + resp.Error = fmt.Sprintf("%v", err) + } + sendWebResponse(w, http.StatusOK, resp) +} + +// /lastupdate/:UUID + +type webLastUpdateIdForUuidResponse struct { + Error string `json:"error,omitempty"` + HubUuid string `json:"hub-uuid"` + LastUpdateId int `json:"lastupdate"` +} + +func webLastUpdateIdForUuid(srv *Server, w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + sendInvalidMethod(w, r.Method) + return + } + + if matched, err := path.Match("/lastupdate/*", r.URL.Path); err != nil || !matched { + sendWebResponse(w, http.StatusBadRequest, webErrorResponse{"invalid uuid"}) + return + } + + var err error + resp := webLastUpdateIdForUuidResponse{} + _, resp.HubUuid = path.Split(r.URL.Path) + if resp.LastUpdateId, err = srv.store.GetLastUpdateIdForUuid(resp.HubUuid); err != nil { + resp.Error = fmt.Sprintf("%v", err) + } + sendWebResponse(w, http.StatusOK, resp) +} + +// +// common functions +// func sendInvalidMethod(w http.ResponseWriter, method string) { sendWebResponse(w, http.StatusMethodNotAllowed, webErrorResponse{"invalid request method: " + method}) @@ -286,7 +321,8 @@ func webRun(listener *net.TCPListener, srv *Server) (err error) { mux.Handle("/hubs", webHandler{srv, webHubs}) mux.Handle("/sources", webHandler{srv, webSources}) // mux.Handle("/updates", webHandler{srv, webUpdates}) - // mux.Handle("/lastupdate", webHandler{srv, webUpdateId}) + mux.Handle("/lastupdate", webHandler{srv, webLastUpdateId}) + mux.Handle("/lastupdate/", webHandler{srv, webLastUpdateIdForUuid}) // mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir( ..staticDir.. )))) mux.Handle("/", webHandler{srv, webNotFound}) diff --git a/src/hub/src/spreadspace.org/sfive/s5store.go b/src/hub/src/spreadspace.org/sfive/s5store.go index 46db278..48fee9c 100644 --- a/src/hub/src/spreadspace.org/sfive/s5store.go +++ b/src/hub/src/spreadspace.org/sfive/s5store.go @@ -507,7 +507,11 @@ func (st Store) GetLastUpdateId() (updateId int, err error) { return } -func (st Store) GetLastUpdateForUuid(uuid string) (updateId int, err error) { +func (st Store) GetLastUpdateIdForUuid(uuid string) (updateId int, err error) { + if uuid == st.hubUuid { + return st.GetLastUpdateId() + } + err = st.db.View(func(tx *bolt.Tx) error { bUpdateId := tx.Bucket([]byte(latestUpdatesBn)).Get([]byte(uuid)) if bUpdateId == nil { diff --git a/src/hub/src/spreadspace.org/sfive/s5store_test.go b/src/hub/src/spreadspace.org/sfive/s5store_test.go index 09bd7dd..a6b2178 100644 --- a/src/hub/src/spreadspace.org/sfive/s5store_test.go +++ b/src/hub/src/spreadspace.org/sfive/s5store_test.go @@ -438,6 +438,14 @@ func TestGetUpdatesAfter(t *testing.T) { t.Fatalf("failed to get last update ID: got %d updates, expected 3", lastId) } + lastIdForUuid, err := store.GetLastUpdateIdForUuid(store.hubUuid) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if lastId != lastIdForUuid { + t.Fatalf("last update ID from GetLastUpdateId = %d and GetLastUpdateIdForUuid(store.hubUuid) = %d differ", lastId, lastIdForUuid) + } + // all the updates updList, err := store.GetUpdatesAfter(-1, -1) if err != nil { @@ -507,7 +515,7 @@ func TestForwardedDataUpdates(t *testing.T) { forwardedHub := "05defdfa-e7d1-4ca8-8b5c-02abb0088d29" // check if there are no updates for this hub in store - lastId, err := store.GetLastUpdateForUuid(forwardedHub) + lastId, err := store.GetLastUpdateIdForUuid(forwardedHub) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -559,7 +567,7 @@ func TestForwardedDataUpdates(t *testing.T) { } // check if the last update for this hub is 3 - lastId, err = store.GetLastUpdateForUuid(forwardedHub) + lastId, err = store.GetLastUpdateIdForUuid(forwardedHub) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -590,35 +598,35 @@ func TestForwardedDataUpdates(t *testing.T) { } func checkForwardedDataUpdates2(t *testing.T, src1Store, src2Store, fwdStore, finalStore Store, fwdSrc1Id, fwdSrc2Id, finalSrc1Id, finalSrc2Id, finalFwdId int) { - lastId, err := fwdStore.GetLastUpdateForUuid(src1Store.GetStoreId()) + lastId, err := fwdStore.GetLastUpdateIdForUuid(src1Store.GetStoreId()) if err != nil { t.Fatalf("unexpected error: %v", err) } if lastId != fwdSrc1Id { t.Fatalf("failed to get last update ID: %d, expected %d", lastId, fwdSrc1Id) } - lastId, err = fwdStore.GetLastUpdateForUuid(src2Store.GetStoreId()) + lastId, err = fwdStore.GetLastUpdateIdForUuid(src2Store.GetStoreId()) if err != nil { t.Fatalf("unexpected error: %v", err) } if lastId != fwdSrc2Id { t.Fatalf("failed to get last update ID: %d, expected %d", lastId, fwdSrc2Id) } - lastId, err = finalStore.GetLastUpdateForUuid(src1Store.GetStoreId()) + lastId, err = finalStore.GetLastUpdateIdForUuid(src1Store.GetStoreId()) if err != nil { t.Fatalf("unexpected error: %v", err) } if lastId != finalSrc1Id { t.Fatalf("failed to get last update ID: %d, expected %d", lastId, finalSrc1Id) } - lastId, err = finalStore.GetLastUpdateForUuid(src2Store.GetStoreId()) + lastId, err = finalStore.GetLastUpdateIdForUuid(src2Store.GetStoreId()) if err != nil { t.Fatalf("unexpected error: %v", err) } if lastId != finalSrc2Id { t.Fatalf("failed to get last update ID: %d, expected %d", lastId, finalSrc2Id) } - lastId, err = finalStore.GetLastUpdateForUuid(fwdStore.GetStoreId()) + lastId, err = finalStore.GetLastUpdateIdForUuid(fwdStore.GetStoreId()) if err != nil { t.Fatalf("unexpected error: %v", err) } |