diff --git a/config/config.go b/config/config.go new file mode 100644 index 00000000..f6eb5760 --- /dev/null +++ b/config/config.go @@ -0,0 +1,63 @@ +package config + +import ( + "errors" + "github.com/go-ini/ini" +) + +type RedisInfo struct { + Host string `ini:"host"` + Port string `ini:"port"` + User string `ini:"user"` + Password string `ini:"password"` +} + +var redisInfo = &RedisInfo{ + Port: "6379", +} + +type MysqlInfo struct { + Host string `ini:"host"` + Port string `ini:"port"` + Database string `ini:"database"` + User string `ini:"user"` + Password string `ini:"password"` +} + +var mysqlInfo = &MysqlInfo{ + Port: "3306", + Database: "icingadb", +} + +func ParseConfig(path string) error { + cfg, err := ini.Load(path) + + if err := cfg.Section("redis").MapTo(redisInfo); err != nil { + return err + } + + if redisInfo.Host == "" { + return errors.New("config: missing redis host") + } + + if err = cfg.Section("mysql").MapTo(mysqlInfo); err != nil { + return err + } + + if mysqlInfo.Host == "" { + return errors.New("config: missing mysql host") + } + if mysqlInfo.User == "" || mysqlInfo.Password == "" { + return errors.New("config: missing mysql credentials") + } + + return nil +} + +func GetMysqlInfo() *MysqlInfo { + return mysqlInfo +} + +func GetRedisInfo() *RedisInfo { + return redisInfo +} diff --git a/icingadb.ini b/icingadb.ini new file mode 100644 index 00000000..f9fd5793 --- /dev/null +++ b/icingadb.ini @@ -0,0 +1,7 @@ +[redis] +host="127.0.0.1" + +[mysql] +host="127.0.0.1" +user="module-dev" +password="icinga0815!" diff --git a/main.go b/main.go index 5d6195b6..7c6c3a4b 100644 --- a/main.go +++ b/main.go @@ -4,29 +4,44 @@ import ( "git.icinga.com/icingadb/icingadb-connection" "git.icinga.com/icingadb/icingadb-ha" "git.icinga.com/icingadb/icingadb-json-decoder" + "git.icinga.com/icingadb/icingadb-main/config" "git.icinga.com/icingadb/icingadb-main/configobject/host" "git.icinga.com/icingadb/icingadb-main/configobject/sync" "git.icinga.com/icingadb/icingadb-main/supervisor" log "github.com/sirupsen/logrus" ) +type RedisInfo struct { + host string + port int + user string + password string +} + func main() { - redisConn, err := icingadb_connection.NewRDBWrapper("127.0.0.1:6379") + if err := config.ParseConfig("icingadb.ini"); err != nil { + log.Fatal(err) + } + + redisInfo := config.GetRedisInfo() + mysqlInfo := config.GetMysqlInfo() + + redisConn, err := icingadb_connection.NewRDBWrapper(redisInfo.Host + ":" + redisInfo.Port) if err != nil { log.Fatal(err) } - mysqlConn, err := icingadb_connection.NewDBWrapper("module-dev:icinga0815!@tcp(127.0.0.1:3306)/icingadb" ) + mysqlConn, err := icingadb_connection.NewDBWrapper(mysqlInfo.User + ":" + mysqlInfo.Password + "@tcp(" + mysqlInfo.Host + ":" + mysqlInfo.Port + ")/" + mysqlInfo.Database) if err != nil { log.Fatal(err) } super := supervisor.Supervisor{ - ChErr: make (chan error), - ChEnv: make(chan *icingadb_ha.Environment), + ChErr: make(chan error), + ChEnv: make(chan *icingadb_ha.Environment), ChDecode: make(chan *icingadb_json_decoder.JsonDecodePackages), - Rdbw: redisConn, - Dbw: mysqlConn, + Rdbw: redisConn, + Dbw: mysqlConn, } ha := icingadb_ha.HA{} @@ -42,7 +57,7 @@ func main() { super.ChErr <- sync.Operator(&super, chHA, &sync.Context{ ObjectType: "host", - Factory: host.NewHost, + Factory: host.NewHost, InsertStmt: host.BulkInsertStmt, DeleteStmt: host.BulkDeleteStmt, UpdateStmt: host.BulkUpdateStmt, @@ -51,10 +66,10 @@ func main() { for { select { - case err := <- super.ChErr: + case err := <-super.ChErr: if err != nil { log.Fatal(err) } } } -} \ No newline at end of file +}