gui done enough

master
Bel LaPointe 2023-04-07 13:33:09 -06:00
parent ef25a77254
commit c76da12b1a
2 changed files with 36 additions and 8 deletions

18
http.go
View File

@ -18,10 +18,11 @@ type Context struct {
func HTTP(port int, db DB) error {
foo := func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/":
httpRoot(w, r)
default:
if r.URL.Path == "/" {
httpGUI(w, r)
} else if strings.HasPrefix(r.URL.Path, "/api/questions") && strings.HasSuffix(r.URL.Path, "/answers") && r.Method == http.MethodPost {
httpPostQuestionAnswers(w, r)
} else {
http.NotFound(w, r)
}
}
@ -65,10 +66,10 @@ func withAuth(foo http.HandlerFunc) http.HandlerFunc {
}
//go:embed public/root.html
var httpRootHTML string
var httpGUIHTML string
func httpRoot(w http.ResponseWriter, r *http.Request) {
body := httpRootHTML
func httpGUI(w http.ResponseWriter, r *http.Request) {
body := httpGUIHTML
if os.Getenv("DEBUG") != "" {
b, _ := os.ReadFile("public/root.html")
body = string(b)
@ -97,3 +98,6 @@ func httpAssignments(ctx context.Context) (interface{}, error) {
}
return todo, nil
}
func httpPostQuestionAnswers(w http.ResponseWriter, r *http.Request) {
}

View File

@ -71,7 +71,14 @@
}
function pushAnswer(idq, answer, passed) {
console.log(idq, answer, passed)
http("post", `/api/questions/${idq}/answers`, noopcallback,
JSON.stringify(
{
"answer": answer,
"passed": new Boolean(passed),
}
),
)
}
function nextQuestion(form) {
@ -110,6 +117,23 @@
form.children.answer.retake = todo[1].Retake
}
function noopcallback(responseBody, responseStatus) {
console.log(responseStatus, responseBody)
}
function http(method, remote, callback, body) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
callback(xmlhttp.responseText, xmlhttp.status)
}
};
xmlhttp.open(method, remote, true);
if (typeof body == "undefined") {
body = null
}
xmlhttp.send(body);
}
nextQuestion(document.getElementById("flash"))
</script>
</footer>