Add namespacing for users

This commit is contained in:
Bel LaPointe
2020-07-22 22:20:06 -06:00
parent 1a6973b02c
commit 660cd64262
5 changed files with 45 additions and 37 deletions

View File

@@ -8,6 +8,8 @@ import (
"local/dndex/storage/entity"
"log"
"net/http"
"path"
"strings"
)
func Html(g storage.Graph) error {
@@ -20,8 +22,8 @@ func Html(g storage.Graph) error {
func foo(g storage.Graph) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
switch r.URL.Path {
case "/who":
switch path.Base(r.URL.Path) {
case "who":
err = who(g, w, r)
default:
http.NotFound(w, r)
@@ -34,12 +36,18 @@ func foo(g storage.Graph) http.Handler {
}
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"]
results := make(map[string]entity.One)
for i := 0; i < len(ids); i++ {
id := ids[i]
ones, err := g.List(r.Context(), id)
ones, err := g.List(r.Context(), namespace, id)
if err != nil {
return err
}
@@ -48,7 +56,7 @@ func who(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
}
one := ones[0]
if verbose {
ones, err := g.List(r.Context(), one.Peers()...)
ones, err := g.List(r.Context(), namespace, one.Peers()...)
if err != nil {
return err
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"local/dndex/config"
"local/dndex/storage"
"local/dndex/storage/entity"
@@ -33,14 +34,16 @@ func TestHtml(t *testing.T) {
}
}()
time.Sleep(time.Millisecond * 250)
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/who?id=%s&v", config.New().Port, want.Name))
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/col/who?id=%s&v", config.New().Port, want.Name))
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
var v interface{}
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
if b, err := ioutil.ReadAll(resp.Body); err != nil {
t.Fatal(err)
} else if err := json.Unmarshal(b, &v); err != nil {
t.Fatalf("err unmarshalling response: %v: %s", err, b)
}
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
@@ -101,10 +104,10 @@ func fillDB(t *testing.T, g storage.Graph) []entity.One {
}
}
for i := range ones {
if err := g.Insert(context.TODO(), ones[i]); err != nil {
if err := g.Insert(context.TODO(), "col", ones[i]); err != nil {
t.Fatal(err)
}
if results, err := g.List(context.TODO(), ones[i].Name); err != nil {
if results, err := g.List(context.TODO(), "col", ones[i].Name); err != nil {
t.Fatal(err)
} else if len(results) != 1 {
t.Fatal(len(results))