summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2017-05-06 20:25:46 +0200
committerChristian Pointner <equinox@spreadspace.org>2017-05-06 20:25:46 +0200
commit3e23b2deaba4d9644f289fc013e83a91e5bf70f0 (patch)
tree517eed4d02db3a19f408aa35b1758c0d7d57b3d3
parentdrop goji which never did much anyway (diff)
re-added hubs and source listing
-rw-r--r--src/hub/src/spreadspace.org/sfive/s5srvWeb.go94
1 files 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})