74 lines
1.6 KiB
Go
74 lines
1.6 KiB
Go
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
|
|
}
|