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 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 }