stub
parent
273b412e30
commit
fe6b87563f
40
http.go
40
http.go
|
|
@ -8,10 +8,12 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Context struct {
|
type Context struct {
|
||||||
User string
|
DB DB
|
||||||
|
User IDU
|
||||||
}
|
}
|
||||||
|
|
||||||
func HTTP(port int, db DB) error {
|
func HTTP(port int, db DB) error {
|
||||||
|
|
@ -24,6 +26,7 @@ func HTTP(port int, db DB) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
foo = withAuth(foo)
|
foo = withAuth(foo)
|
||||||
|
foo = withDB(foo, db)
|
||||||
return http.ListenAndServe(fmt.Sprintf(":%d", port), http.HandlerFunc(foo))
|
return http.ListenAndServe(fmt.Sprintf(":%d", port), http.HandlerFunc(foo))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -37,6 +40,15 @@ func inject(ctx context.Context, v Context) context.Context {
|
||||||
return context.WithValue(ctx, "__context", v)
|
return context.WithValue(ctx, "__context", v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func withDB(foo http.HandlerFunc, db DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
c := extract(r.Context())
|
||||||
|
c.DB = db
|
||||||
|
r = r.WithContext(inject(r.Context(), c))
|
||||||
|
foo(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func withAuth(foo http.HandlerFunc) http.HandlerFunc {
|
func withAuth(foo http.HandlerFunc) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
u, _, ok := r.BasicAuth()
|
u, _, ok := r.BasicAuth()
|
||||||
|
|
@ -46,7 +58,7 @@ func withAuth(foo http.HandlerFunc) http.HandlerFunc {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c := extract(r.Context())
|
c := extract(r.Context())
|
||||||
c.User = u
|
c.User = IDU(u)
|
||||||
r = r.WithContext(inject(r.Context(), c))
|
r = r.WithContext(inject(r.Context(), c))
|
||||||
foo(w, r)
|
foo(w, r)
|
||||||
}
|
}
|
||||||
|
|
@ -62,8 +74,26 @@ func httpRoot(w http.ResponseWriter, r *http.Request) {
|
||||||
body = string(b)
|
body = string(b)
|
||||||
}
|
}
|
||||||
ctx := extract(r.Context())
|
ctx := extract(r.Context())
|
||||||
body = strings.ReplaceAll(body, "{{USER}}", ctx.User)
|
body = strings.ReplaceAll(body, "{{USER}}", string(ctx.User))
|
||||||
assignments, _ := json.Marshal(nil)
|
assignments, err := httpAssignments(r.Context())
|
||||||
body = strings.ReplaceAll(body, "{{ASSIGNMENTS_JSON}}", string(assignments))
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
assignmentsB, _ := json.Marshal(assignments)
|
||||||
|
body = strings.ReplaceAll(body, "{{ASSIGNMENTS_JSON}}", string(assignmentsB))
|
||||||
w.Write([]byte(body))
|
w.Write([]byte(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func httpAssignments(ctx context.Context) (interface{}, error) {
|
||||||
|
db := extract(ctx).DB
|
||||||
|
user := extract(ctx).User
|
||||||
|
todo := map[IDQ]Question{}
|
||||||
|
for q, _ := range db.HistoryOf(user) {
|
||||||
|
if time.Until(db.Next(user, q)) > 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
todo[q] = db.Question(q)
|
||||||
|
}
|
||||||
|
return todo, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,22 +13,51 @@
|
||||||
).toString(36);
|
).toString(36);
|
||||||
console.log("session", session);
|
console.log("session", session);
|
||||||
</script>
|
</script>
|
||||||
<script>
|
|
||||||
let knowledgebase = {{ASSIGNMENTS_JSON}};
|
|
||||||
console.log(knowledgebase);
|
|
||||||
</script>
|
|
||||||
</header>
|
</header>
|
||||||
<body>
|
<body>
|
||||||
{{USER}}
|
<!--{{USER}}-->
|
||||||
<form id="flash" action="" onsubmit="trySolve(this.children.idq.value, this.children.answer.value); return false;">
|
<form id="flash" action="" onsubmit="return false; trySolve(this.children.idq.value, this.children.answer.value); return false;">
|
||||||
<input type="text" name="idq" readonly=true value="" style="display: none;">
|
<input type="text" name="idq" readonly=true value="" style="display: none;">
|
||||||
<div name="question"></div>
|
<div name="question"></div>
|
||||||
<div name="clues"></div>
|
<div name="clues"></div>
|
||||||
<input type="button" value="clue">
|
<input type="button" value="clue">
|
||||||
<input type="text" name="answer">
|
<input type="text" name="answer">
|
||||||
<input type="submit" value="submit">
|
<input type="submit" value="submit">
|
||||||
|
<input type="button" value="skip" onclick="nextQuestion(this.parentElement)">
|
||||||
</form>
|
</form>
|
||||||
</body>
|
</body>
|
||||||
<footer>
|
<footer>
|
||||||
|
<script>
|
||||||
|
let knowledgebase = {{ASSIGNMENTS_JSON}};
|
||||||
|
knowledgebase = Object.
|
||||||
|
keys(knowledgebase).
|
||||||
|
map((key) => [
|
||||||
|
[key, knowledgebase[key]]
|
||||||
|
]);
|
||||||
|
|
||||||
|
function nextQuestion(form) {
|
||||||
|
form.children.answer.value = "";
|
||||||
|
let todo = knowledgebase.pop();
|
||||||
|
if (!todo) {
|
||||||
|
todo = [0];
|
||||||
|
}
|
||||||
|
todo = todo[0];
|
||||||
|
if (! todo) {
|
||||||
|
todo = ["", {Q: "ALL DONE"}];
|
||||||
|
}
|
||||||
|
|
||||||
|
form.children.idq.value = todo[0];
|
||||||
|
|
||||||
|
console.log(todo[1]);
|
||||||
|
form.children.question.innerHTML = `<h3>${todo[1].Q}</h3>`;
|
||||||
|
let clues = ""
|
||||||
|
for (var i of todo[1].Clues) {
|
||||||
|
clues += `<br>${i}`
|
||||||
|
}
|
||||||
|
form.children.clues.innerHTML = clues;
|
||||||
|
}
|
||||||
|
|
||||||
|
nextQuestion(document.getElementById("flash"));
|
||||||
|
</script>
|
||||||
</footer>
|
</footer>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue