From e1f5afa788262a675a03a536d0019ddfb74da430 Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Sun, 31 Jan 2021 18:48:48 +1030 Subject: [PATCH] go fmt --- dau.go | 9 +++--- web/server.go | 78 +++++++++++++++++++++++++-------------------------- 2 files changed, 42 insertions(+), 45 deletions(-) diff --git a/dau.go b/dau.go index f29546c..d2fecae 100644 --- a/dau.go +++ b/dau.go @@ -27,14 +27,13 @@ import ( // "github.com/skratchdot/open-golang/open" "golang.org/x/image/font/inconsolata" - "github.com/tardisx/discord-auto-upload/web" "github.com/tardisx/discord-auto-upload/config" + "github.com/tardisx/discord-auto-upload/web" ) -var lastCheck = time.Now() +var lastCheck = time.Now() var newLastCheck = time.Now() - func main() { parseOptions() @@ -55,7 +54,7 @@ func main() { log.Fatal("could not watch path", err) } lastCheck = newLastCheck - log.Print("sleeping before next check"); + log.Print("sleeping before next check") time.Sleep(time.Duration(config.Config.Watch) * time.Second) } } @@ -235,7 +234,7 @@ func processFile(file string) { log.Print("Bad response from server:", resp.StatusCode) if b, err := ioutil.ReadAll(resp.Body); err == nil { log.Print("Body:", string(b)) - } + } retriesRemaining-- sleepForRetries(retriesRemaining) continue diff --git a/web/server.go b/web/server.go index fb8d147..f6ae23c 100644 --- a/web/server.go +++ b/web/server.go @@ -3,17 +3,16 @@ package web import ( "encoding/json" "fmt" + "github.com/tardisx/discord-auto-upload/assets" "log" "net/http" - "github.com/tardisx/discord-auto-upload/assets" -// "strings" - "regexp" + // "strings" "github.com/tardisx/discord-auto-upload/config" + "mime" "os" - "path/filepath" - "mime" - "text/template" - + "path/filepath" + "regexp" + "text/template" ) // DAUWebServer - stuff for the web server @@ -22,12 +21,12 @@ type DAUWebServer struct { } type valueStringResponse struct { - Success bool `json: 'success'` + Success bool `json: 'success'` Value string `json: 'value'` } type errorResponse struct { - Success bool `json: 'success'` + Success bool `json: 'success'` Error string `json: 'error'` } @@ -39,7 +38,7 @@ func getStatic(w http.ResponseWriter, r *http.Request) { sanitized_path := re.ReplaceAll([]byte(path), []byte("_")) if string(sanitized_path) == "" { - sanitized_path = []byte("index.html"); + sanitized_path = []byte("index.html") } data, err := assets.Asset(string(sanitized_path)) @@ -48,51 +47,50 @@ func getStatic(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, err) } - - extension := filepath.Ext(string(sanitized_path)) + extension := filepath.Ext(string(sanitized_path)) // is this a HTML file? if so wrap it in the template if extension == ".html" { wrapper, _ := assets.Asset("wrapper.tmpl") t := template.Must(template.New("wrapper").Parse(string(wrapper))) var b struct { - Body string - Path string + Body string + Path string Version string } b.Body = string(data) b.Path = string(sanitized_path) b.Version = config.CurrentVersion - t.Execute(w, b) + t.Execute(w, b) return } // otherwise we are a static thing - w.Header().Set("Content-Type", mime.TypeByExtension(extension)) + w.Header().Set("Content-Type", mime.TypeByExtension(extension)) w.Write(data) -// + // } // TODO there should be locks around all these config accesses func getSetWebhook(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - if r.Method == "GET" { - getResponse := valueStringResponse{ Success: true, Value: config.Config.WebHookURL } + if r.Method == "GET" { + getResponse := valueStringResponse{Success: true, Value: config.Config.WebHookURL} // I can't see any way this will fail - js, _ := json.Marshal(getResponse) + js, _ := json.Marshal(getResponse) w.Write(js) - } else if r.Method == "POST" { + } else if r.Method == "POST" { err := r.ParseForm() - if err != nil { - log.Fatal(err) - } + if err != nil { + log.Fatal(err) + } config.Config.WebHookURL = r.PostForm.Get("value") - postResponse := valueStringResponse{ Success: true, Value: config.Config.WebHookURL } + postResponse := valueStringResponse{Success: true, Value: config.Config.WebHookURL} - js, _ := json.Marshal(postResponse) + js, _ := json.Marshal(postResponse) w.Write(js) } } @@ -101,39 +99,39 @@ func getSetDirectory(w http.ResponseWriter, r *http.Request) { log.Print("ok") w.Header().Set("Content-Type", "application/json") - if r.Method == "GET" { - getResponse := valueStringResponse{ Success: true, Value: config.Config.Path } + if r.Method == "GET" { + getResponse := valueStringResponse{Success: true, Value: config.Config.Path} // I can't see any way this will fail - js, _ := json.Marshal(getResponse) + js, _ := json.Marshal(getResponse) w.Write(js) - } else if r.Method == "POST" { + } else if r.Method == "POST" { err := r.ParseForm() - if err != nil { - log.Fatal(err) - } + if err != nil { + log.Fatal(err) + } newPath := r.PostForm.Get("value") // sanity check this path stat, err := os.Stat(newPath) if os.IsNotExist(err) { // not exist - response := errorResponse{ Success: false, Error: fmt.Sprintf("Path: %s - does not exist", newPath) } + response := errorResponse{Success: false, Error: fmt.Sprintf("Path: %s - does not exist", newPath)} js, _ := json.Marshal(response) w.Write(js) - return; - } else if ! stat.IsDir() { + return + } else if !stat.IsDir() { // not a directory - response := errorResponse{ Success: false, Error: fmt.Sprintf("Path: %s - is not a directory", newPath) } + response := errorResponse{Success: false, Error: fmt.Sprintf("Path: %s - is not a directory", newPath)} js, _ := json.Marshal(response) w.Write(js) - return; + return } config.Config.Path = newPath - postResponse := valueStringResponse{ Success: true, Value: config.Config.Path } + postResponse := valueStringResponse{Success: true, Value: config.Config.Path} - js, _ := json.Marshal(postResponse) + js, _ := json.Marshal(postResponse) w.Write(js) } }