Impl delete find filters for boltdb

This commit is contained in:
Bel LaPointe
2020-07-23 16:41:50 -06:00
parent 085150a7b5
commit bed265a228
13 changed files with 670 additions and 92 deletions

49
view/httpisnow.go Normal file
View File

@@ -0,0 +1,49 @@
package view
import (
"fmt"
"io/ioutil"
"local/dndex/storage"
"local/dndex/storage/entity"
"local/dndex/storage/operator"
"net/http"
"path"
"github.com/buger/jsonparser"
)
func httpisnow(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
namespace := path.Base(r.URL.Path)
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
name, err := jsonparser.GetString(b, entity.JSONName)
if err != nil {
return fmt.Errorf("cannot find %q: %v", entity.JSONName, err)
}
if name == "" {
http.Error(w, `{"error":"must provide a name"}`, http.StatusBadRequest)
return nil
}
key, err := jsonparser.GetString(b, "set", "key")
if err != nil {
return fmt.Errorf("cannot find %q: %v", "set.key", err)
}
if key == "" {
http.Error(w, `{"error":"must provide a set.key"}`, http.StatusBadRequest)
return nil
}
value, err := jsonparser.GetString(b, "set", "value")
if err != nil {
return fmt.Errorf("cannot find %q: %v", "set.value", err)
}
operator := operator.Set{Key: key, Value: value}
return g.Update(r.Context(), namespace, entity.One{Name: name}, operator)
}

13
view/httpmeet.go Normal file
View File

@@ -0,0 +1,13 @@
package view
import (
"local/dndex/storage"
"net/http"
"path"
)
func httpmeet(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
namespace := path.Base(r.URL.Path)
w.Write([]byte(namespace))
return nil
}

73
view/httpwho.go Normal file
View File

@@ -0,0 +1,73 @@
package view
import (
"context"
"encoding/json"
"local/dndex/storage"
"local/dndex/storage/entity"
"log"
"net/http"
"path"
"strings"
)
func httpwho(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
namespace := strings.TrimLeft(r.URL.Path, path.Dir(r.URL.Path))
if len(namespace) == 0 {
http.NotFound(w, r)
return nil
}
ids := r.URL.Query()["id"]
_, verbose := r.URL.Query()["v"]
_, one := r.URL.Query()["one"]
results := make(map[string]entity.One)
for i := 0; i < len(ids); i++ {
id := ids[i]
one, err := httpwhoOne(r.Context(), g, namespace, id, verbose)
if err != nil {
return err
}
results[id] = one
}
var marshalme interface{}
if one {
for k := range results {
marshalme = results[k]
break
}
} else {
marshalme = results
}
log.Printf("id=%+v, one=%v, verbose=%v, results:%+v", ids, one, verbose, marshalme)
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(marshalme)
}
func httpwhoOne(ctx context.Context, g storage.Graph, namespace, id string, verbose bool) (entity.One, error) {
ones, err := g.List(ctx, namespace, id)
if err != nil {
return entity.One{}, err
}
if len(ones) != 1 {
ones = append(ones, entity.One{})
}
one := ones[0]
if verbose {
ones, err := g.List(ctx, namespace, one.Peers()...)
if err != nil {
return entity.One{}, err
}
for _, another := range ones {
another.Connections = nil
another.Text = ""
for j := range one.Connections {
if one.Connections[j].Name == another.Name {
another.Relationship = one.Connections[j].Relationship
one.Connections[j] = another
}
}
}
}
return one, nil
}

View File

@@ -1,104 +1,64 @@
package view
import (
"context"
"encoding/json"
"fmt"
"local/dndex/config"
"local/dndex/storage"
"local/dndex/storage/entity"
"local/gziphttp"
"log"
"net/http"
"path"
"strings"
)
func JSON(g storage.Graph) error {
port := config.New().Port
log.Println("listening on", port)
err := http.ListenAndServe(fmt.Sprintf(":%d", port), foo(g))
err := http.ListenAndServe(fmt.Sprintf(":%d", port), jsonHandler(g))
return err
}
func foo(g storage.Graph) http.Handler {
func jsonHandler(g storage.Graph) http.Handler {
mux := http.NewServeMux()
routes := []struct {
path string
foo func(g storage.Graph, w http.ResponseWriter, r *http.Request) error
}{
{
path: "/who/",
foo: httpwho,
},
{
path: "/meet/",
foo: httpmeet,
},
{
path: "/isnow/",
foo: httpisnow,
},
}
for _, route := range routes {
path := route.path
nopath := strings.TrimRight(route.path, "/")
foo := route.foo
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
if err := foo(g, w, r); err != nil {
b, _ := json.Marshal(map[string]string{"error": err.Error()})
http.Error(w, string(b), http.StatusInternalServerError)
}
})
mux.HandleFunc(nopath, http.NotFound)
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
if gziphttp.Can(r) {
w = gziphttp.New(w)
}
var err error
switch path.Base(r.URL.Path) {
case "who":
err = who(g, w, r)
default:
http.NotFound(w, r)
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
gz := gziphttp.New(w)
defer gz.Close()
w = gz
}
mux.ServeHTTP(w, r)
})
}
func who(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
namespace := path.Dir(r.URL.Path)
if len(namespace) < 2 {
http.NotFound(w, r)
return nil
}
namespace = strings.Replace(namespace[1:], "/", ".", -1)
ids := r.URL.Query()["id"]
_, verbose := r.URL.Query()["v"]
_, one := r.URL.Query()["one"]
results := make(map[string]entity.One)
for i := 0; i < len(ids); i++ {
id := ids[i]
one, err := whoOne(r.Context(), g, namespace, id, verbose)
if err != nil {
return err
}
results[id] = one
}
var marshalme interface{}
if one {
for k := range results {
marshalme = results[k]
break
}
} else {
marshalme = results
}
log.Printf("id=%+v, one=%v, verbose=%v, results:%+v", ids, one, verbose, marshalme)
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(marshalme)
}
func whoOne(ctx context.Context, g storage.Graph, namespace, id string, verbose bool) (entity.One, error) {
ones, err := g.List(ctx, namespace, id)
if err != nil {
return entity.One{}, err
}
if len(ones) != 1 {
ones = append(ones, entity.One{})
}
one := ones[0]
if verbose {
ones, err := g.List(ctx, namespace, one.Peers()...)
if err != nil {
return entity.One{}, err
}
for _, another := range ones {
another.Connections = nil
another.Text = ""
for j := range one.Connections {
if one.Connections[j].Name == another.Name {
another.Relationship = one.Connections[j].Relationship
one.Connections[j] = another
}
}
}
}
return one, nil
}

View File

@@ -11,6 +11,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
@@ -28,8 +29,27 @@ func TestJSON(t *testing.T) {
ones := fillDB(t, g)
want := ones[len(ones)-1]
handler := foo(g)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://localhost:%d/col/who?id=%s&v", config.New().Port, want.Name), nil)
handler := jsonHandler(g)
t.Run("404 on /who", func(t *testing.T) {
r := httptest.NewRequest("GET", "/who", strings.NewReader(``))
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusNotFound {
t.Error(w.Code)
}
})
t.Run("404 on /who/", func(t *testing.T) {
r := httptest.NewRequest("GET", "/who/", strings.NewReader(``))
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusNotFound {
t.Error(w.Code)
}
})
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://localhost:%d/who/col?id=%s&v", config.New().Port, want.Name), strings.NewReader(``))
if err != nil {
t.Fatal(err)
}