diff --git a/cmd/webapp/handlers.go b/cmd/webapp/handlers.go index fa51115..a2a4c0f 100644 --- a/cmd/webapp/handlers.go +++ b/cmd/webapp/handlers.go @@ -7,7 +7,6 @@ import ( "fmt" "html/template" "io" - "io/ioutil" "log" "net/http" "net/url" @@ -17,11 +16,13 @@ import ( ) const ( - AlbumsConfigUrl = "https://www.dropbox.com/s/kr8ewc68husts57/albums.json?dl=1" + AlbumsConfigUrl = "https://www.dropbox.com/s/kr8ewc68husts57/albums.json?dl=1" + AllisonConfigUrl = "https://www.dropbox.com/scl/fi/6yalddhnyuo2sld7y845e/web-config.json?rlkey=q1c1lztc1tsz8vhy8xwvqdtl9&st=khjv2q6e&dl=1" ) var ( - albumsTemplate = template.Must(template.ParseFS(templatesFS, "templates/albums.html")) + albumsTemplate = template.Must(template.ParseFS(templatesFS, "templates/albums.html")) + allisonTemplate = template.Must(template.ParseFS(templatesFS, "templates/allison.html")) ) type album struct { @@ -31,6 +32,16 @@ type album struct { CoverUrl string } +type webItem struct { + Desc string `json:"desc"` + Url string `json:"url"` +} + +type webSection struct { + Name string `json:"name"` + Items []*webItem `json:"items"` +} + // albumsHandler serves a page that lists all available photo albums. func (svr *server) albumsHandler(w http.ResponseWriter, _ *http.Request) { rsp, err := http.Get(AlbumsConfigUrl) @@ -64,6 +75,34 @@ func (svr *server) albumsHandler(w http.ResponseWriter, _ *http.Request) { } } +func (svr *server) allisonHandler(w http.ResponseWriter, _ *http.Request) { + rsp, err := http.Get(AllisonConfigUrl) + if err != nil { + internal.HttpError(w, http.StatusInternalServerError, "failed to fetch web config file from dropbox: %s", err) + return + } + defer internal.DrainAndClose(rsp.Body) + + if err := internal.CheckResponse(rsp); err != nil { + internal.HttpError(w, http.StatusInternalServerError, "failed to fetch web config file from dropbox: %s", err) + return + } + + var results struct { + Sections []*webSection `json:"sections"` + } + + if err := json.NewDecoder(rsp.Body).Decode(&results); err != nil { + internal.HttpError(w, http.StatusInternalServerError, "failed to json-decode response: %s", err) + return + } + + if err := allisonTemplate.Execute(w, results.Sections); err != nil { + internal.HttpError(w, http.StatusInternalServerError, "failed to render template: %s", err) + return + } +} + func (svr *server) dumpHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/plain") @@ -93,7 +132,7 @@ func (svr *server) dumpHandler(w http.ResponseWriter, r *http.Request) { writeIt("%s", buf.String()) fmt.Fprintln(w, "") // write blank line to response body - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { writeIt("error: failed to read body: %s", err) } else { diff --git a/cmd/webapp/main.go b/cmd/webapp/main.go index 9470df1..19f7b39 100644 --- a/cmd/webapp/main.go +++ b/cmd/webapp/main.go @@ -122,6 +122,7 @@ func main() { mux.HandleFunc("/albums/", svr.albumsHandler) mux.HandleFunc("/albums/thumbnail/", svr.thumbnailHandler) + mux.HandleFunc("/allison", svr.allisonHandler) mux.HandleFunc("/dump/", svr.dumpHandler) stravaDb := strava.NewSqliteDb(db) diff --git a/cmd/webapp/templates/albums.html b/cmd/webapp/templates/albums.html index 01cdcaa..3efc188 100644 --- a/cmd/webapp/templates/albums.html +++ b/cmd/webapp/templates/albums.html @@ -2,17 +2,28 @@ - +
diff --git a/cmd/webapp/templates/allison.html b/cmd/webapp/templates/allison.html new file mode 100644 index 0000000..86b7e4c --- /dev/null +++ b/cmd/webapp/templates/allison.html @@ -0,0 +1,43 @@ + + + + + + + +
+
Memories & Celebration of Allison Rose
+
+ {{ range . }} +

{{.Name}}

+ + {{ end }} +
+
+ + diff --git a/cmd/webapp/templates/strava.html b/cmd/webapp/templates/strava.html index 093e1c9..022b96f 100644 --- a/cmd/webapp/templates/strava.html +++ b/cmd/webapp/templates/strava.html @@ -4,16 +4,27 @@ -