Skip to content

Commit

Permalink
Load config file via a URL (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonog authored Oct 9, 2017
1 parent 28a9774 commit b487901
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 7 deletions.
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ func Execute() {

var cfgFile string
var cfgDb string
var cfgURL string
var webPort int
var rpcPort int
var disableBrand bool

func init() {
RootCmd.PersistentFlags().StringVarP(&cfgFile, "config-file", "f", "config.json", "config file")
RootCmd.PersistentFlags().StringVarP(&cfgDb, "config-db", "d", "", "config database url")
RootCmd.PersistentFlags().StringVarP(&cfgURL, "config-url", "u", "", "config url")
RootCmd.PersistentFlags().IntVarP(&webPort, "port", "p", 8888, "port to run web server")
RootCmd.PersistentFlags().IntVarP(&rpcPort, "rpc-port", "r", 8889, "port to run RPC server")
}
14 changes: 9 additions & 5 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,28 @@ var serverCmd = &cobra.Command{
Short: "Run checks and server stats",
Long: "Run checks and server stats",
Run: func(cmd *cobra.Command, args []string) {
if cmd.Flag("config-db").Changed && cmd.Flag("config-file").Changed {
log.Fatal("Please specify only one config source")
}
var configStore config.Store
var err error
if cmd.Flag("config-db").Changed {
log.Println("Config via db")
configDb := cmd.Flag("config-db").Value.String()
configStore, err = config.NewDBStore(configDb)
if err != nil {
log.Fatal("Unable to initialise db via :", configDb, " Error: ", err)
log.Fatal("DB config error via :", configDb, " Error: ", err)
}
} else if cmd.Flag("config-url").Changed {
log.Println("Config via URL")
configURL := cmd.Flag("config-url").Value.String()
configStore, err = config.NewURLStore(configURL)
if err != nil {
log.Fatal("URL config error via :", configURL, " Error: ", err)
}
} else {
log.Println("Config via file")
configFile := cmd.Flag("config-file").Value.String()
configStore, err = config.NewFileStore(configFile)
if err != nil {
log.Fatal("Missing or invalid format: ", configFile)
log.Fatal("File config error via :", configFile, " Error: ", err)
}
}
runServer(configStore, serverConfig{
Expand Down
76 changes: 76 additions & 0 deletions config/url_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package config

import (
"encoding/json"
"io/ioutil"
"net/http"

"github.com/jonog/redalert/checks"
"github.com/jonog/redalert/notifiers"
)

type URLStore struct {
URL string
data URLStoreData
}

type URLStoreData struct {
Checks []checks.Config `json:"checks"`
Notifications []notifiers.Config `json:"notifications"`
Preferences Preferences `json:"preferences"`
}

func NewURLStore(URL string) (*URLStore, error) {
config := &URLStore{URL: URL}
err := config.read()
if err != nil {
return nil, err
}

// create check ID if not present
for i := range config.data.Checks {
if config.data.Checks[i].ID == "" {
config.data.Checks[i].ID = generateID(8)
}
}

// create notification ID if not present
for i := range config.data.Notifications {
if config.data.Notifications[i].ID == "" {
config.data.Notifications[i].ID = generateID(8)
}
}

return config, nil
}

func (u *URLStore) read() error {
resp, err := http.Get(u.URL)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
var data URLStoreData
err = json.Unmarshal(body, &data)
if err != nil {
return err
}
u.data = data
return nil
}

func (u *URLStore) Notifications() ([]notifiers.Config, error) {
return u.data.Notifications, nil
}

func (u *URLStore) Checks() ([]checks.Config, error) {
return u.data.Checks, nil
}

func (u *URLStore) Preferences() (Preferences, error) {
return u.data.Preferences, nil
}
3 changes: 1 addition & 2 deletions servicepb/service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b487901

Please sign in to comment.