// ParseConfigParams parses module specific config file parameters func ParseConfigParams(configPath string, section string, params *gwp_context.ModParams) error { // config file must parse successfully c, err := goconf.ReadConfigFile(configPath) if err != nil { return err } // check if we have this section present haveSection := false if ok := c.HasSection(section); ok { haveSection = true } // go through params for _, p := range *params { if p == nil { continue } // check if we have this param defined in config file if !haveSection { // check if this is must-have param if p.Must { return errors.New("Config file error, mandatory parameter " + p.Name + " is missing.") } // assign default value p.Value = p.Default continue } // we have the section // lets see if we can get the value var val interface{} switch p.Type { case gwp_context.TypeInt: val, err = c.GetInt(section, p.Name) case gwp_context.TypeStr: val, err = c.GetString(section, p.Name) case gwp_context.TypeBool: val, err = c.GetBool(section, p.Name) case gwp_context.TypeFloat64: val, err = c.GetFloat64(section, p.Name) default: return errors.New("Invalid parameter type") } if err != nil { if p.Must { return errors.New("Config file error, " + err.Error()) } // assign default value p.Value = p.Default continue } p.Value = val } return nil }
// ParseConfig parses the configuration file and does meaningful checks on defined parameters. // If optional parameters are not met, it sets default values. // It parses only [default] and [project] sections. func ParseConfig(configPath string) (*gwp_context.AppConfig, error) { ac := gwp_context.NewAppConfig() // config file must parse successfully c, err := goconf.ReadConfigFile(configPath) if err != nil { return nil, err } // read params from [default] section conf_addr, err := c.GetString("default", "listen") if err != nil { conf_addr = dflt_conf_addr } conf_mux, err := c.GetBool("default", "gorilla-mux") if err != nil { conf_mux = dflt_conf_mux } // read params from [project] section conf_root, err := c.GetString("project", "root") if err != nil { return nil, err } if !strings.HasSuffix(conf_root, "/") { conf_root += "/" } conf_tmpdir, err := c.GetString("project", "tmpDir") if err != nil { conf_tmpdir = dflt_conf_tmpdir } if !strings.HasSuffix(conf_tmpdir, "/") { conf_tmpdir += "/" } conf_template_path, err := c.GetString("project", "templatePath") if err != nil { conf_template_path = conf_root + "templates/" } if !strings.HasSuffix(conf_template_path, "/") { conf_template_path += "/" } conf_livetpl, err := c.GetBool("project", "live-templates") if err != nil { conf_livetpl = dflt_conf_livetpl } testpath := conf_tmpdir + "go-webproject_tmptest" if err := os.Mkdir(testpath, 0755); err != nil { return nil, errors.New("Error with tmp dir configuration: " + err.Error()) } else { os.Remove(testpath) } p := strings.TrimSpace(conf_template_path) // check if path exists if _, err := os.Stat(p); err != nil { return nil, errors.New("Configuration error, template directory does not exist: " + conf_template_path) } ac.ListenAddr = conf_addr if conf_mux { ac.Mux = "gorilla" } else { ac.Mux = "default" } ac.ProjectRoot = conf_root ac.TempDir = conf_tmpdir ac.TemplatePath = conf_template_path ac.LiveTemplates = conf_livetpl return ac, nil }