2020-10-01 00:14:39 +03:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2020-10-15 00:07:38 +03:00
|
|
|
"github.com/owncast/owncast/utils"
|
2020-10-01 00:14:39 +03:00
|
|
|
)
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// DisableCache writes the disable cache header on the responses.
|
2020-10-01 00:14:39 +03:00
|
|
|
func DisableCache(w http.ResponseWriter) {
|
|
|
|
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
|
|
w.Header().Set("Expires", "Thu, 1 Jan 1970 00:00:00 GMT")
|
|
|
|
}
|
|
|
|
|
|
|
|
func setCacheSeconds(seconds int, w http.ResponseWriter) {
|
|
|
|
secondsStr := strconv.Itoa(seconds)
|
|
|
|
w.Header().Set("Cache-Control", "public, max-age="+secondsStr)
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// SetCachingHeaders will set the cache control header of a response.
|
2020-10-01 00:14:39 +03:00
|
|
|
func SetCachingHeaders(w http.ResponseWriter, r *http.Request) {
|
2020-10-15 00:07:38 +03:00
|
|
|
setCacheSeconds(utils.GetCacheDurationSecondsForPath(r.URL.Path), w)
|
2020-10-01 00:14:39 +03:00
|
|
|
}
|