2018-06-11 15:13:19 +02:00
|
|
|
package file
|
2016-12-31 12:20:38 +13:00
|
|
|
|
2017-01-26 11:45:03 +13:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
2016-12-31 12:20:38 +13:00
|
|
|
|
2018-06-11 15:13:19 +02:00
|
|
|
// Handler represents an HTTP API handler for managing static files.
|
|
|
|
type Handler struct {
|
2016-12-31 12:20:38 +13:00
|
|
|
http.Handler
|
|
|
|
}
|
|
|
|
|
2018-06-11 15:13:19 +02:00
|
|
|
// NewHandler creates a handler to serve static files.
|
|
|
|
func NewHandler(assetPublicPath string) *Handler {
|
|
|
|
h := &Handler{
|
2017-10-26 11:17:45 +02:00
|
|
|
Handler: http.FileServer(http.Dir(assetPublicPath)),
|
2016-12-31 12:20:38 +13:00
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2017-01-26 11:45:03 +13:00
|
|
|
func isHTML(acceptContent []string) bool {
|
|
|
|
for _, accept := range acceptContent {
|
|
|
|
if strings.Contains(accept, "text/html") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-06-11 15:13:19 +02:00
|
|
|
func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2017-01-26 11:45:03 +13: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")
|
|
|
|
}
|
2017-05-29 22:10:36 +02:00
|
|
|
handler.Handler.ServeHTTP(w, r)
|
2016-12-31 12:20:38 +13:00
|
|
|
}
|