Add sanity checks for configuration and UI to display errors.
This commit is contained in:
@@ -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 => {
|
||||
this.config = json;
|
||||
console.log(json);
|
||||
if (json.error) {
|
||||
this.error = json.error
|
||||
} else {
|
||||
this.success = 'Configuration saved';
|
||||
this.config = 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))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user