-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Config:Add getter to make the config globally accessible
- Move it to new package to avoid "import cycle" - Dont pass it as param to functions
- Loading branch information
1 parent
7c5a84d
commit 8db1d1b
Showing
6 changed files
with
94 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package daemonConfig | ||
|
||
import ( | ||
"errors" | ||
"github.com/creasty/defaults" | ||
"github.com/goccy/go-yaml" | ||
icingadbConfig "github.com/icinga/icingadb/pkg/config" | ||
"os" | ||
) | ||
|
||
type ConfigFile struct { | ||
Listen string `yaml:"listen" default:"localhost:5680"` | ||
DebugPassword string `yaml:"debug-password"` | ||
ChannelPluginDir string `yaml:"channel-plugin-dir" default:"/usr/libexec/icinga-notifications/channel"` | ||
Icingaweb2URL string `yaml:"icingaweb2-url"` | ||
Database icingadbConfig.Database `yaml:"database"` | ||
Logging icingadbConfig.Logging `yaml:"logging"` | ||
} | ||
|
||
var config *ConfigFile | ||
|
||
// Load loads the daemon config from given path. Call it only once when starting the daemon. | ||
func Load(path string) error { | ||
if config != nil { | ||
return errors.New("config already set") | ||
} | ||
|
||
cfg, err := fromFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
config = cfg | ||
|
||
return nil | ||
} | ||
|
||
// Config returns the config that was loaded while starting the daemon | ||
func Config() *ConfigFile { | ||
return config | ||
} | ||
|
||
func fromFile(path string) (*ConfigFile, error) { | ||
f, err := os.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer func() { _ = f.Close() }() | ||
|
||
var c ConfigFile | ||
|
||
if err := defaults.Set(&c); err != nil { | ||
return nil, err | ||
} | ||
|
||
d := yaml.NewDecoder(f) | ||
if err := d.Decode(&c); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := c.Validate(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &c, nil | ||
} | ||
|
||
func (c *ConfigFile) Validate() error { | ||
if err := c.Database.Validate(); err != nil { | ||
return err | ||
} | ||
if err := c.Logging.Validate(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters