Browse Source

Use some constants

master
Jan-Lukas Else 4 months ago
parent
commit
89ecd7c656
  1. 2
      authentication.go
  2. 17
      cache.go
  3. 2
      captcha.go
  4. 4
      geoTiles.go
  5. 2
      go.mod
  6. 4
      go.sum
  7. 2
      media.go
  8. 2
      staticFiles.go
  9. 2
      templateAssets.go

2
authentication.go

@ -52,7 +52,7 @@ func (a *goBlog) authMiddleware(next http.Handler) http.Handler {
return
}
// Show login form
w.Header().Set("Cache-Control", "no-store,max-age=0")
w.Header().Set(cacheControl, "no-store,max-age=0")
w.Header().Set("X-Robots-Tag", "noindex")
h, _ := json.Marshal(r.Header)
b, _ := io.ReadAll(io.LimitReader(r.Body, 20*1000*1000)) // Only allow 20 MB

17
cache.go

@ -19,6 +19,9 @@ import (
const (
cacheLoggedInKey contextKey = "cacheLoggedIn"
cacheExpirationKey contextKey = "cacheExpiration"
lastModified = "Last-Modified"
cacheControl = "Cache-Control"
)
type cache struct {
@ -129,13 +132,13 @@ func (a *goBlog) setCacheHeaders(w http.ResponseWriter, cache *cacheItem) {
}
// Set cache headers
w.Header().Set("ETag", cache.eTag)
w.Header().Set("Last-Modified", cache.creationTime.UTC().Format(http.TimeFormat))
if w.Header().Get("Cache-Control") == "" {
w.Header().Set(lastModified, cache.creationTime.UTC().Format(http.TimeFormat))
if w.Header().Get(cacheControl) == "" {
if cache.expiration != 0 {
w.Header().Set("Cache-Control", fmt.Sprintf("public,max-age=%d,stale-while-revalidate=%d", cache.expiration, cache.expiration))
w.Header().Set(cacheControl, fmt.Sprintf("public,max-age=%d,stale-while-revalidate=%d", cache.expiration, cache.expiration))
} else {
exp := a.cfg.Cache.Expiration
w.Header().Set("Cache-Control", fmt.Sprintf("public,max-age=%d,s-max-age=%d,stale-while-revalidate=%d", exp, exp/3, exp))
w.Header().Set(cacheControl, fmt.Sprintf("public,max-age=%d,s-max-age=%d,stale-while-revalidate=%d", exp, exp/3, exp))
}
}
}
@ -185,7 +188,7 @@ func (c *cache) getCache(key string, next http.Handler, r *http.Request) (item *
eTag, _ = getSHA256(bytes.NewReader(body))
}
lastMod := time.Now()
if lm := result.Header.Get("Last-Modified"); lm != "" {
if lm := result.Header.Get(lastModified); lm != "" {
if parsedTime, te := dateparse.ParseLocal(lm); te == nil {
lastMod = parsedTime
}
@ -193,7 +196,7 @@ func (c *cache) getCache(key string, next http.Handler, r *http.Request) (item *
// Remove problematic headers
result.Header.Del("Accept-Ranges")
result.Header.Del("ETag")
result.Header.Del("Last-Modified")
result.Header.Del(lastModified)
// Create cache item
exp, _ := cr.Context().Value(cacheExpirationKey).(int)
item = &cacheItem{
@ -205,7 +208,7 @@ func (c *cache) getCache(key string, next http.Handler, r *http.Request) (item *
body: body,
}
// Save cache
if cch := item.header.Get("Cache-Control"); !containsStrings(cch, "no-store", "private", "no-cache") {
if cch := item.header.Get(cacheControl); !containsStrings(cch, "no-store", "private", "no-cache") {
if exp == 0 {
c.c.Set(key, item, item.cost())
} else {

2
captcha.go

@ -64,7 +64,7 @@ func (a *goBlog) captchaMiddleware(next http.Handler) http.Handler {
}
// Render captcha
_ = ses.Save(r, w)
w.Header().Set("Cache-Control", "no-store,max-age=0")
w.Header().Set(cacheControl, "no-store,max-age=0")
a.renderWithStatusCode(w, r, http.StatusUnauthorized, a.renderCaptcha, &renderData{
Data: &captchaRenderData{
captchaMethod: r.Method,

4
geoTiles.go

@ -27,7 +27,7 @@ func (a *goBlog) proxyTiles(basePath string) http.HandlerFunc {
"Accept-Encoding",
"Accept-Language",
"Accept",
"Cache-Control",
cacheControl,
"If-Modified-Since",
"If-None-Match",
"User-Agent",
@ -45,7 +45,7 @@ func (a *goBlog) proxyTiles(basePath string) http.HandlerFunc {
"Accept-Ranges",
"Access-Control-Allow-Origin",
"Age",
"Cache-Control",
cacheControl,
"Content-Length",
"Content-Type",
"Etag",

2
go.mod

@ -55,7 +55,7 @@ require (
// master
github.com/yuin/goldmark-emoji v1.0.2-0.20210607094911-0487583eca38
github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01
golang.org/x/crypto v0.0.0-20220128200615-198e4374d7ed
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/text v0.3.7

4
go.sum

@ -472,8 +472,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220128200615-198e4374d7ed h1:YoWVYYAfvQ4ddHv3OKmIvX7NCAhFGTj62VP2l2kfBbA=
golang.org/x/crypto v0.0.0-20220128200615-198e4374d7ed/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 h1:71vQrMauZZhcTVK6KdYM+rklehEEwb3E+ZhaE5jrPrE=
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=

2
media.go

@ -21,6 +21,6 @@ func (a *goBlog) serveMediaFile(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
return
}
w.Header().Add("Cache-Control", "public,max-age=31536000,immutable")
w.Header().Add(cacheControl, "public,max-age=31536000,immutable")
http.ServeFile(w, r, f)
}

2
staticFiles.go

@ -39,6 +39,6 @@ func hasStaticPath(path string) bool {
// Gets only called by registered paths
func (a *goBlog) serveStaticFile(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", fmt.Sprintf("public,max-age=%d,s-max-age=%d,stale-while-revalidate=%d", a.cfg.Cache.Expiration, a.cfg.Cache.Expiration/3, a.cfg.Cache.Expiration))
w.Header().Set(cacheControl, fmt.Sprintf("public,max-age=%d,s-max-age=%d,stale-while-revalidate=%d", a.cfg.Cache.Expiration, a.cfg.Cache.Expiration/3, a.cfg.Cache.Expiration))
http.ServeFile(w, r, filepath.Join(staticFolder, r.URL.Path))
}

2
templateAssets.go

@ -110,7 +110,7 @@ func (a *goBlog) serveAsset(w http.ResponseWriter, r *http.Request) {
a.serve404(w, r)
return
}
w.Header().Set("Cache-Control", "public,max-age=31536000,immutable")
w.Header().Set(cacheControl, "public,max-age=31536000,immutable")
w.Header().Set(contentType, af.contentType+contenttype.CharsetUtf8Suffix)
_, _ = w.Write(af.body)
}

Loading…
Cancel
Save