2016-12-30 23:20:38 +00:00
|
|
|
package http
|
|
|
|
|
2017-01-25 22:45:03 +00:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
2016-12-30 23:20:38 +00:00
|
|
|
|
|
|
|
// FileHandler represents an HTTP API handler for managing static files.
|
|
|
|
type FileHandler struct {
|
|
|
|
http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFileHandler(assetPath string) *FileHandler {
|
|
|
|
h := &FileHandler{
|
|
|
|
Handler: http.FileServer(http.Dir(assetPath)),
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2017-01-25 22:45:03 +00:00
|
|
|
func isHTML(acceptContent []string) bool {
|
|
|
|
for _, accept := range acceptContent {
|
|
|
|
if strings.Contains(accept, "text/html") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-12-30 23:20:38 +00:00
|
|
|
func (fileHandler *FileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2017-01-25 22:45:03 +00:00
|
|
|
if !isHTML(r.Header["Accept"]) {
|
|
|
|
w.Header().Set("Cache-Control", "max-age=31536000")
|
|
|
|
} else {
|
|
|
|
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
|
|
}
|
2016-12-30 23:20:38 +00:00
|
|
|
fileHandler.Handler.ServeHTTP(w, r)
|
|
|
|
}
|