From 5d91288992bed9505571cca4935cc7c5b2f7c2f4 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Tue, 16 Jul 2019 11:29:06 -0600 Subject: [PATCH] k --- main.go | 51 +++++++++++++++++++++++++++++++++++++++++------ public/csv | 4 ++++ public/index.html | 3 ++- 3 files changed, 51 insertions(+), 7 deletions(-) create mode 100755 public/csv diff --git a/main.go b/main.go index 5323f01..a7ca629 100644 --- a/main.go +++ b/main.go @@ -9,10 +9,15 @@ listing file. package main import ( + "context" + "io/ioutil" "local/args" "log" "net/http" + "os" + "path" "strings" + "time" ) func main() { @@ -24,11 +29,45 @@ func main() { } d := fs.Get("d").GetString() - p := strings.TrimPrefix(fs.Get("p").GetString(), ":") + p := fs.Get("p").GetString() + go serve(d, p) + go poll(path.Join(d, "csv"), time.Minute) - http.Handle("/", http.FileServer(http.Dir(d))) - - log.Printf("Serving %s on HTTP port: %s\n", d, p) - - log.Fatal(http.ListenAndServe(":"+p, nil)) + <-context.Background().Done() +} + +func serve(directory, port string) { + port = strings.TrimPrefix(port, ":") + http.Handle("/", http.FileServer(http.Dir(directory))) + log.Printf("Serving %s on HTTP port: %s\n", directory, port) + log.Fatal(http.ListenAndServe(":"+port, nil)) +} + +func poll(path string, interval time.Duration) { + for { + select { + case <-context.Background().Done(): + case <-time.After(interval): + if err := refresh(path); err != nil { + log.Println(err) + } + } + } +} + +func refresh(path string) error { + url := "https://docs.google.com/spreadsheets/d/14F7zRrwGT6JkCeZqZLPM2r2GScrm9FLFxlPtDINbNWg/gviz/tq?tqx=out:csv" + resp, err := http.Get(url) + if err != nil { + return err + } + defer resp.Body.Close() + b, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + if err := ioutil.WriteFile(path, b, os.ModePerm); err != nil { + return err + } + return nil } diff --git a/public/csv b/public/csv new file mode 100755 index 0000000..fdfb069 --- /dev/null +++ b/public/csv @@ -0,0 +1,4 @@ +"Link to Ride","Date","Day","Time","Route","Start","Notes" +"https://ridewithgps.com/routes/29835879","May 11","Sat","9:00 AM","Hobble Creek R & L Forks 86-Mi","OLD Bike Peddler 24 E Main American Fork","Great way to start the cycling year with good friends! Weather Permitting" +"","May 11","Sat","9:45 AM","64-Mile Ride Hobble Creek R & L Forks","Gas Station 1600 E 800 N Orem","Same route as last week." +"","May 13","Mon","5:45 AM","Spin Class","VASA Fitness American Fork","High Intensity group workout on new spin bikes" \ No newline at end of file diff --git a/public/index.html b/public/index.html index 5339bb3..faeb3d7 100644 --- a/public/index.html +++ b/public/index.html @@ -106,10 +106,11 @@ var google_doc_pre = "https://docs.google.com/spreadsheets/d/" var google_doc_id = "14F7zRrwGT6JkCeZqZLPM2r2GScrm9FLFxlPtDINbNWg" var google_doc_post = "/gviz/tq?tqx=out:csv" + //`${cors_anywhere}/${google_doc_pre}/${google_doc_id}/${google_doc_post}`, http( "get", - `${cors_anywhere}/${google_doc_pre}/${google_doc_id}/${google_doc_post}`, + window.location+"/csv", populate_schedule, JSON.stringify({"cc": "cc"}), )