Commit 20c0796

Richard Luby <richluby@gmail.com>
2016-10-26 09:08:48
program now loads questions into memory
program loads questions from the specified file. if a directory is specified, program recursively loads from the files in the directory.
1 parent c067bb0
question.go
@@ -13,7 +13,6 @@ type Record struct {
 	question,
 	answer,
 	category string
-	isInitialized bool
 }
 
 // parseLine parses a csv line and returns a single record
@@ -39,28 +38,40 @@ func parseLine(line string) (Record, error) {
 // The questions are expected in the form of a csv in the order
 // question, answer, category
 // Lines starting with '#' are ignored
-func LoadFile(questionFile string) ([]Record, error) {
-	log.Printf("Loading file: %s", questionFile)
-	file, err := os.Open(questionFile)
-	if err != nil {
-		return nil, err
-	}
-	defer file.Close()
+func LoadFile(file *os.File) ([]Record, error) {
+	log.Printf("Loading file: %+v", file)
 	var records []Record
 	scanner := bufio.NewScanner(file)
 	for scanner.Scan() {
 		if record, err := parseLine(scanner.Text()); err != nil {
-			return nil, err
+			log.Printf("Error while parsing line: %+v. \nContinuing execution", err)
 		} else if record.question != "" {
 			records = append(records, record)
 		}
 	}
-	log.Printf("Loaded %d questions from %s.", 0, questionFile)
-	return nil, fmt.Errorf("Unable to finish readLines for %s", questionFile)
+	log.Printf("Loaded %d questions from %s.", len(records), file.Name())
+	return records, nil
 }
 
-// loadDirectory uses the given directory to load questions from the files therein
-func LoadDirectory(questionDirectory string) ([]Record, error) {
-
-	return nil, fmt.Errorf("Unable to finish loadDirectory for %s", questionDirectory)
+// loadRecords loads the records into memory according to
+// the supplied configuration
+func loadRecords(path string, fileInfo os.FileInfo, err error) error {
+	file, err := os.Open(path)
+	if err != nil {
+		return err
+	}
+	defer file.Close()
+	fileStats, err := file.Stat()
+	if err != nil {
+		return err
+	}
+	var loadedRecords []Record
+	if fileStats.Mode().IsRegular() {
+		loadedRecords, err = LoadFile(file)
+		if err != nil {
+			return err
+		}
+		records = append(records, loadedRecords...)
+	}
+	return nil
 }
questioner.go
@@ -9,6 +9,7 @@ import (
 	"log"
 	"net/http"
 	"os"
+	"path/filepath"
 	"strconv"
 )
 
@@ -31,8 +32,6 @@ type CONFIG struct {
 	LISTEN_ADDRESS string
 	//LISTEN_PORT defines the the port on which to listen
 	LISTEN_PORT int
-	// MAX_CONNECTIONS defines the maximum numbers of users with an active connection
-	MAX_CONNECTIONS int
 	// PERMIT_BLANK_PASSWORD determines if a password should be provided with the user names
 	PERMIT_BLANK_PASSWORD bool
 	// PRIVATE_KEY defines the path to the server's private key for signing https connections
@@ -47,13 +46,15 @@ type CONFIG struct {
 const defaultConfig = `
 LISTEN_ADDRESS = "127.0.0.1"
 LISTEN_PORT = 80
-MAX_CONNECTIONS = 1500
 PERMIT_BLANK_PASSWORD = true
 USE_HTTPS = false
 PRIVATE_KEY = "~/.ssh/question.priv"
 QUESTIONS = "/path/to/questions"
 `
 
+// Contains the questions from which to pull
+var records []Record
+
 //LoadConfiguration loads the configuration file
 // cfgFile : the file that contains the configuration
 func LoadConfiguration(cfgFile string) CONFIG {
@@ -96,10 +97,16 @@ func Listen(serverConfig CONFIG) {
 // the server reads the questions file before establishing a listener
 func main() {
 	var confFile string
+	var err error
 	flag.StringVar(&confFile, "file", "", "define a specific configuration file to read")
 	flag.StringVar(&confFile, "f", "", "define a specific configuration file to read")
 	flag.Parse()
 	config := LoadConfiguration(confFile)
-	log.Printf("Configuration: %+v\t%s\n", config, confFile)
+	if err = filepath.Walk(config.QUESTIONS, loadRecords); err != nil {
+		log.Printf("Failed to load questions due to error: %+v", err)
+		os.Exit(EXIT_CODE.FILE_IO_ERROR)
+	}
+	log.Printf("Loaded %d questions from %s.", len(records), config.QUESTIONS)
+	log.Printf("Configuration: %+v\n", config)
 	Listen(config)
 }