satellite/console/consoleweb: initialize mime lazily

Change-Id: I80b78edcf057acef9b5a599cb77308baddc07692
This commit is contained in:
Egon Elbre 2023-09-07 16:48:36 +03:00 committed by Storj Robot
parent 0a3ee6ff8a
commit 3e73d414d1
2 changed files with 18 additions and 12 deletions

View File

@ -12,7 +12,6 @@ import (
"fmt"
"html/template"
"io/fs"
"mime"
"net"
"net/http"
"net/http/httputil"
@ -215,6 +214,8 @@ func (a *apiAuth) RemoveAuthCookie(w http.ResponseWriter) {
// NewServer creates new instance of console server.
func NewServer(logger *zap.Logger, config Config, service *console.Service, oidcService *oidc.Service, mailService *mailservice.Service, analytics *analytics.Service, abTesting *abtesting.Service, accountFreezeService *console.AccountFreezeService, listener net.Listener, stripePublicKey string, neededTokenPaymentConfirmations int, nodeURL storj.NodeURL, packagePlans paymentsconfig.PackagePlans) *Server {
initAdditionalMimeTypes()
server := Server{
log: logger,
config: config,
@ -1083,7 +1084,7 @@ func (server *Server) serveError(w http.ResponseWriter, status int) {
func (server *Server) seoHandler(w http.ResponseWriter, req *http.Request) {
header := w.Header()
header.Set(contentType, mime.TypeByExtension(".txt"))
header.Set(contentType, typeByExtension(".txt"))
header.Set("X-Content-Type-Options", "nosniff")
_, err := w.Write([]byte(server.config.SEO))
@ -1111,7 +1112,7 @@ func (server *Server) brotliMiddleware(fn http.Handler) http.Handler {
}
extension := filepath.Ext(info.Name()[:len(info.Name())-3])
w.Header().Set(contentType, mime.TypeByExtension(extension))
w.Header().Set(contentType, typeByExtension(extension))
w.Header().Set("Content-Encoding", "br")
newRequest := new(http.Request)

View File

@ -11,6 +11,7 @@ import (
"net/http"
"regexp"
"strings"
"sync"
"github.com/zeebo/errs"
@ -21,16 +22,20 @@ import (
// ContentLengthLimit describes 4KB limit.
const ContentLengthLimit = 4 * memory.KB
func init() {
err := mime.AddExtensionType(".ttf", "font/ttf")
if err != nil {
panic(err.Error())
}
var _initAdditionalMimeTypes sync.Once
err = mime.AddExtensionType(".txt", "text/plain")
if err != nil {
panic(err.Error())
}
// initAdditionalMimeTypes initializes additional mime types,
// however we do it lazily to avoid needing to load OS mime types in tests.
func initAdditionalMimeTypes() {
_initAdditionalMimeTypes.Do(func() {
_ = mime.AddExtensionType(".ttf", "font/ttf")
_ = mime.AddExtensionType(".txt", "text/plain")
})
}
func typeByExtension(ext string) string {
initAdditionalMimeTypes()
return mime.TypeByExtension(ext)
}
// JSON request from graphql clients.