Update GET config API when config file not set
This commit is contained in:
+6
-1
@@ -9,11 +9,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func configHandler(w http.ResponseWriter, r *http.Request) {
|
func configHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if app.ConfigPath == "" {
|
||||||
|
http.Error(w, "", http.StatusGone)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "GET":
|
case "GET":
|
||||||
data, err := os.ReadFile(app.ConfigPath)
|
data, err := os.ReadFile(app.ConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.NotFound(w, r)
|
http.Error(w, "", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, err = w.Write(data); err != nil {
|
if _, err = w.Write(data); err != nil {
|
||||||
|
|||||||
+12
-2
@@ -41,7 +41,7 @@
|
|||||||
method: 'POST', body: editor.getValue()
|
method: 'POST', body: editor.getValue()
|
||||||
}).then(r => {
|
}).then(r => {
|
||||||
if (r.ok) {
|
if (r.ok) {
|
||||||
alert("OK");
|
alert('OK');
|
||||||
fetch('api/exit', {method: 'POST'});
|
fetch('api/exit', {method: 'POST'});
|
||||||
} else {
|
} else {
|
||||||
r.text().then(alert);
|
r.text().then(alert);
|
||||||
@@ -50,9 +50,19 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
window.addEventListener('load', () => {
|
window.addEventListener('load', () => {
|
||||||
fetch('api/config').then(r => r.text()).then(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);
|
editor.setValue(data);
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
alert(`Unknown error: ${r.statusText} (${r.status})`);
|
||||||
|
}
|
||||||
|
});
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
Reference in New Issue
Block a user