From 3e23b2deaba4d9644f289fc013e83a91e5bf70f0 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 6 May 2017 20:25:46 +0200 Subject: re-added hubs and source listing --- src/hub/src/spreadspace.org/sfive/s5srvWeb.go | 94 +++++++++++++++++---------- 1 file changed, 60 insertions(+), 34 deletions(-) diff --git a/src/hub/src/spreadspace.org/sfive/s5srvWeb.go b/src/hub/src/spreadspace.org/sfive/s5srvWeb.go index 9b5542b..697780b 100644 --- a/src/hub/src/spreadspace.org/sfive/s5srvWeb.go +++ b/src/hub/src/spreadspace.org/sfive/s5srvWeb.go @@ -39,16 +39,21 @@ import ( "net/http" "time" // "strconv" + "fmt" ) -type webNotFoundResponse struct { +type webErrorResponse struct { Error string `json:"error,omitempty"` } func webNotFound(srv *Server, w http.ResponseWriter, r *http.Request) { - sendWebResponse(w, http.StatusNotFound, webNotFoundResponse{"not found"}) + // TODO: show index on '^/$' + + sendWebResponse(w, http.StatusNotFound, webErrorResponse{"not found"}) } +// /healthz + type webHealthzResponse struct { Error string `json:"error,omitempty"` Status string `json:"status"` @@ -56,41 +61,58 @@ type webHealthzResponse struct { } func webHealthz(srv *Server, w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + sendInvalidMethod(w, r.Method) + return + } + resp := webHealthzResponse{} resp.HubId = srv.GetHubId() resp.Status = "OK" // TODO: do a more sophisticated check sendWebResponse(w, http.StatusOK, resp) } -// func (srv Server) webGetHubsList(c web.C, w http.ResponseWriter, r *http.Request) { -// const resourceName = "hubs" -// values, err := srv.store.GetHubs() -// if err != nil { -// http.Error(w, fmt.Sprintf("failed to retrieve %s: %v", resourceName, err), http.StatusInternalServerError) -// return -// } -// jsonString, err := json.Marshal(GenericDataContainer{values}) -// if err != nil { -// http.Error(w, fmt.Sprintf("failed to marshal %s: %v", resourceName, err), http.StatusInternalServerError) -// return -// } -// fmt.Fprintf(w, "%s", jsonString) -// } +// /hubs -// func (srv Server) webGetSourcesList(c web.C, w http.ResponseWriter, r *http.Request) { -// const resourceName = "sources" -// values, err := srv.store.GetSources() -// if err != nil { -// http.Error(w, fmt.Sprintf("failed to retrieve %s: %v", resourceName, err), http.StatusInternalServerError) -// return -// } -// jsonString, err := json.Marshal(GenericDataContainer{values}) -// if err != nil { -// http.Error(w, fmt.Sprintf("failed to marshal %s: %v", resourceName, err), http.StatusInternalServerError) -// return -// } -// fmt.Fprintf(w, "%s", jsonString) -// } +type webHubsResponse struct { + Error string `json:"error,omitempty"` + Hubs []string `json:"hubs"` +} + +func webHubs(srv *Server, w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + sendInvalidMethod(w, r.Method) + return + } + + var err error + resp := webHubsResponse{} + if resp.Hubs, err = srv.store.GetHubs(); err != nil { + resp.Error = fmt.Sprintf("%v", err) + } + sendWebResponse(w, http.StatusOK, resp) +} + +// /sources + +type webSourcesResponse struct { + Error string `json:"error,omitempty"` + Sources []SourceId `json:"sources"` +} + +func webSources(srv *Server, w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + sendInvalidMethod(w, r.Method) + return + } + + var err error + resp := webSourcesResponse{} + if resp.Sources, err = srv.store.GetSources(); err != nil { + resp.Error = fmt.Sprintf("%v", err) + } + sendWebResponse(w, http.StatusOK, resp) +} // func (srv Server) webGetUpdateList(c web.C, w http.ResponseWriter, r *http.Request) { // const resourceName = "updates" @@ -223,6 +245,10 @@ func webHealthz(srv *Server, w http.ResponseWriter, r *http.Request) { // fmt.Fprintf(w, "%d", value) // } +func sendInvalidMethod(w http.ResponseWriter, method string) { + sendWebResponse(w, http.StatusMethodNotAllowed, webErrorResponse{"invalid request method: " + method}) +} + func sendWebResponse(w http.ResponseWriter, status int, respdata interface{}) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) @@ -257,10 +283,10 @@ func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) { func webRun(listener *net.TCPListener, srv *Server) (err error) { mux := http.NewServeMux() mux.Handle("/healthz", webHandler{srv, webHealthz}) - // mux.Handle("/hubs", webHandler{srv, webGetHubsList}) - // mux.Handle("/sources", webHandler{srv, webGetSourcesList}) - // mux.Handle("/updates", webHandler{srv, webGetUpdateList}) - // mux.Handle("/lastupdate", webHandler{srv, webGetLastUpdateId}) + 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("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir( ..staticDir.. )))) mux.Handle("/", webHandler{srv, webNotFound}) -- cgit v1.2.3