Compress more http content-types.

Also, some minor refactoring
This commit is contained in:
Deluan 2022-11-26 13:13:05 -05:00
parent 472f99b2b5
commit 1b5f855bff
5 changed files with 38 additions and 26 deletions

View file

@ -61,7 +61,7 @@ func loggerInjector(next http.Handler) http.Handler {
})
}
func robotsTXT(fs fs.FS) func(next http.Handler) http.Handler {
func robotsTXT(fs fs.FS) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/robots.txt") {
@ -74,7 +74,7 @@ func robotsTXT(fs fs.FS) func(next http.Handler) http.Handler {
}
}
func corsHandler() func(h http.Handler) http.Handler {
func corsHandler() func(http.Handler) http.Handler {
return cors.Handler(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{
@ -91,7 +91,7 @@ func corsHandler() func(h http.Handler) http.Handler {
})
}
func secureMiddleware() func(h http.Handler) http.Handler {
func secureMiddleware() func(http.Handler) http.Handler {
sec := secure.New(secure.Options{
ContentTypeNosniff: true,
FrameDeny: true,
@ -102,6 +102,19 @@ func secureMiddleware() func(h http.Handler) http.Handler {
return sec.Handler
}
func compressMiddleware() func(http.Handler) http.Handler {
return middleware.Compress(
5,
"application/xml",
"application/json",
"application/javascript",
"text/html",
"text/plain",
"text/css",
"text/javascript",
)
}
func clientUniqueIdAdder(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()