package utils import ( "encoding/json" "github.com/muety/wakapi/config" "net/http" "regexp" "strconv" "strings" "time" ) const ( cacheMaxAgePattern = `max-age=(\d+)` ) var ( cacheMaxAgeRe *regexp.Regexp ) func init() { cacheMaxAgeRe = regexp.MustCompile(cacheMaxAgePattern) } func RespondJSON(w http.ResponseWriter, r *http.Request, status int, object interface{}) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) if err := json.NewEncoder(w).Encode(object); err != nil { config.Log().Request(r).Error("error while writing json response: %v", err) } } func IsNoCache(r *http.Request, cacheTtl time.Duration) bool { cacheControl := r.Header.Get("cache-control") if strings.Contains(cacheControl, "no-cache") { return true } if match := cacheMaxAgeRe.FindStringSubmatch(cacheControl); match != nil && len(match) > 1 { if maxAge, _ := strconv.Atoi(match[1]); time.Duration(maxAge)*time.Second <= cacheTtl { return true } } return false }