Compare commits

...

18 Commits

Author SHA1 Message Date
Bel LaPointe d32d8059bb todo 2023-04-07 14:06:42 -06:00
Bel LaPointe 64e992c6b6 shuffle http 2023-04-07 14:01:52 -06:00
Bel LaPointe 0fd0981a39 Q can be image in http 2023-04-07 13:56:04 -06:00
Bel LaPointe aacc5d2fc6 img clues in http 2023-04-07 13:53:11 -06:00
Bel LaPointe 08abc8b48f run gui 2023-04-07 13:47:33 -06:00
Bel LaPointe c960de86f3 http writable 2023-04-07 13:46:29 -06:00
Bel LaPointe 7a464c2f09 test http POST api/questions/QID/answers 2023-04-07 13:44:42 -06:00
Bel LaPointe c76da12b1a gui done enough 2023-04-07 13:33:09 -06:00
Bel LaPointe ef25a77254 func q redo 2023-04-07 13:25:40 -06:00
Bel LaPointe 80d987b69e ok ui can rotate questions 2023-04-07 13:22:13 -06:00
Bel LaPointe 1816500617 display questions 2023-04-07 13:13:40 -06:00
Bel LaPointe fe6b87563f stub 2023-04-07 13:07:04 -06:00
Bel LaPointe 273b412e30 shuffling 2023-04-07 12:43:53 -06:00
Bel LaPointe da6097c5e2 load all into mem 2023-04-07 12:43:06 -06:00
Bel LaPointe 365a857e2a sessionid 2023-04-07 12:28:36 -06:00
Bel LaPointe 40d5740817 template lite 2023-04-07 12:21:53 -06:00
Bel LaPointe 933f87d11d http root serves public/root.html 2023-04-07 12:19:31 -06:00
Bel LaPointe a8b5942833 http stub 2023-04-07 12:03:33 -06:00
7 changed files with 538 additions and 133 deletions

145
http.go Normal file
View File

@ -0,0 +1,145 @@
package main
import (
"context"
_ "embed"
"encoding/json"
"fmt"
"net/http"
"os"
"os/signal"
"path"
"strings"
"syscall"
"time"
)
type Context struct {
DB DB
User IDU
}
func HTTP(port int, db DB) error {
ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT)
defer can()
foo := func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
httpGUI(w, r)
} else if strings.HasPrefix(r.URL.Path, "/static/") {
httpStatic(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)
}
}
foo = withAuth(foo)
foo = withDB(foo, db)
foo = withCtx(foo, ctx)
go func() {
http.ListenAndServe(fmt.Sprintf(":%d", port), http.HandlerFunc(foo))
}()
<-ctx.Done()
return nil
}
func extract(ctx context.Context) Context {
v := ctx.Value("__context")
v2, _ := v.(Context)
return v2
}
func inject(ctx context.Context, v Context) context.Context {
return context.WithValue(ctx, "__context", v)
}
func withCtx(foo http.HandlerFunc, ctx context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(ctx)
foo(w, r)
}
}
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 {
return func(w http.ResponseWriter, r *http.Request) {
u, _, ok := r.BasicAuth()
if !ok || u == "" {
w.Header().Set("WWW-Authenticate", "Basic")
w.WriteHeader(401)
return
}
c := extract(r.Context())
c.User = IDU(u)
r = r.WithContext(inject(r.Context(), c))
foo(w, r)
}
}
//go:embed public/root.html
var httpGUIHTML string
func httpGUI(w http.ResponseWriter, r *http.Request) {
body := httpGUIHTML
if os.Getenv("DEBUG") != "" {
b, _ := os.ReadFile("public/root.html")
body = string(b)
}
ctx := extract(r.Context())
body = strings.ReplaceAll(body, "{{USER}}", string(ctx.User))
assignments, err := httpAssignments(r.Context())
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))
}
func httpStatic(w http.ResponseWriter, r *http.Request) {
p := strings.TrimPrefix(r.URL.Path, "/static/")
p = strings.ReplaceAll(p, "/..", "/")
p = path.Clean(p)
http.ServeFile(w, r, p)
}
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
}
func httpPostQuestionAnswers(w http.ResponseWriter, r *http.Request) {
idq := IDQ(path.Base(strings.Split(r.URL.Path, "/answers")[0]))
var payload struct {
Answer string `json:"answer"`
Passed bool `json:"passed"`
}
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ctx := extract(r.Context())
if err := ctx.DB.PushAnswer(ctx.User, idq, Renderable(payload.Answer), payload.Passed); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

50
http_test.go Normal file
View File

@ -0,0 +1,50 @@
package main
import (
"net/http"
"net/http/httptest"
"os"
"path"
"strings"
"testing"
"time"
)
func TestHTTPPostQuestionAnswers(t *testing.T) {
p := path.Join(t.TempDir(), "db.yaml")
os.WriteFile(p, []byte("{}"), os.ModePerm)
db, err := newYamlDB(p)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
r := httptest.NewRequest(
http.MethodPost,
"/api/questions/QID/answers",
strings.NewReader(`{"answer":"a", "passed": false}`),
)
r.SetBasicAuth("u", "")
withAuth(withDB(httpPostQuestionAnswers, db))(w, r)
if w.Code != http.StatusOK {
t.Error(w.Code)
}
_, got := db.LastAnswer("u", "QID")
if got == (Answer{}) {
t.Error("no answer pushed:", got)
}
if got.Q != "QID" {
t.Error(got.Q)
} else if got.A != "a" {
t.Error(got.A)
} else if time.Since(time.Unix(0, got.TS)) > time.Minute {
t.Error(got.TS)
} else if got.Author != "u" {
t.Error(got.Author)
} else if got.Pass != false {
t.Error(got.Pass)
}
}

126
main.go
View File

@ -1,11 +1,8 @@
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"strings"
"strconv"
"time"
)
@ -59,115 +56,10 @@ func Main() error {
return err
}
defer db.Close()
user := IDU(os.Getenv("USER"))
failed, err := Review(db, user)
if err != nil {
return err
if port, _ := strconv.Atoi(os.Getenv("PORT")); port > 0 {
return HTTP(port, db)
}
for len(failed) > 0 {
for i := len(failed) - 1; i >= 0; i-- {
if _, passed, err := ReviewQ(db, user, failed[i]); err != nil {
return err
} else if passed == CMD_PASS {
return nil
} else if passed != CMD_FAIL {
failed = append(failed[:i], failed[i+1:]...)
}
}
}
return nil
}
func Review(db DB, user IDU) ([]IDQ, error) {
failed := []IDQ{}
for q, _ := range db.HistoryOf(user) {
if time.Until(db.Next(user, q)) > 0 {
continue
}
response, passed, err := ReviewQ(db, user, q)
if err != nil {
return nil, err
}
switch passed {
case CMD_QUIT:
return nil, nil
case CMD_FAIL:
failed = append(failed, q)
if err := db.PushAnswer(user, q, Renderable(response), false); err != nil {
return nil, err
}
case CMD_PASS:
if err := db.PushAnswer(user, q, Renderable(response), true); err != nil {
return nil, err
}
}
fmt.Println()
}
return failed, nil
}
func ReviewQ(db DB, user IDU, q IDQ) (string, string, error) {
question := db.Question(q)
fmt.Printf("> Q: %s\n", question.Q.ToString(db.UserResolution(user)))
fmt.Printf("\n")
fmt.Printf("> %+v\n", question.Tags)
var response string
gotAClue := false
for i := range question.Clues {
if i == 0 {
fmt.Printf("> /clue for a clue\n")
}
response = readline()
if response != "/clue" {
break
}
gotAClue = true
fmt.Printf("> %s", question.Clues[i].ToString(db.UserResolution(user)))
if i+1 < len(question.Clues) {
fmt.Printf(" | /clue for another clue")
}
fmt.Printf("\n")
}
if len(question.Clues) == 0 || response == "/clue" {
response = readline()
}
if !gotAClue && len(question.Solution) > 0 && question.Solution[0].ToString(1) == response {
return response, CMD_PASS, nil
}
if len(question.Solution) > 0 {
fmt.Printf("> Solution:")
for i := range question.Solution {
fmt.Printf("\n\t%s", question.Solution[i].ToString(db.UserResolution(user)))
}
fmt.Println()
}
if id, _ := db.LastAnswer(user, q); id == "" {
} else if lastAnswer := db.Answer(id); lastAnswer.A != "" {
fmt.Printf("> Last time, you responded:\n\t%s\n", lastAnswer.A)
}
fmt.Printf("> Did you pass this time? [%s%s%s%s]\n",
strings.ToUpper(CMD_PASS),
strings.ToLower(CMD_FAIL),
strings.ToLower(CMD_SKIP),
strings.ToLower(CMD_QUIT),
)
switch strings.ToLower(readline()) {
case CMD_SKIP:
return response, CMD_SKIP, nil
case CMD_FAIL:
return response, CMD_FAIL, nil
case CMD_QUIT:
return response, CMD_QUIT, nil
}
return response, CMD_PASS, nil
}
func readline() string {
fmt.Printf("\t")
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
return strings.TrimSpace(text)
return Terminal(db)
}
func NewDB() (DB, error) {
@ -182,13 +74,3 @@ func (q Question) Tagged(tag IDT) bool {
}
return false
}
func (renderable Renderable) ToString(resolution int) string {
s := string(renderable)
if !strings.HasPrefix(s, "img:") {
return s
}
buff := bytes.NewBuffer(nil)
ViewAt(buff, s[4:], resolution)
return string(buff.Bytes())
}

165
public/root.html Normal file
View File

@ -0,0 +1,165 @@
<!DOCTYPE html>
<html>
<header>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/dark.css">
<script>
let session = Number(
Date.
now().
toString().
split("").
reverse().
join("")
).toString(36)
console.log("session", session)
</script>
</header>
<body>
<!--{{USER}}-->
<form id="flash" action="">
<h1 name="status"></h1>
<input type="text" name="idq" readonly=true value="" style="display: none;">
<div name="question"></div>
<div name="clues"></div>
<div name="solution"></div>
<input type="button" value="clue">
<input type="text" name="answer">
<input type="button" value="pass" onclick="passQuestion(this.parentElement)">
<input type="button" value="fail" onclick="failQuestion(this.parentElement)">
<input type="button" value="skip" onclick="skipQuestion(this.parentElement)">
</form>
</body>
<footer>
<script>
let knowledgebase = {{ASSIGNMENTS_JSON}}
knowledgebase = Object.
keys(knowledgebase).
map((key) => [
[key, knowledgebase[key]]
])
function shuffle(array) {
let currentIndex = array.length, randomIndex;
while (currentIndex != 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
shuffle(knowledgebase)
console.log(0, knowledgebase)
function passQuestion(form) {
if (!form.children.answer.retake) {
pushAnswer(form.children.idq.value, form.children.answer.value, true)
}
nextQuestion(form)
}
function failQuestion(form) {
if (!form.children.answer.retake) {
pushAnswer(form.children.idq.value, form.children.answer.value, false)
}
queueRedoQuestion(form)
nextQuestion(form)
}
function skipQuestion(form) {
queueRedoQuestion(form)
nextQuestion(form)
}
function queueRedoQuestion(form) {
knowledgebase.push([[
form.children.idq.value,
{
Q: form.children.question.value,
Clues: form.children.clues.value,
Solution: form.children.solution.value,
Retake: true,
},
]])
}
function pushAnswer(idq, answer, passed) {
http("post", `/api/questions/${idq}/answers`, noopcallback,
JSON.stringify(
{
"answer": answer,
"passed": new Boolean(passed),
}
),
)
}
function nextQuestion(form) {
form.children.answer.value = ""
let todo = knowledgebase.shift()
if (!todo) {
todo = [0]
}
todo = todo[0]
if (! todo) {
todo = ["", {Q: "ALL DONE"}]
}
form.children.status.innerHTML = knowledgebase.length.toString()
form.children.idq.value = todo[0]
if (todo[1].Q.startsWith("img:")) {
form.children.question.innerHTML = `<img src="static/${todo[1].Q.slice(4, 1000)}"/>`
} else {
form.children.question.innerHTML = `<h3>${todo[1].Q}</h3>`
}
form.children.question.value = todo[1].Q
let clues = ""
for (var i in todo[1].Clues) {
clues += `<details><summary>clue #${i}</summary>`
if (todo[1].Clues[i].startsWith("img:")) {
clues += `<img src="static/${todo[1].Clues[i].slice(4, 1000)}"/>`
} else {
clues += todo[1].Clues[i]
}
clues += `</details>`
}
form.children.clues.innerHTML = clues
form.children.clues.value = todo[1].Clues
let solution = ""
for (var i in todo[1].Solution) {
solution += `<br>${todo[1].Solution[i]}`
}
if (solution) {
solution = `<details><summary>solution</summary>${solution}</details>`
}
form.children.solution.innerHTML = solution
form.children.solution.value = todo[1].Solution
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>
</html>

132
terminal.go Normal file
View File

@ -0,0 +1,132 @@
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"strings"
"time"
)
func Terminal(db DB) error {
user := IDU(os.Getenv("USER"))
failed, err := Review(db, user)
if err != nil {
return err
}
for len(failed) > 0 {
for i := len(failed) - 1; i >= 0; i-- {
if _, passed, err := ReviewQ(db, user, failed[i]); err != nil {
return err
} else if passed == CMD_PASS {
return nil
} else if passed != CMD_FAIL {
failed = append(failed[:i], failed[i+1:]...)
}
}
}
return nil
}
func Review(db DB, user IDU) ([]IDQ, error) {
failed := []IDQ{}
for q, _ := range db.HistoryOf(user) {
if time.Until(db.Next(user, q)) > 0 {
continue
}
response, passed, err := ReviewQ(db, user, q)
if err != nil {
return nil, err
}
switch passed {
case CMD_QUIT:
return nil, nil
case CMD_FAIL:
failed = append(failed, q)
if err := db.PushAnswer(user, q, Renderable(response), false); err != nil {
return nil, err
}
case CMD_PASS:
if err := db.PushAnswer(user, q, Renderable(response), true); err != nil {
return nil, err
}
}
fmt.Println()
}
return failed, nil
}
func ReviewQ(db DB, user IDU, q IDQ) (string, string, error) {
question := db.Question(q)
fmt.Printf("> Q: %s\n", question.Q.ToString(db.UserResolution(user)))
fmt.Printf("\n")
fmt.Printf("> %+v\n", question.Tags)
var response string
gotAClue := false
for i := range question.Clues {
if i == 0 {
fmt.Printf("> /clue for a clue\n")
}
response = readline()
if response != "/clue" {
break
}
gotAClue = true
fmt.Printf("> %s", question.Clues[i].ToString(db.UserResolution(user)))
if i+1 < len(question.Clues) {
fmt.Printf(" | /clue for another clue")
}
fmt.Printf("\n")
}
if len(question.Clues) == 0 || response == "/clue" {
response = readline()
}
if !gotAClue && len(question.Solution) > 0 && question.Solution[0].ToString(1) == response {
return response, CMD_PASS, nil
}
if len(question.Solution) > 0 {
fmt.Printf("> Solution:")
for i := range question.Solution {
fmt.Printf("\n\t%s", question.Solution[i].ToString(db.UserResolution(user)))
}
fmt.Println()
}
if id, _ := db.LastAnswer(user, q); id == "" {
} else if lastAnswer := db.Answer(id); lastAnswer.A != "" {
fmt.Printf("> Last time, you responded:\n\t%s\n", lastAnswer.A)
}
fmt.Printf("> Did you pass this time? [%s%s%s%s]\n",
strings.ToUpper(CMD_PASS),
strings.ToLower(CMD_FAIL),
strings.ToLower(CMD_SKIP),
strings.ToLower(CMD_QUIT),
)
switch strings.ToLower(readline()) {
case CMD_SKIP:
return response, CMD_SKIP, nil
case CMD_FAIL:
return response, CMD_FAIL, nil
case CMD_QUIT:
return response, CMD_QUIT, nil
}
return response, CMD_PASS, nil
}
func readline() string {
fmt.Printf("\t")
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
return strings.TrimSpace(text)
}
func (renderable Renderable) ToString(resolution int) string {
s := string(renderable)
if !strings.HasPrefix(s, "img:") {
return s
}
buff := bytes.NewBuffer(nil)
ViewAt(buff, s[4:], resolution)
return string(buff.Bytes())
}

47
testdata/sample.yaml vendored Normal file → Executable file
View File

@ -1,7 +1,21 @@
knowledge:
questions:
uuid1:
uuid10:
q: whats a fieldset
solution:
- solution A
- solution B
clues:
- clue1 of 2
- img:testdata/tofugu.d/sa-hiragana-0.png
tags:
- ops
- data-platform
uuid11:
q: whats a responseset
solution:
- solution A
- solution B
clues:
- clue1 of 2
- clue2 of 2
@ -9,19 +23,34 @@ knowledge:
- ops
- data-platform
answers:
uuid2:
q: uuid1
4ad47d8f-2e9b-46fe-8a12-67db85efc6f8:
q: uuid11
a: ""
ts: 1680896808930227000
author: breel
pass: false
36648846-4e14-4518-b434-5d4ff3b0127b:
q: uuid10
a: ""
ts: 1680896805896000000
author: breel
pass: false
uuid20:
q: uuid10
a: a schema
ts: 123
author: breel
uuid0:
q: uuid1
pass: false
uuid21:
q: uuid10
a: not a schema
ts: 122
author: breel
pass: false
users:
breel:
history:
uuid1:
- {a: uuid0, pass: true}
- {a: uuid2, pass: true}
tags:
assignments:
- ops
cadence: 5s
resolution: 1

View File

@ -3,9 +3,7 @@ todo:
- exemplary answers; declaring
- exemplary answers; sharing
- tags; declare mastery
- tags; assign to a user
- entrypoint; explicit answer all
- entrypoint; answer tag
- entrypoint; edit question
- entrypoint; delete question
- ascii images from url
@ -31,3 +29,7 @@ done:
ts: Fri Apr 7 10:46:14 MDT 2023
- todo: entrypoint; create question
ts: Fri Apr 7 10:46:14 MDT 2023
- todo: tags; assign to a user
ts: Fri Apr 7 14:06:39 MDT 2023
- todo: entrypoint; answer tag
ts: Fri Apr 7 14:06:39 MDT 2023