gr
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user