From 710e20d6e00978cea8ec9fefe96e5f8bd22747ae Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Wed, 22 Jul 2020 20:40:44 -0600 Subject: [PATCH] JSON API new spec with deref --- config/config.go | 17 +++++++++--- storage/entity/one.go | 15 ++++++----- storage/entity/peer.go | 6 ----- storage/graph.go | 4 +++ storage/graph_test.go | 6 ++--- storage/mon.go | 2 +- view/html.go | 23 +++++++++++++--- view/html_test.go | 59 +++++++++++++++++++++++++++--------------- 8 files changed, 86 insertions(+), 46 deletions(-) delete mode 100644 storage/entity/peer.go diff --git a/config/config.go b/config/config.go index e96398d..1a010a7 100644 --- a/config/config.go +++ b/config/config.go @@ -3,8 +3,11 @@ package config import "local/args" type Config struct { - Port int - DBURI string + Port int + DBURI string + Database string + FilePrefix string + FileRoot string } func New() Config { @@ -12,13 +15,19 @@ func New() Config { as.Append(args.INT, "p", "port to listen on", 18114) as.Append(args.STRING, "dburi", "database uri", "mongodb://localhost:27017") + as.Append(args.STRING, "fileprefix", "path prefix for file service", "/__files__") + as.Append(args.STRING, "fileroot", "path to file hosting root", "/tmp/") + as.Append(args.STRING, "database", "database name to use", "db") if err := as.Parse(); err != nil { panic(err) } return Config{ - Port: as.GetInt("p"), - DBURI: as.GetString("dburi"), + Port: as.GetInt("p"), + DBURI: as.GetString("dburi"), + FilePrefix: as.GetString("fileprefix"), + FileRoot: as.GetString("fileroot"), + Database: as.GetString("database"), } } diff --git a/storage/entity/one.go b/storage/entity/one.go index 2673932..96c68e9 100644 --- a/storage/entity/one.go +++ b/storage/entity/one.go @@ -12,13 +12,14 @@ const ( ) type One struct { - Name string `bson:"_id,omitempty"` - Type string `bson:"type,omitempty"` - Title string `bson:"title,omitempty"` - Image string `bson:"image,omitempty"` - Text string `bson:"text,omitempty"` - Modified int64 `bson:"modified,omitempty"` - Connections []Peer `bson:"connections,omitempty"` + Name string `bson:"_id,omitempty" json:"name,omitempty"` + Type string `bson:"type,omitempty" json:"type,omitempty"` + Title string `bson:"title,omitempty" json:"title,omitempty"` + Image string `bson:"image,omitempty" json:"image,omitempty"` + Text string `bson:"text,omitempty" json:"text,omitempty"` + Relationship string `bson:"relationship,omitempty" json:"relationship,omitempty"` + Modified int64 `bson:"modified,omitempty" json:"modified,omitempty"` + Connections []One `bson:"connections,omitempty" json:"connections,omitempty"` } func (o One) Query() One { diff --git a/storage/entity/peer.go b/storage/entity/peer.go deleted file mode 100644 index 98106cc..0000000 --- a/storage/entity/peer.go +++ /dev/null @@ -1,6 +0,0 @@ -package entity - -type Peer struct { - Name string `bson:"_id,omitempty"` - Relationship string `bson:"relationship,omitempty"` -} diff --git a/storage/graph.go b/storage/graph.go index 5b29268..77072f8 100644 --- a/storage/graph.go +++ b/storage/graph.go @@ -43,3 +43,7 @@ func (g Graph) Insert(ctx context.Context, one entity.One) error { func (g Graph) Update(ctx context.Context, one entity.One, modify interface{}) error { return g.mongo.Update(ctx, one, modify) } + +func (g Graph) Delete(ctx context.Context, filter interface{}) error { + return g.mongo.Delete(ctx, filter) +} diff --git a/storage/graph_test.go b/storage/graph_test.go index c4d57af..00b7211 100644 --- a/storage/graph_test.go +++ b/storage/graph_test.go @@ -34,7 +34,7 @@ func TestIntegration(t *testing.T) { randomOne(), randomOne(), } - ones[0].Connections = []entity.Peer{entity.Peer{Name: ones[2].Name, Relationship: ":("}} + ones[0].Connections = []entity.One{entity.One{Name: ones[2].Name, Relationship: ":("}} t.Run("graph.Insert(...)", func(t *testing.T) { for _, one := range ones { @@ -91,7 +91,7 @@ func TestIntegration(t *testing.T) { }) t.Run("graph.Update(foo, ++...); graph.Update(foo, --if :()", func(t *testing.T) { - err := graph.Update(ctx, ones[0].Query(), operator.Set{entity.Connections, []entity.Peer{entity.Peer{Name: "hello", Relationship: ":("}}}) + err := graph.Update(ctx, ones[0].Query(), operator.Set{entity.Connections, []entity.One{entity.One{Name: "hello", Relationship: ":("}}}) if err != nil { t.Fatal(err) } @@ -139,6 +139,6 @@ func randomOne() entity.One { Image: "/path/to.jpg", Text: "tee hee xd", Modified: time.Now().UnixNano(), - Connections: []entity.Peer{}, + Connections: []entity.One{}, } } diff --git a/storage/mon.go b/storage/mon.go index 12251d8..f391529 100644 --- a/storage/mon.go +++ b/storage/mon.go @@ -34,7 +34,7 @@ func NewMongo() Mongo { } return Mongo{ client: c, - db: "db", + db: config.New().Database, col: "col", } } diff --git a/view/html.go b/view/html.go index ebba75e..454260d 100644 --- a/view/html.go +++ b/view/html.go @@ -5,6 +5,7 @@ import ( "fmt" "local/whodunit/config" "local/whodunit/storage" + "local/whodunit/storage/entity" "log" "net/http" ) @@ -33,16 +34,30 @@ func foo(g storage.Graph) http.Handler { } func who(g storage.Graph, w http.ResponseWriter, r *http.Request) error { - results := make(map[string]storage.One) - for _, id := range r.URL.Query()["id"] { + 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) if err != nil { return err } if len(ones) != 1 { - ones = append(ones, storage.One{}) + ones = append(ones, entity.One{}) + } + one := ones[0] + results[id] = one + if verbose { + ones, err := g.List(r.Context(), one.Peers()...) + if err != nil { + return err + } + for i, one := range ones { + one.Connections = nil + results[id].Connections[i] = one + } } - results[id] = ones[0] } log.Println("results:", results) return json.NewEncoder(w).Encode(results) diff --git a/view/html_test.go b/view/html_test.go index 685a064..8d86781 100644 --- a/view/html_test.go +++ b/view/html_test.go @@ -6,39 +6,25 @@ import ( "fmt" "local/whodunit/config" "local/whodunit/storage" + "local/whodunit/storage/entity" "net/http" "os" "testing" "time" + + "github.com/google/uuid" ) func TestHtml(t *testing.T) { - if len(os.Getenv("INTEGRATION")) > 0 { + if len(os.Getenv("INTEGRATION")) == 0 { t.Logf("skipping because $INTEGRATION unset") return } os.Args = os.Args[:1] - g := storage.NewGraph() - ones := []storage.One{ - storage.One{ - ID: "A", - Know: []storage.One{storage.One{}}, - }, - storage.One{ - ID: "B", - Know: []storage.One{storage.One{}}, - }, - } - ones[0].Know[0] = ones[1] - ones[0].Know[0].Know = nil - ones[0].Know[0].Relation = ":)" - ones[1].Know[0] = ones[0] - ones[1].Know[0].Know = nil - ones[1].Know[0].Relation = ":(" - g.Insert(context.TODO(), ones[0]) - g.Insert(context.TODO(), ones[1]) + g := storage.NewGraph() + ones := fillDB(t, g) go func() { if err := Html(g); err != nil { @@ -46,7 +32,7 @@ func TestHtml(t *testing.T) { } }() time.Sleep(time.Millisecond * 250) - resp, err := http.Get(fmt.Sprintf("http://localhost:%d/who?id=A&id=B", config.New().Port)) + resp, err := http.Get(fmt.Sprintf("http://localhost:%d/who?id=%s&v", config.New().Port, ones[len(ones)-1].Name)) if err != nil { t.Fatal(err) } @@ -61,3 +47,34 @@ func TestHtml(t *testing.T) { } t.Logf("\n%s\n", b) } + +func fillDB(t *testing.T, g storage.Graph) []entity.One { + ones := make([]entity.One, 5) + for i := range ones { + ones[i] = randomOne() + if i > 0 { + ones[i].Connections = []entity.One{entity.One{ + Name: ones[i-1].Name, + Relationship: ":D", + }} + } + } + for i := range ones { + if err := g.Insert(context.TODO(), ones[i]); err != nil { + t.Fatal(err) + } + } + return ones +} + +func randomOne() entity.One { + return entity.One{ + Name: "name-" + uuid.New().String()[:5], + Type: "type-" + uuid.New().String()[:5], + Title: "titl-" + uuid.New().String()[:5], + Image: "imge-" + uuid.New().String()[:5], + Text: "text-" + uuid.New().String()[:5], + Modified: time.Now().UnixNano(), + Connections: []entity.One{}, + } +}