diff --git a/cmd/server/internal/public/index.html b/cmd/server/internal/public/index.html index 8279ac0..60251c1 100644 --- a/cmd/server/internal/public/index.html +++ b/cmd/server/internal/public/index.html @@ -7,5 +7,48 @@

Hello World

diff --git a/cmd/server/main.go b/cmd/server/main.go index 344e3de..012ba9e 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -33,7 +33,7 @@ type Handler struct { } type DB interface { - GetQuestions() ([]string, error) + GetQuestions() ([]Question, error) GetQuestion(string) (Question, error) InsertAnswer(string, string, Answer) error GetAnswers(string) ([]Answer, error) @@ -42,6 +42,9 @@ type DB interface { type fsDB string type Question struct { + ID string + Live bool + Closed bool Text string Options []string } @@ -79,6 +82,7 @@ func newConfig() (Config, error) { fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError) fs.StringVar(&cfg.Addr, "addr", ":8080", "address to listen on") fs.IntVar(&cfg.RPS, "rps", 100, "requests per second to serve") + fs.StringVar(&cfg.fsDB, "fs-db", "/tmp/live-audience.d", "api dir to serve") err := fs.Parse(os.Args[1:]) return cfg, err @@ -234,6 +238,7 @@ func (db fsDB) GetQuestion(qid string) (Question, error) { if err := json.Unmarshal(b, &q); err != nil { return Question{}, err } + q.ID = qid return q, nil } @@ -250,15 +255,20 @@ func (db fsDB) InsertAnswer(qid, uid string, a Answer) error { return os.WriteFile(p, b, os.ModePerm) } -func (db fsDB) GetQuestions() ([]string, error) { +func (db fsDB) GetQuestions() ([]Question, error) { p := db.path("") entries, err := os.ReadDir(p) if err != nil { return nil, err } - results := []string{} + results := []Question{} for _, entry := range entries { - results = append(results, path.Base(entry.Name())) + qid := path.Base(entry.Name()) + q, err := db.GetQuestion(qid) + if err != nil { + return nil, err + } + results = append(results, q) } return results, nil } diff --git a/cmd/server/main_test.go b/cmd/server/main_test.go index 93ea7c6..abbc35c 100644 --- a/cmd/server/main_test.go +++ b/cmd/server/main_test.go @@ -79,10 +79,10 @@ func TestRunHTTP(t *testing.T) { if w.Code != http.StatusOK { t.Error(w.Code) } - var result []string + var result []Question if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil { t.Error(err) - } else if fmt.Sprint(result) != fmt.Sprint([]string{"0"}) { + } else if fmt.Sprint(result) != fmt.Sprint([]Question{{ID: "0", Live: false, Closed: false, Text: "QUESTION TEXT", Options: []string{"X", "Y"}}}) { t.Error(result) } }) @@ -100,7 +100,7 @@ func TestRunHTTP(t *testing.T) { var result Question if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil { t.Error(err) - } else if fmt.Sprint(result) != fmt.Sprint(Question{Text: "QUESTION TEXT", Options: []string{"X", "Y"}}) { + } else if fmt.Sprint(result) != fmt.Sprint(Question{ID: "0", Text: "QUESTION TEXT", Options: []string{"X", "Y"}}) { t.Error(result) } })