// take last value in case of duplicates func (s *server) configFromForm(cf *cfg.Config, form url.Values) error { for k, vs := range form { if strings.HasPrefix(k, "config.") || strings.HasPrefix(k, "source.") { cf.Put(k, vs[len(vs)-1]) } } return nil }
func (s *server) getFromConf(cf *cfg.Config, key string) (string, error) { if v, ok := cf.Get(key); ok { if v == "" { return "", fmt.Errorf("required parameter %s is empty", key) } return v, nil } return "", fmt.Errorf("required parameter %s not found", key) }
func newDategen(c *cfg.Config) *dategen { d := &dategen{ ch: make(chan RawEntry), date: time.Now().UTC().Format("20060102150405"), zone: c.GetVal("dns.zone", "lan"), } go d.run() return d }
func newStaticgen(c *cfg.Config) (*staticgen, error) { key, ok := c.Get("config.key") if !ok { return nil, errors.New("key not specified") } val, ok := c.Get("config.val") if !ok { return nil, errors.New("val not specified") } s := &staticgen{ ch: make(chan RawEntry), } go s.run(key, val) return s, nil }
func newMysql(c *cfg.Config) (*mysql, error) { usr, ok := c.Get("config.user") if !ok { return nil, errors.New("mysql user not specified") } pwd, ok := c.Get("config.password") if !ok { return nil, errors.New("mysql password not specified") } dbname, ok := c.Get("config.database") if !ok { return nil, errors.New("mysql database name not specified") } query, ok := c.Get("config.query") if !ok { return nil, errors.New("mysql query not specified") } host := c.GetVal("config.host", "localhost") port := c.GetVal("config.port", "3306") dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", usr, pwd, host, port, dbname) db, err := sql.Open("mysql", dsn) if err != nil { return nil, fmt.Errorf("failed to connect to mysql[%s]: %s", dsn, err) } rows, err := db.Query(query) if err != nil { return nil, fmt.Errorf("failed to execute query on mysql[%s]: %s", dsn, err) } m := &mysql{ ch: make(chan RawEntry, 100), rows: rows, } // rows is closed in run() go func() { m.run() db.Close() }() return m, nil }
func (s *server) configFromJSON(cf *cfg.Config, r io.Reader) error { if err := cf.FromJSON(r); err != nil { return fmt.Errorf("cannot parse JSON: %s", err) } return nil }