package conf import ( "fmt" "github.com/pelletier/go-toml" "net" "os" "time" ) type conf struct { BindHost string `toml:"bind_host"` BindPort int `toml:"bind_port"` SMTPHost string `toml:"smtp_host"` SMTPPort int `toml:"smtp_port"` SMTPUsername string `toml:"smtp_username"` SMTPPassword string `toml:"smtp_password"` SMTPReceiver string `toml:"smtp_receiver"` CaptchaDirectory string `toml:"captcha_directory"` CaptchaLength int `toml:"captcha_length"` CaptchaWidth int `toml:"captcha_width"` CaptchaHeight int `toml:"captcha_height"` CaptchaExpiration time.Duration `toml:"captcha_expiration"` } var Conf = conf{ BindHost: "127.0.0.1", BindPort: 8000, CaptchaLength: 6, CaptchaWidth: 240, CaptchaHeight: 80, CaptchaExpiration: time.Hour, } func ParseFile(path string) bool { var failure bool // Open config file file, err := os.Open(path) if err != nil { fmt.Fprintf(os.Stderr, "ERROR: cannot open config file %q: %s\n", path, err.Error()) return false } // Read data from config file var data = make([]byte, 10000) n, err := file.Read(data) if err != nil { fmt.Fprintf(os.Stderr, "ERROR: cannot read config file %q: %s\n", path, err.Error()) return false } // Parse toml from config file if err = toml.Unmarshal(data[:n], &Conf); err != nil { fmt.Fprintf(os.Stderr, "ERROR: cannot load toml config from file %q: %s\n", path, err.Error()) return false } // Check arguments in config file if net.ParseIP(Conf.BindHost) == nil { fmt.Fprintf(os.Stderr, "ERROR: bad value for 'bind_host' in config file %q: should be a valid IP address\n", path) failure = true } if Conf.BindPort < 1 || Conf.BindPort > 65535 { fmt.Fprintf(os.Stderr, "ERROR: bad value for 'bind_port' in config file %q: should be an integer in 1-65535\n", path) failure = true } if Conf.SMTPHost == "" { fmt.Fprintf(os.Stderr, "ERROR: missing value for 'smtp_host' in config file %q\n", path) failure = true } if Conf.SMTPPort < 1 || Conf.SMTPPort > 65535 { fmt.Fprintf(os.Stderr, "ERROR: bad value for 'smtp_port' in config file %q: should be an integer in 1-65535\n", path) failure = true } if Conf.SMTPUsername == "" { fmt.Fprintf(os.Stderr, "ERROR: missing value for 'smtp_username' in config file %q\n", path) failure = true } if Conf.SMTPPassword == "" { fmt.Fprintf(os.Stderr, "ERROR: missing value for 'smtp_password' in config file %q\n", path) failure = true } if Conf.SMTPReceiver == "" { fmt.Fprintf(os.Stderr, "ERROR: missing value for 'smtp_receiver' in config file %q\n", path) failure = true } if Conf.CaptchaDirectory == "" { fmt.Fprintf(os.Stderr, "ERROR: missing value for 'captcha_directory' in config file %q\n", path) failure = true } if Conf.CaptchaLength == 0 { fmt.Fprintf(os.Stderr, "ERROR: missing value for 'captcha_length' in config file %q\n", path) failure = true } if Conf.CaptchaWidth == 0 { fmt.Fprintf(os.Stderr, "ERROR: missing value for 'captcha_width' in config file %q\n", path) failure = true } if Conf.CaptchaHeight == 0 { fmt.Fprintf(os.Stderr, "ERROR: missing value for 'captcha_height' in config file %q\n", path) failure = true } return !failure }