summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2017-05-06 05:01:48 +0200
committerChristian Pointner <equinox@spreadspace.org>2017-05-06 05:01:48 +0200
commitf9ac8f4a7673f186c092a5d02bcca9730ea9a2dc (patch)
tree546259b52a863b1a08d5fc1e5cd790421d0f976d
parentparse from and limit from request query (diff)
implemented bulk receiver for updates via web interfaces (not working yet)
-rw-r--r--src/hub/src/spreadspace.org/sfive/s5srvWeb.go50
-rwxr-xr-xsrc/hub/test-client28
2 files changed, 54 insertions, 24 deletions
diff --git a/src/hub/src/spreadspace.org/sfive/s5srvWeb.go b/src/hub/src/spreadspace.org/sfive/s5srvWeb.go
index fcb07d7..662e08d 100644
--- a/src/hub/src/spreadspace.org/sfive/s5srvWeb.go
+++ b/src/hub/src/spreadspace.org/sfive/s5srvWeb.go
@@ -35,6 +35,7 @@ package sfive
import (
"encoding/json"
"fmt"
+ "io"
"net/http"
"strconv"
@@ -160,26 +161,47 @@ func (srv Server) webGetUpdate(c web.C, w http.ResponseWriter, r *http.Request)
fmt.Fprintf(w, "%s", jsonString)
}
-// TODO: add different API endpoint called bulk
-// container := DataUpdateFullContainer{}
-// err = json.Unmarshal(buffer, &container)
-// if err == nil {
-// if err = srv.AppendMany(container.Data); err != nil {
-// http.Error(w, fmt.Sprintf("failed to store data: %s", err), http.StatusInternalServerError)
-// } else {
-// fmt.Fprintf(w, "%d update(s) successfully stored.", len(container.Data))
-// }
-// return
-// }
+func (srv Server) webPostUpdateBulk(c web.C, w http.ResponseWriter, r *http.Request) {
+ decoder, err := NewStatefulDecoder(r.Body)
+ if err != nil {
+ http.Error(w, fmt.Sprintf("failed to read/decode init message: %v", err), http.StatusBadRequest)
+ return
+ }
+
+ data := []DataUpdateFull{}
+ for {
+ value, err := decoder.Decode()
+ if err != nil {
+ if err != io.EOF {
+ http.Error(w, fmt.Sprintf("failed to read/decode update message: %v", err), http.StatusBadRequest)
+ return
+ }
+ // TODO: check for temporary error?
+ return
+ }
+ data = append(data, value)
+ }
+
+ if err = srv.AppendMany(data); err != nil {
+ http.Error(w, fmt.Sprintf("failed to store data: %s", err), http.StatusInternalServerError)
+ } else {
+ fmt.Fprintf(w, "%d update(s) successfully stored.", len(data))
+ }
+}
func (srv Server) webPostUpdate(c web.C, w http.ResponseWriter, r *http.Request) {
const resourceName = "updates"
- decoder := NewStatelessDecoder(r.Body)
+ if bulk := r.FormValue("bulk"); bulk != "" {
+ srv.webPostUpdateBulk(c, w, r)
+ return
+ }
+
+ decoder := NewStatelessDecoder(r.Body)
data, err := decoder.Decode()
- if err != nil {
+ if err != nil && err != io.EOF {
s5l.Printf("web: failed to decode: %v\n", err)
- http.Error(w, fmt.Sprintf("failed decoding %s: %v", resourceName, err), http.StatusBadRequest)
+ http.Error(w, fmt.Sprintf("failed to read/decode update message: %v", err), http.StatusBadRequest)
return
}
diff --git a/src/hub/test-client b/src/hub/test-client
index fcb9a98..612e576 100755
--- a/src/hub/test-client
+++ b/src/hub/test-client
@@ -1,22 +1,30 @@
-#!/bin/sh
+#!/bin/bash
TEST_D="./test"
-echo pipe: import sample.json
-echo ------------------------
+echo "pipe: import sample.json"
+echo "------------------------"
socat file:../../dat/sample.json,rdonly "unix-client:$TEST_D/pipe"
+echo ""
-echo pipe-gram: import sample-gram.json
-echo ----------------------------------
+echo "pipe-gram: import sample-gram.json"
+echo "----------------------------------"
while read x; do echo "$x" | socat stdio "unix-sendto:$TEST_D/pipegram"; done < ../../dat/sample-gram.json
+echo ""
-echo post update
-echo -----------
+echo "post update"
+echo "-----------"
curl -i --data @../../dat/sample-post.json 'http://localhost:8000/updates'
+echo -e "\n"
-echo show query result
-echo -----------------
-curl -i 'http://localhost:8000/updates'
+echo "post update (bulk)"
+echo "------------------"
+curl -i --data @../../dat/sample-post-bulk.json 'http://localhost:8000/updates?bulk=1'
+echo -e "\n"
+echo "show query result"
+echo "-----------------"
+curl -i 'http://localhost:8000/updates'
+echo ""
echo '\n\ndone'