Commit 849bd5b
Changed files (2)
question.go
@@ -10,9 +10,9 @@ import (
)
type Record struct {
- question,
- answer,
- category string
+ Question,
+ Answer,
+ Category string
}
// parseLine parses a csv line and returns a single record
@@ -28,15 +28,15 @@ func parseLine(line string) (Record, error) {
if len(tokens) != 3 {
return record, fmt.Errorf("Unable to parse line for %s", line)
}
- record.question = strings.TrimSpace(tokens[0])
- record.answer = strings.TrimSpace(tokens[1])
- record.category = strings.TrimSpace(tokens[2])
+ record.Question = strings.TrimSpace(tokens[0])
+ record.Answer = strings.TrimSpace(tokens[1])
+ record.Category = strings.TrimSpace(tokens[2])
return record, nil
}
-// loadFile reads the lines of the question directory and returns an array of Records
+// loadFile reads the lines of the Answer directory and returns an array of Records
// The questions are expected in the form of a csv in the order
-// question, answer, category
+// question, Category, category
// Lines starting with '#' are ignored
func LoadFile(file *os.File) ([]Record, error) {
log.Printf("Loading file: %+v", file)
@@ -45,7 +45,7 @@ func LoadFile(file *os.File) ([]Record, error) {
for scanner.Scan() {
if record, err := parseLine(scanner.Text()); err != nil {
log.Printf("Error while parsing line: %+v. \nContinuing execution", err)
- } else if record.question != "" {
+ } else if record.Question != "" {
records = append(records, record)
}
}
questioner.go
@@ -2,15 +2,17 @@
package main
import (
+ "encoding/json"
"flag"
"github.com/BurntSushi/toml"
- "io"
"io/ioutil"
"log"
+ "math/rand"
"net/http"
"os"
"path/filepath"
"strconv"
+ "time"
)
// EXIT_CODES define exit error codes
@@ -80,7 +82,15 @@ func LoadConfiguration(cfgFile string) CONFIG {
// HandleConnections processes client connections
func handleConnections(writer http.ResponseWriter, request *http.Request) {
log.Printf("Client Connected: %+v\tvia %+v", request.RemoteAddr, request.UserAgent())
- io.WriteString(writer, "Welcome to the questioning server.\n")
+ var err error
+ var question []byte
+ for question == nil {
+ question, err = json.Marshal(records[rand.Intn(len(records))])
+ if err != nil {
+ log.Printf("Error selecting question: %+v", err)
+ }
+ }
+ writer.Write(question)
}
// Listen binds the listening server and starts the listening loop
@@ -108,5 +118,6 @@ func main() {
}
log.Printf("Loaded %d questions from %s.", len(records), config.QUESTIONS)
log.Printf("Configuration: %+v\n", config)
+ rand.Seed(time.Now().Unix())
Listen(config)
}