mirror of
https://github.com/Icinga/icingadb.git
synced 2026-05-28 04:35:54 -04:00
Add config parser
This commit is contained in:
parent
05d5e97dd5
commit
e1319b5acd
3 changed files with 94 additions and 9 deletions
63
config/config.go
Normal file
63
config/config.go
Normal file
|
|
@ -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
|
||||
}
|
||||
7
icingadb.ini
Normal file
7
icingadb.ini
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
[redis]
|
||||
host="127.0.0.1"
|
||||
|
||||
[mysql]
|
||||
host="127.0.0.1"
|
||||
user="module-dev"
|
||||
password="icinga0815!"
|
||||
33
main.go
33
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue