diff options
author | Laria Carolin Chabowski <laria@laria.me> | 2022-01-11 23:39:30 +0100 |
---|---|---|
committer | Laria Carolin Chabowski <laria@laria.me> | 2022-01-11 23:40:14 +0100 |
commit | 2d73e0567c9ac6e03cfc8bfc62a8f24ef4fadebc (patch) | |
tree | 6eb596532249711e9f01f3c8f214cc8d61bc0ca5 /main.go | |
parent | 67852eb58e1797a54a34df44e3444829d9031ad7 (diff) | |
download | startpage-master.tar.gz startpage-master.tar.bz2 startpage-master.zip |
The old XML API will be discontinued next month and is already returning
errors occasionally (apparently to nudge lazy devs like me to update their
code :D).
We're also doing a much better job at caching now, as suggested by the API
documentation.
We're not yet showing a weather icon, I still have to figure out how to do
this now (apparently the weather icons are a separate API / actually just a
tag.gz archive that the weather API references?), but at least we're not
panic()ing any more in the request handlers due to the unexpected error
responses :/.
See https://developer.yr.no/doc/GettingStarted/ for an intorduction to the
new API.
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 25 |
1 files changed, 20 insertions, 5 deletions
@@ -7,6 +7,7 @@ import ( "fmt" "html/template" "log" + "math" "net/http" "os" "path" @@ -52,11 +53,11 @@ func loadTemplate(templateDir string) error { } func buildWeatherProvider(config Config) *weather.WeatherProvider { - if config.WeatherPlace == "" { + if config.WeatherCoords.Lat == "" || config.WeatherCoords.Lon == "" { return nil } - return weather.NewWeatherProvider(config.WeatherPlace) + return weather.NewWeatherProvider(config.WeatherCoords.Lat, config.WeatherCoords.Lon) } func buildRedditImageProvider(config Config) *reddit_background.RedditImageProvider { @@ -96,9 +97,23 @@ func main() { log.Fatal(http.ListenAndServe(*laddr, nil)) } +type TplWeather struct { + Temp int +} + +func convertWeather(ts *weather.TimeseriesEntry) *TplWeather { + if ts == nil { + return nil + } + + return &TplWeather{ + Temp: int(math.Round(ts.Temperature())), + } +} + type TplData struct { BgImage *reddit_background.RedditImageForAjax - Weather *weather.Weather + Weather *TplWeather Links []Link CanSaveBg bool } @@ -109,7 +124,7 @@ func startpage(config Config, redditImageProvider *reddit_background.RedditImage return func(rw http.ResponseWriter, req *http.Request) { defer req.Body.Close() - var curWeather *weather.Weather = nil + var curWeather *weather.TimeseriesEntry = nil if weatherProvider != nil { var err error if curWeather, err = weatherProvider.CurrentWeather(); err != nil { @@ -119,7 +134,7 @@ func startpage(config Config, redditImageProvider *reddit_background.RedditImage if err := tpl.Execute(rw, &TplData{ redditImageProvider.Image().ForAjax(), - curWeather, + convertWeather(curWeather), config.Links, config.BackgroundSavepath != "", }); err != nil { |