Can display but have not yet fetched one
parent
1b051ee1d5
commit
43746485d4
5
init.sh
5
init.sh
|
|
@ -20,4 +20,9 @@ mshell() {
|
||||||
--eval "$*"
|
--eval "$*"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
remove() {
|
||||||
|
mshell \
|
||||||
|
'db.getSiblingDB("db")["col"].remove({})'
|
||||||
|
}
|
||||||
|
|
||||||
export DBURI=${DB_URI:-"mongodb://localhost:$port"}
|
export DBURI=${DB_URI:-"mongodb://localhost:$port"}
|
||||||
|
|
|
||||||
6
main.go
6
main.go
|
|
@ -2,10 +2,16 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"local/whodunit/config"
|
"local/whodunit/config"
|
||||||
|
"local/whodunit/storage"
|
||||||
|
"local/whodunit/view"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
c := config.New()
|
c := config.New()
|
||||||
log.Println(c)
|
log.Println(c)
|
||||||
|
g := storage.NewGraph()
|
||||||
|
if err := view.Html(g); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,88 @@
|
||||||
|
<html>
|
||||||
|
<header>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<script>
|
||||||
|
function who(form) {
|
||||||
|
var items = {}
|
||||||
|
for (var i=0; i<form.length; i++) {
|
||||||
|
if (form[i].name)
|
||||||
|
items[form[i].name] = form[i].value
|
||||||
|
}
|
||||||
|
console.log(items)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function draw(one) {
|
||||||
|
var id = one.id
|
||||||
|
var meta = one.meta
|
||||||
|
var know = one.know
|
||||||
|
var html = "<div>\n"
|
||||||
|
html += '<h2>' + id + '</h2>\n'
|
||||||
|
html += '<ul>\n'
|
||||||
|
for(var k in meta) {
|
||||||
|
html += '<li>'
|
||||||
|
html += k + ': '
|
||||||
|
var lines = meta[k].split("\n")
|
||||||
|
if (lines.length == 1) {
|
||||||
|
html += lines[0]
|
||||||
|
} else {
|
||||||
|
html += "<ul>"
|
||||||
|
lines.forEach(function(e) {
|
||||||
|
html += "<li>" + e + "</li>"
|
||||||
|
})
|
||||||
|
html += "</ul>"
|
||||||
|
}
|
||||||
|
html += '</li>\n'
|
||||||
|
}
|
||||||
|
html += '</ul>\n'
|
||||||
|
html += '<h3>Knows</h3>\n'
|
||||||
|
html += '<ul>\n'
|
||||||
|
for(var k of know) {
|
||||||
|
html += '<li>\n'
|
||||||
|
var action = '\'who([{name: "id", value: "' + k.id + '"}])\''
|
||||||
|
html += "<a href='#' onclick=" + action + ">"
|
||||||
|
html += k.id + ", <i>" + k.relation + " </i>"
|
||||||
|
html += "</a>"
|
||||||
|
html += '</li>\n'
|
||||||
|
}
|
||||||
|
html += '</ul>\n'
|
||||||
|
html += "</div>\n"
|
||||||
|
document.getElementById("display").innerHTML += html
|
||||||
|
}
|
||||||
|
|
||||||
|
function draw_clear() {
|
||||||
|
document.getElementById("display").innerHTML = ""
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</header>
|
||||||
|
<body>
|
||||||
|
<h1>Goodbye World</h1>
|
||||||
|
<form onsubmit="who(this); return false;">
|
||||||
|
<input name="id" type="text"/>
|
||||||
|
<input type="submit"/>
|
||||||
|
</form>
|
||||||
|
<div id="display">
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
<footer>
|
||||||
|
<script>
|
||||||
|
draw({
|
||||||
|
id: "id",
|
||||||
|
meta: {
|
||||||
|
"20200710": "hello",
|
||||||
|
"20200711": "world\n!",
|
||||||
|
},
|
||||||
|
know: [
|
||||||
|
{
|
||||||
|
id: "id0",
|
||||||
|
relation: ":)",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "id1",
|
||||||
|
relation: ":(",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
</footer>
|
||||||
|
</html>
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue