workin on drawing

main
Bel LaPointe 2024-02-20 14:26:52 -07:00
parent 2520e8156b
commit f5bd9f27ce
2 changed files with 41 additions and 5 deletions

View File

@ -23,8 +23,10 @@
function pollState() { function pollState() {
http("GET", "/api/v1/questions", (body) => { http("GET", "/api/v1/questions", (body) => {
if (!body)
return;
g_questions = JSON.parse(body); g_questions = JSON.parse(body);
console.log("polled state:", body); console.log("TODO polled state:", body);
pollLiveAnswer(); pollLiveAnswer();
pollLiveQuestion(); pollLiveQuestion();
}); });
@ -35,12 +37,28 @@
if (live_questions) { if (live_questions) {
g_live_question = live_questions[0]; g_live_question = live_questions[0];
} }
if (g_live_question) {
document.getElementById("question-text").innerHTML = g_live_question.Text;
let options = "";
for (let i of g_live_question.Options)
options += `<li>${i}</li>`;
document.getElementById("question-options").innerHTML = options;
}
} }
function pollLiveAnswer() { function pollLiveAnswer() {
if (!g_live_question || !g_live_question.Closed) { if (!g_live_question || !g_live_question.Closed) {
document.getElementById("answers").hidden = true;
return; return;
} }
document.getElementById("answers").hidden = false;
http("GET", `/api/v1/questions/${g_live_question.ID}/answers`, (body) => {
let answers = JSON.parse(body);
let result = "";
for (let i of answers)
result += `<br>${i}`;
document.getElementById("answers").innerHTML = result;
});
} }
function http(method, remote, callback, body) { function http(method, remote, callback, body) {
@ -58,7 +76,11 @@
} }
pollState(); pollState();
setInterval(() => { pollState(); }, 1000); setInterval(() => {
try {
pollState();
} catch {}
}, 1000);
</script> </script>
</footer> </footer>
</html> </html>

View File

@ -17,6 +17,7 @@ import (
"strings" "strings"
"syscall" "syscall"
"gitea.inhome.blapointe.com/local/gziphttp"
"golang.org/x/time/rate" "golang.org/x/time/rate"
) )
@ -162,6 +163,10 @@ var public = func() http.FileSystem {
func (h Handler) handle(session Session, w http.ResponseWriter, r *http.Request) error { func (h Handler) handle(session Session, w http.ResponseWriter, r *http.Request) error {
if !strings.HasPrefix(r.URL.Path, "/api/") { if !strings.HasPrefix(r.URL.Path, "/api/") {
if gziphttp.Can(r) {
w = gziphttp.New(w)
}
w.Header().Set("Cache-Control", "private, max-age=60")
http.FileServer(public).ServeHTTP(w, r) http.FileServer(public).ServeHTTP(w, r)
return nil return nil
} }
@ -222,7 +227,7 @@ func (h Handler) handleAPIV1QuestionsAnswersPost(session Session, w http.Respons
uid := session.User.ID uid := session.User.ID
var a Answer var a Answer
if err := json.NewDecoder(r.Body).Decode(&a); err != nil { if err := json.NewDecoder(r.Body).Decode(&a); err != nil {
return err return fmt.Errorf("failed to read answer: %w", err)
} }
return h.db.InsertAnswer(qid, uid, a) return h.db.InsertAnswer(qid, uid, a)
} }
@ -236,7 +241,7 @@ func (db fsDB) GetQuestion(qid string) (Question, error) {
var q Question var q Question
if err := json.Unmarshal(b, &q); err != nil { if err := json.Unmarshal(b, &q); err != nil {
return Question{}, err return Question{}, fmt.Errorf("failed to parse %s as question: %w", b, err)
} }
q.ID = qid q.ID = qid
return q, nil return q, nil
@ -263,6 +268,9 @@ func (db fsDB) GetQuestions() ([]Question, error) {
} }
results := []Question{} results := []Question{}
for _, entry := range entries { for _, entry := range entries {
if strings.HasPrefix(path.Base(entry.Name()), ".") {
continue
}
qid := path.Base(entry.Name()) qid := path.Base(entry.Name())
q, err := db.GetQuestion(qid) q, err := db.GetQuestion(qid)
if err != nil { if err != nil {
@ -276,18 +284,24 @@ func (db fsDB) GetQuestions() ([]Question, error) {
func (db fsDB) GetAnswers(qid string) ([]Answer, error) { func (db fsDB) GetAnswers(qid string) ([]Answer, error) {
p := db.path(qid) + ".d" p := db.path(qid) + ".d"
entries, err := os.ReadDir(p) entries, err := os.ReadDir(p)
if os.IsNotExist(err) {
return nil, nil
}
if err != nil { if err != nil {
return nil, err return nil, err
} }
results := []Answer{} results := []Answer{}
for _, entry := range entries { for _, entry := range entries {
if strings.HasPrefix(path.Base(entry.Name()), ".") {
continue
}
b, err := os.ReadFile(path.Join(p, entry.Name())) b, err := os.ReadFile(path.Join(p, entry.Name()))
if err != nil { if err != nil {
return nil, err return nil, err
} }
var a Answer var a Answer
if err := json.Unmarshal(b, &a); err != nil { if err := json.Unmarshal(b, &a); err != nil {
return nil, err return nil, fmt.Errorf("failed to parse %s as answer: %w", path.Join(p, entry.Name()), err)
} }
results = append(results, a) results = append(results, a)
} }