From 6412e0a30cd37cd8b814802f8d24021af7d60bbd Mon Sep 17 00:00:00 2001 From: siddharth ravikumar Date: Sun, 29 May 2022 21:49:43 -0400 Subject: peach: update static file serving Add Cache-Control header for static files. --- main.go | 20 +++++++++++++++++--- templates/search.tmpl | 4 ++-- templates/weather.tmpl | 4 ++-- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index c19e133..c58839c 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ import ( "ricketyspace.net/peach/nws" "ricketyspace.net/peach/photon" + "ricketyspace.net/peach/version" ) // peach port. defaults to 8151 @@ -37,6 +38,7 @@ var latLngRegex = regexp.MustCompile(`/(-?[0-9]+\.?[0-9]+?),(-?[0-9]+\.?[0-9]+)` type Weather struct { Title string + Version string Location string Now WeatherNow Period WeatherPeriod @@ -64,6 +66,7 @@ type WeatherTimeline struct { type Search struct { Title string + Version string Location string Message string MatchingCoords []photon.Coordinates @@ -78,9 +81,6 @@ func init() { } func main() { - // static files handler. - http.Handle("/static/", http.FileServer(http.FS(peachFS))) - // search handler. http.HandleFunc("/search", showSearch) @@ -107,6 +107,9 @@ func main() { showWeather(w, float32(lat), float32(lng)) }) + // static files handler. + http.HandleFunc("/static/", serveStaticFile) + // start server log.Fatal(http.ListenAndServe(peachAddr, nil)) } @@ -164,6 +167,15 @@ func showSearch(w http.ResponseWriter, r *http.Request) { } } +func serveStaticFile(w http.ResponseWriter, r *http.Request) { + // Add Cache-Control header + w.Header().Set("Cache-Control", "max-age=604800") + + // Serve. + server := http.FileServer(http.FS(peachFS)) + server.ServeHTTP(w, r) +} + func NewWeather(point *nws.Point, f, fh *nws.Forecast) (*Weather, error) { w := new(Weather) w.Location = fmt.Sprintf("%s, %s", @@ -171,6 +183,7 @@ func NewWeather(point *nws.Point, f, fh *nws.Forecast) (*Weather, error) { strings.ToLower(point.Properties.RelativeLocation.Properties.State), ) w.Title = w.Location + w.Version = version.Version w.Now = WeatherNow{ Temperature: fh.Properties.Periods[0].Temperature, TemperatureUnit: fh.Properties.Periods[0].TemperatureUnit, @@ -214,6 +227,7 @@ func NewWeather(point *nws.Point, f, fh *nws.Forecast) (*Weather, error) { func NewSearch(r *http.Request) (*Search, error) { s := new(Search) s.Title = "search" + s.Version = version.Version if r.Method == "GET" { return s, nil diff --git a/templates/search.tmpl b/templates/search.tmpl index 1101f08..d00942c 100644 --- a/templates/search.tmpl +++ b/templates/search.tmpl @@ -5,8 +5,8 @@ peach - {{.Title}} - - + +
diff --git a/templates/weather.tmpl b/templates/weather.tmpl index e03ac75..2c731d4 100644 --- a/templates/weather.tmpl +++ b/templates/weather.tmpl @@ -5,8 +5,8 @@ peach - {{.Title}} - - + +
-- cgit v1.2.3