-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
88 additions
and
7 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,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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.