Skip to content

Implement common file server to return consistent Content-Type#191

Merged
brian-brazil merged 2 commits intoprometheus:masterfrom
mrasu:force-mime
May 7, 2019
Merged

Implement common file server to return consistent Content-Type#191
brian-brazil merged 2 commits intoprometheus:masterfrom
mrasu:force-mime

Conversation

@mrasu
Copy link
Copy Markdown
Contributor

@mrasu mrasu commented May 4, 2019

When /etc/mime.types has an unusual mime type, Content-Type of response becomes the type and you may get an unexpected result.
This server returns consistent Content-Type header for js and css files

Signed-off-by: mrasu m.rasu.hitsuji@gmail.com

When `/etc/mime.types` has a unusual mime type, Content-Type of response becomes the type and you may get unexpected result.
This server returns consistent Content-Type header for js and css files

Signed-off-by: mrasu <m.rasu.hitsuji@gmail.com>
"path/filepath"
)

func StaticFileServer(root http.FileSystem) http.Handler {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can even remove the internal staticFileHandler struct by using a http.HandlerFunc:

func StaticFileServer(root http.FileSystem) http.Handler {
        return http.HandlerFunc(
                func(w http.ResponseWriter, r *http.Request) {
                        fileExt := filepath.Ext(r.URL.Path)

                        switch fileExt {
                        case ".js":
                                w.Header().Set("Content-Type", "application/javascript")
                        case ".css":
                                w.Header().Set("Content-Type", "text/css")
                        }

                        http.FileServer(root).ServeHTTP(w, r)
                },
        )
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Changed

case ".js":
w.Header().Set("Content-Type", "application/javascript")
case ".css":
w.Header().Set("Content-Type", "text/css")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add images too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I add png, jpg, and gif

Signed-off-by: mrasu <m.rasu.hitsuji@gmail.com>
@brian-brazil
Copy link
Copy Markdown
Contributor

👍

Copy link
Copy Markdown
Member

@simonpasquier simonpasquier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@brian-brazil brian-brazil merged commit 1ba8873 into prometheus:master May 7, 2019
@brian-brazil
Copy link
Copy Markdown
Contributor

Thanks!

@mrasu mrasu deleted the force-mime branch May 8, 2019 11:49
@beorn7
Copy link
Copy Markdown
Member

beorn7 commented May 8, 2019

@brian-brazil does this justify tagging a new release? It would help to use this feature in other repos.

@brian-brazil
Copy link
Copy Markdown
Contributor

Sure, done. We can release this repo anytime, it's pretty light.

alanprot pushed a commit to alanprot/common that referenced this pull request Mar 15, 2023
* Make signal-handling by Server configurable.

* Fixed unrelated lint warnings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants