JSON API new spec with deref
parent
4d667e7b11
commit
710e20d6e0
|
|
@ -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"),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -1,6 +0,0 @@
|
|||
package entity
|
||||
|
||||
type Peer struct {
|
||||
Name string `bson:"_id,omitempty"`
|
||||
Relationship string `bson:"relationship,omitempty"`
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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{},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ func NewMongo() Mongo {
|
|||
}
|
||||
return Mongo{
|
||||
client: c,
|
||||
db: "db",
|
||||
db: config.New().Database,
|
||||
col: "col",
|
||||
}
|
||||
}
|
||||
|
|
|
|||
23
view/html.go
23
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)
|
||||
|
|
|
|||
|
|
@ -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{},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue