From df0bf9bed794edae1f0c01e685e1aa17faa4ec20 Mon Sep 17 00:00:00 2001 From: Tilo K Date: Sun, 15 Feb 2026 18:49:22 +0100 Subject: [PATCH] feat: optimize mime type --- config/config.go | 2 + go.mod | 6 +++ go.sum | 21 ++++++++++ http_handle.go | 3 +- main.go | 6 +++ response_helper/response_helper.go | 67 ++++++++++++++++++++++++++++++ 6 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 go.sum diff --git a/config/config.go b/config/config.go index f7c37f4..0e793bd 100644 --- a/config/config.go +++ b/config/config.go @@ -3,6 +3,7 @@ package config type Config struct { NetInterface string Htdocs string + Profile bool } func GetConfig() *Config { @@ -10,5 +11,6 @@ func GetConfig() *Config { return &Config{ NetInterface: "127.0.0.1:80", Htdocs: "./htdocs", + Profile: false, } } diff --git a/go.mod b/go.mod index e2f133b..e842fdb 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,9 @@ module tilok.dev/go-http-server go 1.26.0 + +require ( + github.com/felixge/fgprof v0.9.3 // indirect + github.com/google/pprof v0.0.0-20211214055906-6f57359322fd // indirect + github.com/pkg/profile v1.7.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d066422 --- /dev/null +++ b/go.sum @@ -0,0 +1,21 @@ +github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/felixge/fgprof v0.9.3 h1:VvyZxILNuCiUCSXtPtYmmtGvb65nqXh2QFWc0Wpf2/g= +github.com/felixge/fgprof v0.9.3/go.mod h1:RdbpDgzqYVh/T9fPELJyV7EYJuHB55UTEULNun8eiPw= +github.com/google/pprof v0.0.0-20211214055906-6f57359322fd h1:1FjCyPC+syAzJ5/2S8fqdZK1R22vvA0J7JZKcuOIQ7Y= +github.com/google/pprof v0.0.0-20211214055906-6f57359322fd/go.mod h1:KgnwoLYCZ8IQu3XUZ8Nc/bM9CCZFOyjUNOSygVozoDg= +github.com/ianlancetaylor/demangle v0.0.0-20210905161508-09a460cdf81d/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w= +github.com/pkg/profile v1.7.0 h1:hnbDkaNWPCLMO9wGLdBFTIZvzDrDfBM2072E1S9gJkA= +github.com/pkg/profile v1.7.0/go.mod h1:8Uer0jas47ZQMJ7VD+OHknK4YDY07LPUC6dEvqDjvNo= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/http_handle.go b/http_handle.go index 1316efa..80a6d76 100644 --- a/http_handle.go +++ b/http_handle.go @@ -7,7 +7,6 @@ import ( "io" "io/fs" "log/slog" - "mime" "net" "os" "path" @@ -74,7 +73,7 @@ func sendFile(file *os.File, conn net.Conn) { parts := strings.Split(fileStat.Name(), ".") ext := "." + parts[len(parts)-1] - mime_type := mime.TypeByExtension(ext) + mime_type := rh.ExtensionToMimetype[ext] length := fileStat.Size() if mime_type == "" { diff --git a/main.go b/main.go index 43ee636..5000769 100644 --- a/main.go +++ b/main.go @@ -6,12 +6,18 @@ import ( "net" "time" + "github.com/pkg/profile" "tilok.dev/go-http-server/config" rh "tilok.dev/go-http-server/response_helper" ) func main() { conf := config.GetConfig() + + if conf.Profile { + defer profile.Start().Stop() + } + listener, err := net.Listen("tcp", conf.NetInterface) if err != nil { slog.Error("Could not create listener", "err", err.Error()) diff --git a/response_helper/response_helper.go b/response_helper/response_helper.go index 14c97e7..6f8679c 100644 --- a/response_helper/response_helper.go +++ b/response_helper/response_helper.go @@ -12,3 +12,70 @@ func RespondWithStatusCode(statusCode int, conn net.Conn) { conn.Write([]byte("Content-Length: 0\r\n")) conn.Write([]byte("\r\n")) } + +var ExtensionToMimetype = map[string]string{ + ".aac": "audio/aac", + ".avi": "video/x-msvideo", + ".avif": "image/avif", + ".bin": "application/octet-stream", + ".bmp": "image/bmp", + ".bz": "application/x-bzip", + ".bz2": "application/x-bzip2", + ".css": "text/css; charset=utf-8", + ".csv": "text/csv; charset=utf-8", + ".doc": "application/msword", + ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + ".eot": "application/vnd.ms-fontobject", + ".epub": "application/epub+zip", + ".gif": "image/gif", + ".gz": "application/gzip", + ".htm": "text/html; charset=utf-8", + ".html": "text/html; charset=utf-8", + ".ico": "image/x-icon", + ".jar": "application/java-archive", + ".jpeg": "image/jpeg", + ".jpg": "image/jpeg", + ".js": "text/javascript; charset=utf-8", + ".json": "application/json; charset=utf-8", + ".map": "application/json; charset=utf-8", + ".md": "text/markdown; charset=utf-8", + ".mjs": "text/javascript; charset=utf-8", + ".mp3": "audio/mpeg", + ".mp4": "video/mp4", + ".mpeg": "video/mpeg", + ".mpg": "video/mpeg", + ".odp": "application/vnd.oasis.opendocument.presentation", + ".ods": "application/vnd.oasis.opendocument.spreadsheet", + ".odt": "application/vnd.oasis.opendocument.text", + ".oga": "audio/ogg", + ".ogg": "audio/ogg", + ".ogv": "video/ogg", + ".otf": "font/otf", + ".pdf": "application/pdf", + ".png": "image/png", + ".ppt": "application/vnd.ms-powerpoint", + ".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation", + ".rar": "application/vnd.rar", + ".rtf": "application/rtf", + ".svg": "image/svg+xml", + ".tar": "application/x-tar", + ".tif": "image/tiff", + ".tiff": "image/tiff", + ".ts": "video/mp2t", + ".ttf": "font/ttf", + ".txt": "text/plain; charset=utf-8", + ".wav": "audio/wav", + ".weba": "audio/webm", + ".webm": "video/webm", + ".webp": "image/webp", + ".woff": "font/woff", + ".woff2": "font/woff2", + ".xhtml": "application/xhtml+xml; charset=utf-8", + ".xls": "application/vnd.ms-excel", + ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + ".xml": "application/xml; charset=utf-8", + ".yaml": "application/yaml; charset=utf-8", + ".yml": "application/yaml; charset=utf-8", + ".zip": "application/zip", + ".7z": "application/x-7z-compressed", +}