Update GET config API when config file not set

This commit is contained in:
Alexey Khit
2023-01-18 10:00:20 +03:00
parent 6a61b5234e
commit 6e49d51c33
2 changed files with 19 additions and 4 deletions
+6 -1
View File
@@ -9,11 +9,16 @@ import (
)
func configHandler(w http.ResponseWriter, r *http.Request) {
if app.ConfigPath == "" {
http.Error(w, "", http.StatusGone)
return
}
switch r.Method {
case "GET":
data, err := os.ReadFile(app.ConfigPath)
if err != nil {
http.NotFound(w, r)
http.Error(w, "", http.StatusNotFound)
return
}
if _, err = w.Write(data); err != nil {
+13 -3
View File
@@ -41,7 +41,7 @@
method: 'POST', body: editor.getValue()
}).then(r => {
if (r.ok) {
alert("OK");
alert('OK');
fetch('api/exit', {method: 'POST'});
} else {
r.text().then(alert);
@@ -50,8 +50,18 @@
});
window.addEventListener('load', () => {
fetch('api/config').then(r => r.text()).then(data => {
editor.setValue(data);
fetch('api/config', {cache: 'no-cache'}).then(r => {
if (r.status === 410) {
alert('Config file is not set');
} else if (r.status === 404) {
editor.setValue(''); // config file not exist
} else if (r.ok) {
r.text().then(data => {
editor.setValue(data);
});
} else {
alert(`Unknown error: ${r.statusText} (${r.status})`);
}
});
})
</script>