mirror of
				https://github.com/owncast/owncast.git
				synced 2025-11-04 13:27:21 +08:00 
			
		
		
		
	Update caching logic/headers (#184)
* Improve caching by adding etags and reducing the max-age * Move caching into middleware. Set cache headers on segments
This commit is contained in:
		@ -37,21 +37,29 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// For search engine bots and social scrapers return a special
 | 
				
			||||||
 | 
						// server-rendered page.
 | 
				
			||||||
	if utils.IsUserAgentABot(r.UserAgent()) && isIndexRequest {
 | 
						if utils.IsUserAgentABot(r.UserAgent()) && isIndexRequest {
 | 
				
			||||||
		handleScraperMetadataPage(w, r)
 | 
							handleScraperMetadataPage(w, r)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// If the ETags match then return a StatusNotModified
 | 
				
			||||||
 | 
						if responseCode := middleware.ProcessEtags(w, r); responseCode != 0 {
 | 
				
			||||||
 | 
							w.WriteHeader(responseCode)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if path.Ext(r.URL.Path) == ".m3u8" {
 | 
						if path.Ext(r.URL.Path) == ".m3u8" {
 | 
				
			||||||
		middleware.DisableCache(w)
 | 
							middleware.DisableCache(w)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		clientID := utils.GenerateClientIDFromRequest(r)
 | 
							clientID := utils.GenerateClientIDFromRequest(r)
 | 
				
			||||||
		core.SetClientActive(clientID)
 | 
							core.SetClientActive(clientID)
 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		// Set a cache control header of one day
 | 
					 | 
				
			||||||
		middleware.SetCache(1, w)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Set a cache control max-age header
 | 
				
			||||||
 | 
						middleware.SetCachingHeaders(w, r)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	http.ServeFile(w, r, path.Join("webroot", r.URL.Path))
 | 
						http.ServeFile(w, r, path.Join("webroot", r.URL.Path))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										1
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
									
									
									
									
								
							@ -4,6 +4,7 @@ go 1.14
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
require (
 | 
					require (
 | 
				
			||||||
	github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
 | 
						github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
 | 
				
			||||||
 | 
						github.com/amalfra/etag v0.0.0-20190921100247-cafc8de96bc5
 | 
				
			||||||
	github.com/aws/aws-sdk-go v1.34.0
 | 
						github.com/aws/aws-sdk-go v1.34.0
 | 
				
			||||||
	github.com/go-ole/go-ole v1.2.4 // indirect
 | 
						github.com/go-ole/go-ole v1.2.4 // indirect
 | 
				
			||||||
	github.com/mattn/go-sqlite3 v1.14.0
 | 
						github.com/mattn/go-sqlite3 v1.14.0
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										2
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.sum
									
									
									
									
									
								
							@ -1,6 +1,8 @@
 | 
				
			|||||||
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
 | 
					github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
 | 
				
			||||||
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
 | 
					github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
 | 
				
			||||||
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
 | 
					github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
 | 
				
			||||||
 | 
					github.com/amalfra/etag v0.0.0-20190921100247-cafc8de96bc5 h1:+ty4KYpIDUhjzsVsV+HiTeYEfufBc/4FLNiqIGU1A1U=
 | 
				
			||||||
 | 
					github.com/amalfra/etag v0.0.0-20190921100247-cafc8de96bc5/go.mod h1:Qk51jPgvIaO549MR+IvLP/uMZbZGs05QJSzEhDVZ1jc=
 | 
				
			||||||
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
 | 
					github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
 | 
				
			||||||
github.com/aws/aws-sdk-go v1.34.0 h1:brux2dRrlwCF5JhTL7MUT3WUwo9zfDHZZp3+g3Mvlmo=
 | 
					github.com/aws/aws-sdk-go v1.34.0 h1:brux2dRrlwCF5JhTL7MUT3WUwo9zfDHZZp3+g3Mvlmo=
 | 
				
			||||||
github.com/aws/aws-sdk-go v1.34.0/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
 | 
					github.com/aws/aws-sdk-go v1.34.0/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										64
									
								
								router/middleware/caching.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								router/middleware/caching.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,64 @@
 | 
				
			|||||||
 | 
					package middleware
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"path"
 | 
				
			||||||
 | 
						"path/filepath"
 | 
				
			||||||
 | 
						"strconv"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/amalfra/etag"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//DisableCache writes the disable cache header on the responses
 | 
				
			||||||
 | 
					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)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// ProcessEtags gets and sets ETags for caching purposes
 | 
				
			||||||
 | 
					func ProcessEtags(w http.ResponseWriter, r *http.Request) int {
 | 
				
			||||||
 | 
						info, err := os.Stat(filepath.Join("webroot", r.URL.Path))
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return 0
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						localContentEtag := etag.Generate(info.ModTime().String(), true)
 | 
				
			||||||
 | 
						if remoteEtagHeader := r.Header.Get("If-None-Match"); remoteEtagHeader != "" {
 | 
				
			||||||
 | 
							if remoteEtagHeader == localContentEtag {
 | 
				
			||||||
 | 
								return http.StatusNotModified
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						w.Header().Set("Etag", localContentEtag)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 0
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// SetCachingHeaders will set the cache control header of a response
 | 
				
			||||||
 | 
					func SetCachingHeaders(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
						setCacheSeconds(getCacheDurationSecondsForPath(r.URL.Path), w)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func getCacheDurationSecondsForPath(filePath string) int {
 | 
				
			||||||
 | 
						if path.Base(filePath) == "thumbnail.jpg" {
 | 
				
			||||||
 | 
							// Thumbnails re-generate during live
 | 
				
			||||||
 | 
							return 20
 | 
				
			||||||
 | 
						} else if path.Ext(filePath) == ".js" || path.Ext(filePath) == ".css" {
 | 
				
			||||||
 | 
							// Cache javascript & CSS
 | 
				
			||||||
 | 
							return 60
 | 
				
			||||||
 | 
						} else if path.Ext(filePath) == ".ts" {
 | 
				
			||||||
 | 
							// Cache video segments as long as you want. They can't change.
 | 
				
			||||||
 | 
							// This matters most for local hosting of segments for recordings
 | 
				
			||||||
 | 
							// and not for live or 3rd party storage.
 | 
				
			||||||
 | 
							return 31557600
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Default cache length in seconds
 | 
				
			||||||
 | 
						return 30 * 60
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -1,19 +0,0 @@
 | 
				
			|||||||
package middleware
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"net/http"
 | 
					 | 
				
			||||||
	"strconv"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//DisableCache writes the disable cache header on the responses
 | 
					 | 
				
			||||||
func DisableCache(w http.ResponseWriter) {
 | 
					 | 
				
			||||||
	w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
 | 
					 | 
				
			||||||
	w.Header().Set("Expires", "0")
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//SetCache will set the cache control header of a response
 | 
					 | 
				
			||||||
func SetCache(days int, w http.ResponseWriter) {
 | 
					 | 
				
			||||||
	seconds := strconv.Itoa(days * 86400)
 | 
					 | 
				
			||||||
	w.Header().Set("Cache-Control", "max-age="+seconds)
 | 
					 | 
				
			||||||
	w.Header().Set("Expires", seconds)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
		Reference in New Issue
	
	Block a user