From 6e493522c8f47468baffb562ed445091b1b98a42 Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Sat, 6 Feb 2021 12:50:44 +1030 Subject: [PATCH] Add username and watch period web configuration --- data/config.html | 30 +++++++++++++++++++++ web/server.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/data/config.html b/data/config.html index 68cfd05..fe485c4 100644 --- a/data/config.html +++ b/data/config.html @@ -18,6 +18,21 @@ +
+
+
+ Bot username (optional) +
+
+ + +
+
+ +
+
+
+
@@ -33,6 +48,21 @@
+
+
+
+ Period between filesystem checks +
+
+ + +
+
+ +
+
+
+

Learn more

diff --git a/web/server.go b/web/server.go index f6ae23c..551b773 100644 --- a/web/server.go +++ b/web/server.go @@ -6,7 +6,7 @@ import ( "github.com/tardisx/discord-auto-upload/assets" "log" "net/http" - // "strings" + "strconv" "github.com/tardisx/discord-auto-upload/config" "mime" "os" @@ -95,6 +95,70 @@ func getSetWebhook(w http.ResponseWriter, r *http.Request) { } } +// TODO there should be locks around all these config accesses +func getSetUsername(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + + if r.Method == "GET" { + getResponse := valueStringResponse{Success: true, Value: config.Config.Username} + + // I can't see any way this will fail + js, _ := json.Marshal(getResponse) + w.Write(js) + } else if r.Method == "POST" { + err := r.ParseForm() + if err != nil { + log.Fatal(err) + } + config.Config.Username = r.PostForm.Get("value") + postResponse := valueStringResponse{Success: true, Value: config.Config.Username} + + js, _ := json.Marshal(postResponse) + w.Write(js) + } +} + + +func getSetWatch(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + + if r.Method == "GET" { + getResponse := valueStringResponse{Success: true, Value: strconv.Itoa(config.Config.Watch)} + + // I can't see any way this will fail + js, _ := json.Marshal(getResponse) + w.Write(js) + } else if r.Method == "POST" { + err := r.ParseForm() + if err != nil { + log.Fatal(err) + } + + i, err := strconv.Atoi(r.PostForm.Get("value")) + + if err != nil { + response := errorResponse{Success: false, Error: fmt.Sprintf("Bad value for watch: %v", err)} + js, _ := json.Marshal(response) + w.Write(js) + return + } + + if i < 1 { + response := errorResponse{Success: false, Error: "must be > 0"} + js, _ := json.Marshal(response) + w.Write(js) + return + } + + + config.Config.Watch = i + postResponse := valueStringResponse{Success: true, Value: strconv.Itoa(config.Config.Watch)} + + js, _ := json.Marshal(postResponse) + w.Write(js) + } +} + func getSetDirectory(w http.ResponseWriter, r *http.Request) { log.Print("ok") w.Header().Set("Content-Type", "application/json") @@ -139,6 +203,8 @@ func getSetDirectory(w http.ResponseWriter, r *http.Request) { func StartWebServer() { http.HandleFunc("/", getStatic) http.HandleFunc("/rest/config/webhook", getSetWebhook) + http.HandleFunc("/rest/config/username", getSetUsername) + http.HandleFunc("/rest/config/watch", getSetWatch) http.HandleFunc("/rest/config/directory", getSetDirectory) log.Print("Starting web server on http://localhost:9090")