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" "fmt"
"html/template" "html/template"
"io/fs" "io/fs"
"mime"
"net" "net"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
@ -215,6 +214,8 @@ func (a *apiAuth) RemoveAuthCookie(w http.ResponseWriter) {
// NewServer creates new instance of console server. // 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 { 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{ server := Server{
log: logger, log: logger,
config: config, 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) { func (server *Server) seoHandler(w http.ResponseWriter, req *http.Request) {
header := w.Header() header := w.Header()
header.Set(contentType, mime.TypeByExtension(".txt")) header.Set(contentType, typeByExtension(".txt"))
header.Set("X-Content-Type-Options", "nosniff") header.Set("X-Content-Type-Options", "nosniff")
_, err := w.Write([]byte(server.config.SEO)) _, 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]) 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") w.Header().Set("Content-Encoding", "br")
newRequest := new(http.Request) newRequest := new(http.Request)

View File

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