Commit c1a0a4e

Richard Luby <richluby@gmail.com>
2016-10-25 11:38:57
improved config file handling
added additional configuration options
1 parent cd46518
Changed files (1)
questioner.go
@@ -3,11 +3,12 @@ package main
 
 import (
 	"flag"
-	"fmt"
 	"github.com/BurntSushi/toml"
+	"io"
 	"io/ioutil"
 	"log"
 	"os"
+	"strconv"
 )
 
 // EXIT_CODES define exit error codes
@@ -25,6 +26,8 @@ var EXIT_CODE = EXIT_CODES{
 
 // CONFIG defines default for the server
 type CONFIG struct {
+	// LISTEN_ADDRESS defines the local address on which to listen
+	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
@@ -33,16 +36,17 @@ type CONFIG struct {
 	PERMIT_BLANK_PASSWORD bool
 	// PRIVATE_KEY defines the path to the server's private key for signing https connections
 	PRIVATE_KEY string
+	// USE_HTTPS determines if the server should use HTTPS
+	USE_HTTPS bool
 }
 
-// config contains the configuration for the program
-var config CONFIG
-
 // defaultConfig is used when no configuration file is given
 const defaultConfig = `
-LISTEN_PORT = 8022
+LISTEN_ADDRESS = "127.0.0.1"
+LISTEN_PORT = 80
 MAX_CONNECTIONS = 1500
 PERMIT_BLANK_PASSWORD = true
+USE_HTTPS = false
 PRIVATE_KEY = "~/.ssh/question.priv"
 `
 
@@ -50,6 +54,7 @@ PRIVATE_KEY = "~/.ssh/question.priv"
 // cfgFile : the file that contains the configuration
 func LoadConfiguration(cfgFile string) CONFIG {
 	var contents string
+	var config CONFIG
 	if cfgFile == "" {
 		contents = defaultConfig
 	} else {
@@ -61,7 +66,7 @@ func LoadConfiguration(cfgFile string) CONFIG {
 		contents = string(buffer)
 	}
 	if _, err := toml.Decode(contents, &config); err != nil {
-		log.Fatalf("Could not load configuration: %+v\n", err.Error())
+		log.Fatalf("Could not parse configuration: %+v\n", err.Error())
 		os.Exit(EXIT_CODE.BAD_CONFIG)
 	}
 	return config
@@ -80,9 +85,11 @@ func Listen(serverConfig CONFIG) {
 
 // main handles starting the listening server
 func main() {
-	confFile := flag.String("conf-file", "", "define a specific configuration file to read")
+	var confFile string
+	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)
+	config := LoadConfiguration(confFile)
+	log.Printf("%+v\t%s\n", config, confFile)
 	Listen(config)
-	fmt.Printf("%+v\n", config)
 }