48 lines
974 B
Go
48 lines
974 B
Go
package view
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"local/whodunit/config"
|
|
"local/whodunit/storage"
|
|
"log"
|
|
"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 = append(ones, storage.One{})
|
|
}
|
|
results[id] = ones[0]
|
|
}
|
|
log.Println("results:", results)
|
|
return json.NewEncoder(w).Encode(results)
|
|
}
|