summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2020-10-14 22:52:21 +0200
committerLaria Carolin Chabowski <laria@laria.me>2020-10-14 22:52:21 +0200
commitb8f7e622bbee068ac9e6a96c2307dbc7d7b02b14 (patch)
treec945307775d90c51301d060878ff816aae4f017d /main.go
parent5b456afb49bfa6ff1567510bc1d9362377d32216 (diff)
downloadstartpage-b8f7e622bbee068ac9e6a96c2307dbc7d7b02b14.tar.gz
startpage-b8f7e622bbee068ac9e6a96c2307dbc7d7b02b14.tar.bz2
startpage-b8f7e622bbee068ac9e6a96c2307dbc7d7b02b14.zip
Lazy load background image using XHR
We now don't immediately reload the reddit background image, as this delays the initial page load quite a bit. Instead we trigger an XHR request on page load to update the background image.
Diffstat (limited to 'main.go')
-rw-r--r--main.go23
1 files changed, 21 insertions, 2 deletions
diff --git a/main.go b/main.go
index e1ee16c..77944c0 100644
--- a/main.go
+++ b/main.go
@@ -1,6 +1,7 @@
package main
import (
+ "encoding/json"
"errors"
"flag"
"fmt"
@@ -61,6 +62,7 @@ func main() {
http.HandleFunc("/", startpage(*config, redditImageProvider))
http.HandleFunc("/bgimg", bgimg(redditImageProvider))
+ http.HandleFunc("/update-bgimg", updateBgimg(redditImageProvider))
if config.BackgroundSavepath != "" {
http.HandleFunc("/savebg", savebg(redditImageProvider, config.BackgroundSavepath))
@@ -70,7 +72,7 @@ func main() {
}
type TplData struct {
- BgImage *reddit_background.RedditImage
+ BgImage *reddit_background.RedditImageForAjax
Weather *weather.Weather
Links []Link
CanSaveBg bool
@@ -91,7 +93,7 @@ func startpage(config Config, redditImageProvider *reddit_background.RedditImage
}
if err := tpl.Execute(rw, &TplData{
- redditImageProvider.Image(),
+ redditImageProvider.Image().ForAjax(),
curWeather,
config.Links,
config.BackgroundSavepath != "",
@@ -118,6 +120,23 @@ func bgimg(redditImageProvider *reddit_background.RedditImageProvider) http.Hand
}
}
+func updateBgimg(redditImageProvider *reddit_background.RedditImageProvider) http.HandlerFunc {
+ return func(rw http.ResponseWriter, req *http.Request) {
+ defer req.Body.Close()
+
+ updated := redditImageProvider.UpdateImage()
+
+ rw.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(rw).Encode(struct {
+ Updated bool
+ Image *reddit_background.RedditImageForAjax
+ }{
+ updated,
+ redditImageProvider.Image().ForAjax(),
+ })
+ }
+}
+
func savebg(redditImageProvider *reddit_background.RedditImageProvider, savepath string) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
defer req.Body.Close()