diff options
author | siddharth ravikumar <s@ricketyspace.net> | 2022-05-29 21:49:43 -0400 |
---|---|---|
committer | siddharth ravikumar <s@ricketyspace.net> | 2022-05-29 21:49:43 -0400 |
commit | 6412e0a30cd37cd8b814802f8d24021af7d60bbd (patch) | |
tree | c4fc19f7ed1a2ec6ae6f420953b889722139ec6a | |
parent | 4335ba333449e6f5a95a566a9461249bf2dd8e07 (diff) |
peach: update static file serving
Add Cache-Control header for static files.
-rw-r--r-- | main.go | 20 | ||||
-rw-r--r-- | templates/search.tmpl | 4 | ||||
-rw-r--r-- | templates/weather.tmpl | 4 |
3 files changed, 21 insertions, 7 deletions
@@ -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 @@ <title>peach - {{.Title}}</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="author" content="siddharth ravikumar"> - <link rel="preload" href="/static/peach.css" as="style" /> - <style>@import url("/static/peach.css");</style> + <link rel="preload" href="/static/peach.css?{{ .Version }}" as="style" /> + <style>@import url("/static/peach.css?{{ .Version }}");</style> </head> <body> <div class="peach"> 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 @@ <title>peach - {{.Title}}</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="author" content="siddharth ravikumar"> - <link rel="preload" href="/static/peach.css" as="style" /> - <style>@import url("/static/peach.css");</style> + <link rel="preload" href="/static/peach.css?{{ .Version }}" as="style" /> + <style>@import url("/static/peach.css?{{ .Version }}");</style> </head> <body> <div class="peach"> |