Can display but have not yet fetched one
This commit is contained in:
45
view/html.go
Normal file
45
view/html.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package view
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"local/whodunit/config"
|
||||
"local/whodunit/storage"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Html(g storage.Graph) error {
|
||||
err := http.ListenAndServe(fmt.Sprintf(":%d", config.New().Port), foo(g))
|
||||
return err
|
||||
}
|
||||
|
||||
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":
|
||||
err = who(g, w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
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"] {
|
||||
ones, err := g.List(r.Context(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(ones) != 1 {
|
||||
ones = nil
|
||||
}
|
||||
results[id] = ones[0]
|
||||
}
|
||||
return json.NewEncoder(w).Encode(results)
|
||||
}
|
||||
63
view/html_test.go
Normal file
63
view/html_test.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package view
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"local/whodunit/config"
|
||||
"local/whodunit/storage"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestHtml(t *testing.T) {
|
||||
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])
|
||||
|
||||
go func() {
|
||||
if err := Html(g); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
time.Sleep(time.Millisecond * 250)
|
||||
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/who?id=A&id=B", config.New().Port))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var v interface{}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, err := json.MarshalIndent(v, "", " ")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Logf("\n%s\n", b)
|
||||
}
|
||||
Reference in New Issue
Block a user