Beispiel #1
0
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)
}
Beispiel #2
0
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
}
Beispiel #3
0
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
}