feat: add ETag header to static assets
we are adding an ETag tag based on calculating the md5 hash of the file contents
This commit is contained in:
parent
8839b646fb
commit
54cda5546a
@ -2,12 +2,15 @@
|
|||||||
package misc
|
package misc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/hex"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
|
||||||
@ -141,29 +144,42 @@ var stylesheets = []string{"default.css", "custom.css"}
|
|||||||
|
|
||||||
func handlerStyle(w http.ResponseWriter, rq *http.Request) {
|
func handlerStyle(w http.ResponseWriter, rq *http.Request) {
|
||||||
w.Header().Set("Content-Type", mime.TypeByExtension(".css"))
|
w.Header().Set("Content-Type", mime.TypeByExtension(".css"))
|
||||||
|
hash := md5.New()
|
||||||
|
var buf strings.Builder
|
||||||
for _, name := range stylesheets {
|
for _, name := range stylesheets {
|
||||||
file, err := static.FS.Open(name)
|
file, err := static.FS.Open(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
_, err = io.Copy(w, file)
|
_, err = io.Copy(io.MultiWriter(&buf, hash), file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
_ = file.Close()
|
_ = file.Close()
|
||||||
}
|
}
|
||||||
|
w.Header().Set("ETag", hex.EncodeToString(hash.Sum(nil)))
|
||||||
|
|
||||||
|
_, err := io.Copy(w, strings.NewReader(buf.String()))
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlerRobotsTxt(w http.ResponseWriter, rq *http.Request) {
|
func handlerRobotsTxt(w http.ResponseWriter, rq *http.Request) {
|
||||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||||
|
hash := md5.New()
|
||||||
file, err := static.FS.Open("robots.txt")
|
file, err := static.FS.Open("robots.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
_, err = io.Copy(hash, file)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
w.Header().Set("ETag", hex.EncodeToString(hash.Sum(nil)))
|
||||||
_, err = io.Copy(w, file)
|
_, err = io.Copy(w, file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println()
|
log.Println(err)
|
||||||
}
|
}
|
||||||
_ = file.Close()
|
_ = file.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user