...

Source file src/github.com/prometheus/common/server/static_file_server.go

Documentation: github.com/prometheus/common/server

     1  // Copyright 2019 The Prometheus Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package server
    15  
    16  import (
    17  	"net/http"
    18  	"path/filepath"
    19  )
    20  
    21  var mimeTypes = map[string]string{
    22  	".cjs":   "application/javascript",
    23  	".css":   "text/css",
    24  	".eot":   "font/eot",
    25  	".gif":   "image/gif",
    26  	".ico":   "image/x-icon",
    27  	".jpg":   "image/jpeg",
    28  	".js":    "application/javascript",
    29  	".json":  "application/json",
    30  	".less":  "text/plain",
    31  	".map":   "application/json",
    32  	".otf":   "font/otf",
    33  	".png":   "image/png",
    34  	".svg":   "image/svg+xml",
    35  	".ttf":   "font/ttf",
    36  	".txt":   "text/plain",
    37  	".woff":  "font/woff",
    38  	".woff2": "font/woff2",
    39  }
    40  
    41  func StaticFileServer(root http.FileSystem) http.Handler {
    42  	return http.HandlerFunc(
    43  		func(w http.ResponseWriter, r *http.Request) {
    44  			fileExt := filepath.Ext(r.URL.Path)
    45  
    46  			if t, ok := mimeTypes[fileExt]; ok {
    47  				w.Header().Set("Content-Type", t)
    48  			}
    49  
    50  			http.FileServer(root).ServeHTTP(w, r)
    51  		},
    52  	)
    53  }
    54  

View as plain text