gui done enough
parent
ef25a77254
commit
c76da12b1a
18
http.go
18
http.go
|
|
@ -18,10 +18,11 @@ type Context struct {
|
||||||
|
|
||||||
func HTTP(port int, db DB) error {
|
func HTTP(port int, db DB) error {
|
||||||
foo := func(w http.ResponseWriter, r *http.Request) {
|
foo := func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
if r.URL.Path == "/" {
|
||||||
case "/":
|
httpGUI(w, r)
|
||||||
httpRoot(w, r)
|
} else if strings.HasPrefix(r.URL.Path, "/api/questions") && strings.HasSuffix(r.URL.Path, "/answers") && r.Method == http.MethodPost {
|
||||||
default:
|
httpPostQuestionAnswers(w, r)
|
||||||
|
} else {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -65,10 +66,10 @@ func withAuth(foo http.HandlerFunc) http.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
//go:embed public/root.html
|
//go:embed public/root.html
|
||||||
var httpRootHTML string
|
var httpGUIHTML string
|
||||||
|
|
||||||
func httpRoot(w http.ResponseWriter, r *http.Request) {
|
func httpGUI(w http.ResponseWriter, r *http.Request) {
|
||||||
body := httpRootHTML
|
body := httpGUIHTML
|
||||||
if os.Getenv("DEBUG") != "" {
|
if os.Getenv("DEBUG") != "" {
|
||||||
b, _ := os.ReadFile("public/root.html")
|
b, _ := os.ReadFile("public/root.html")
|
||||||
body = string(b)
|
body = string(b)
|
||||||
|
|
@ -97,3 +98,6 @@ func httpAssignments(ctx context.Context) (interface{}, error) {
|
||||||
}
|
}
|
||||||
return todo, nil
|
return todo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func httpPostQuestionAnswers(w http.ResponseWriter, r *http.Request) {
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,14 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
function pushAnswer(idq, answer, passed) {
|
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) {
|
function nextQuestion(form) {
|
||||||
|
|
@ -110,6 +117,23 @@
|
||||||
form.children.answer.retake = todo[1].Retake
|
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"))
|
nextQuestion(document.getElementById("flash"))
|
||||||
</script>
|
</script>
|
||||||
</footer>
|
</footer>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue