Add sanity checks for configuration and UI to display errors.
This commit is contained in:
parent
1809033049
commit
3e6cf49394
@ -70,7 +70,7 @@ func DefaultConfig() *ConfigV2 {
|
||||
c.WatchInterval = 10
|
||||
c.Port = 9090
|
||||
w := Watcher{
|
||||
WebHookURL: "abcedf",
|
||||
WebHookURL: "https://webhook.url.here",
|
||||
Path: "/your/screenshot/dir/here",
|
||||
Username: "",
|
||||
NoWatermark: false,
|
||||
@ -127,6 +127,28 @@ func (c *ConfigService) Load() error {
|
||||
|
||||
func (c *ConfigService) Save() error {
|
||||
daulog.SendLog("saving configuration", daulog.LogTypeInfo)
|
||||
// sanity checks
|
||||
for _, watcher := range c.Config.Watchers {
|
||||
|
||||
// give the sample one a pass? this is kinda gross...
|
||||
if watcher.Path == "/your/screenshot/dir/here" {
|
||||
continue
|
||||
}
|
||||
info, err := os.Stat(watcher.Path)
|
||||
if os.IsNotExist(err) {
|
||||
return fmt.Errorf("path '%s' does not exist", watcher.Path)
|
||||
}
|
||||
if !info.IsDir() {
|
||||
return fmt.Errorf("path '%s' is not a directory", watcher.Path)
|
||||
}
|
||||
}
|
||||
|
||||
for _, watcher := range c.Config.Watchers {
|
||||
if strings.Index(watcher.WebHookURL, "https://") != 0 {
|
||||
return fmt.Errorf("webhook URL '%s' does not look valid", watcher.WebHookURL)
|
||||
}
|
||||
}
|
||||
|
||||
jsonString, _ := json.Marshal(c.Config)
|
||||
err := ioutil.WriteFile(c.ConfigFilename, jsonString, os.ModePerm)
|
||||
if err != nil {
|
||||
|
@ -3,9 +3,12 @@
|
||||
<main role="main" class="inner DAU" x-data="configuration()" x-init="get_config()">
|
||||
<h1 class="DAU-heading">Config</h1>
|
||||
|
||||
</a>
|
||||
<div x-cloak x-show="error" class="alert alert-danger" role="alert" x-text="error">
|
||||
</div>
|
||||
<div x-cloak x-show="success" class="alert alert-success" role="alert" x-text="success">
|
||||
</div>
|
||||
|
||||
<form class="">
|
||||
<form x-cloak class="">
|
||||
|
||||
<p>Configuration changes are not made until the Save button is pressed
|
||||
at the bottom of this page.
|
||||
@ -138,7 +141,7 @@
|
||||
|
||||
<div class="my-5">
|
||||
<button type="button" class="btn btn-secondary" href="#"
|
||||
@click.prevent="config.Watchers.push({Username: '', WebHookURL: '', Path: '', NoWatermark: false, Exclude: []});">
|
||||
@click.prevent="config.Watchers.push({Username: '', WebHookURL: 'https://webhook.url.here/', Path: '/directory/path/here', NoWatermark: false, Exclude: []});">
|
||||
Add a new watcher</button>
|
||||
</div>
|
||||
|
||||
@ -161,7 +164,7 @@
|
||||
<script>
|
||||
function configuration() {
|
||||
return {
|
||||
config: {},
|
||||
config: {}, error: '', success: '',
|
||||
get_config() {
|
||||
fetch('/rest/config')
|
||||
.then(response => response.json()) // convert to json
|
||||
@ -171,11 +174,18 @@
|
||||
})
|
||||
},
|
||||
save_config() {
|
||||
this.error = '';
|
||||
this.success = '';
|
||||
fetch('/rest/config', { method: 'POST', body: JSON.stringify(this.config) })
|
||||
.then(response => response.json()) // convert to json
|
||||
.then(json => {
|
||||
if (json.error) {
|
||||
this.error = json.error
|
||||
} else {
|
||||
this.success = 'Configuration saved';
|
||||
this.config = json;
|
||||
console.log(json);
|
||||
}
|
||||
window.scrollTo(0,0);
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -108,3 +108,6 @@ pre {
|
||||
.mastfoot {
|
||||
color: rgba(255, 255, 255, .5);
|
||||
}
|
||||
|
||||
/* for alpine.js */
|
||||
[x-cloak] { display: none !important; }
|
@ -108,23 +108,36 @@ func (ws *WebService) getLogs(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (ws *WebService) handleConfig(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
|
||||
type ErrorResponse struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
newConfig := config.ConfigV2{}
|
||||
|
||||
defer r.Body.Close()
|
||||
b, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
w.Write([]byte("bad body"))
|
||||
return
|
||||
}
|
||||
err = json.Unmarshal(b, &newConfig)
|
||||
if err != nil {
|
||||
w.Write([]byte("bad data"))
|
||||
log.Printf("%s", err)
|
||||
w.WriteHeader(400)
|
||||
j, _ := json.Marshal(ErrorResponse{Error: "badly formed JSON"})
|
||||
w.Write(j)
|
||||
return
|
||||
}
|
||||
ws.Config.Config = newConfig
|
||||
ws.Config.Save()
|
||||
err = ws.Config.Save()
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
j, _ := json.Marshal(ErrorResponse{Error: err.Error()})
|
||||
w.Write(j)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
b, _ := json.Marshal(ws.Config.Config)
|
||||
|
@ -78,7 +78,7 @@ func TestGetConfig(t *testing.T) {
|
||||
t.Errorf("expected error to be nil got %v", err)
|
||||
}
|
||||
|
||||
if string(b) != `{"WatchInterval":10,"Version":2,"Port":9090,"Watchers":[{"WebHookURL":"abcedf","Path":"/your/screenshot/dir/here","Username":"","NoWatermark":false,"Exclude":[]}]}` {
|
||||
if string(b) != `{"WatchInterval":10,"Version":2,"Port":9090,"Watchers":[{"WebHookURL":"https://webhook.url.here","Path":"/your/screenshot/dir/here","Username":"","NoWatermark":false,"Exclude":[]}]}` {
|
||||
t.Errorf("Got unexpected response %v", string(b))
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user