Commit 4c90bc8
Changed files (3)
client.go
@@ -40,7 +40,7 @@ func loadClientConfiguration(cfgFile string) CONFIG {
}
// getRecordFromServer retrieves a record from the server
-func getRecordFromServer(client *http.Client, config CONFIG) (Record, error) {
+func getRecordFromServer(client *http.Client, config CONFIG, numQuestions int) ([]Record, error) {
var serverURL string
// allows adding configuration for port specific (ie HTTPS) requests
switch config.PORT {
@@ -49,18 +49,18 @@ func getRecordFromServer(client *http.Client, config CONFIG) (Record, error) {
default:
serverURL = "http://" + config.SERVER + ":" + strconv.Itoa(config.PORT)
}
- resp, err := client.Get(serverURL)
+ resp, err := client.Get(serverURL + "/record?questions=" + strconv.Itoa(numQuestions))
if err != nil {
- return Record{}, err
+ return nil, err
}
defer resp.Body.Close()
- var record Record
+ var recordArray []Record
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
- return Record{}, err
+ return nil, err
}
- err = json.Unmarshal(data, &record)
- return record, err
+ err = json.Unmarshal(data, &recordArray)
+ return recordArray, err
}
// initUserSession starts the interactive prompt for the user
command.go
@@ -85,7 +85,7 @@ func init() {
// executeTest runs a user through a test
func executeTest(args []string) error {
client := &http.Client{}
- var record Record
+ var recordArray []Record
var err error
var input string
questions := 20
@@ -95,12 +95,12 @@ func executeTest(args []string) error {
return fmt.Errorf("Error while converting to number: %+v", err)
}
}
+ recordArray, err = getRecordFromServer(client, config, questions)
+ if err != nil {
+ return fmt.Errorf("Error while request from server: %+v", err)
+ }
reader := bufio.NewReader(os.Stdin)
- for i := 0; i < questions; i += 1 {
- record, err = getRecordFromServer(client, config)
- if err != nil {
- return fmt.Errorf("Error while request from server: %+v", err)
- }
+ for i, record := range recordArray {
fmt.Printf("%d) %s\n", i+1, record.Question)
input, _ = reader.ReadString('\n')
if strings.Compare(strings.TrimSpace(input), record.Answer) == 0 {
questioner.go
@@ -2,7 +2,6 @@
package main
import (
- "encoding/json"
"flag"
"github.com/BurntSushi/toml"
"io/ioutil"
@@ -66,24 +65,10 @@ func loadServerConfiguration(cfgFile string) CONFIG {
return config
}
-// HandleConnections processes client connections
-func handleConnections(writer http.ResponseWriter, request *http.Request) {
- log.Printf("Client Connected: %+v\tvia %+v", request.RemoteAddr, request.UserAgent())
- 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
// serverConfig : contains the configuration to be used
func Listen(serverConfig CONFIG) {
- http.HandleFunc("/", handleConnections)
+ http.HandleFunc("/record", handleRecordRequests)
log.Fatal(http.ListenAndServe(
serverConfig.LISTEN_ADDRESS+":"+
strconv.Itoa(serverConfig.LISTEN_PORT),