diff --git a/cmd/server/internal/public/index.html b/cmd/server/internal/public/index.html index c797860..494035e 100644 --- a/cmd/server/internal/public/index.html +++ b/cmd/server/internal/public/index.html @@ -23,8 +23,10 @@ function pollState() { http("GET", "/api/v1/questions", (body) => { + if (!body) + return; g_questions = JSON.parse(body); - console.log("polled state:", body); + console.log("TODO polled state:", body); pollLiveAnswer(); pollLiveQuestion(); }); @@ -35,12 +37,28 @@ if (live_questions) { 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 += `
  • ${i}
  • `; + document.getElementById("question-options").innerHTML = options; + } } function pollLiveAnswer() { if (!g_live_question || !g_live_question.Closed) { + document.getElementById("answers").hidden = true; 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 += `
    ${i}`; + document.getElementById("answers").innerHTML = result; + }); } function http(method, remote, callback, body) { @@ -58,7 +76,11 @@ } pollState(); - setInterval(() => { pollState(); }, 1000); + setInterval(() => { + try { + pollState(); + } catch {} + }, 1000); diff --git a/cmd/server/main.go b/cmd/server/main.go index 012ba9e..6532929 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -17,6 +17,7 @@ import ( "strings" "syscall" + "gitea.inhome.blapointe.com/local/gziphttp" "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 { 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) return nil } @@ -222,7 +227,7 @@ func (h Handler) handleAPIV1QuestionsAnswersPost(session Session, w http.Respons uid := session.User.ID var a Answer 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) } @@ -236,7 +241,7 @@ func (db fsDB) GetQuestion(qid string) (Question, error) { var q Question 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 return q, nil @@ -263,6 +268,9 @@ func (db fsDB) GetQuestions() ([]Question, error) { } results := []Question{} for _, entry := range entries { + if strings.HasPrefix(path.Base(entry.Name()), ".") { + continue + } qid := path.Base(entry.Name()) q, err := db.GetQuestion(qid) if err != nil { @@ -276,18 +284,24 @@ func (db fsDB) GetQuestions() ([]Question, error) { func (db fsDB) GetAnswers(qid string) ([]Answer, error) { p := db.path(qid) + ".d" entries, err := os.ReadDir(p) + if os.IsNotExist(err) { + return nil, nil + } if err != nil { return nil, err } results := []Answer{} for _, entry := range entries { + if strings.HasPrefix(path.Base(entry.Name()), ".") { + continue + } b, err := os.ReadFile(path.Join(p, entry.Name())) if err != nil { return nil, err } var a Answer 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) }