The server package provides HTTP server utilities for serving static files with appropriate MIME type handling.
import "github.com/prometheus/common/server"This package provides a simple wrapper around Go's standard HTTP file server that ensures proper MIME type detection and setting. It's particularly useful for serving web UI assets in Prometheus components.
func StaticFileServer(root http.FileSystem) http.HandlerReturns an http.Handler that serves files from the specified filesystem with appropriate MIME types. This function wraps the standard http.FileServer and adds custom MIME type detection to ensure files are served with correct Content-Type headers.
Parameters:
root - The root http.FileSystem to serve files fromReturns: An http.Handler that can be used with http.Handle or http.HandleFunc
package main
import (
"embed"
"log"
"net/http"
"github.com/prometheus/common/server"
)
//go:embed static/*
var staticFiles embed.FS
func main() {
// Create a file server for embedded files
fs := http.FS(staticFiles)
handler := server.StaticFileServer(fs)
// Register the handler
http.Handle("/", handler)
// Start the server
log.Println("Serving on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}package main
import (
"log"
"net/http"
"os"
"github.com/prometheus/common/server"
)
func main() {
// Serve files from a directory on disk
fs := http.Dir("./public")
handler := server.StaticFileServer(fs)
// Strip the /static prefix before passing to the file server
http.Handle("/static/", http.StripPrefix("/static/", handler))
log.Println("Serving /static from ./public on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}package main
import (
"embed"
"log"
"net/http"
"github.com/prometheus/common/assets"
"github.com/prometheus/common/server"
)
//go:embed ui/dist/*
var uiFiles embed.FS
func main() {
// Create assets filesystem with gzip support
assetsFS := assets.New(uiFiles)
// Create static file server
handler := server.StaticFileServer(http.FS(assetsFS))
// Serve the UI
http.Handle("/", handler)
log.Println("Serving UI on :9090")
log.Fatal(http.ListenAndServe(":9090", nil))
}package main
import (
"embed"
"encoding/json"
"log"
"net/http"
"github.com/prometheus/common/server"
)
//go:embed static/*
var staticFiles embed.FS
func main() {
// API endpoints
http.HandleFunc("/api/status", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
})
// Static file serving
fs := http.FS(staticFiles)
handler := server.StaticFileServer(fs)
http.Handle("/", handler)
log.Println("Serving on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}The StaticFileServer function ensures that files are served with appropriate MIME types based on their file extensions. The server recognizes the following file types:
.cjs - CommonJS modules (application/javascript).css - Stylesheets (text/css).eot - Embedded OpenType fonts (font/eot).gif - GIF images (image/gif).ico - Icons (image/x-icon).jpg - JPEG images (image/jpeg).js - JavaScript (application/javascript).json - JSON data (application/json).less - LESS stylesheets (text/plain).map - Source maps (application/json).otf - OpenType fonts (font/otf).png - PNG images (image/png).svg - SVG images (image/svg+xml).ttf - TrueType fonts (font/ttf).txt - Text files (text/plain).woff - WOFF fonts (font/woff).woff2 - WOFF2 fonts (font/woff2)For a complete reference of all supported MIME type mappings, see the table below:
| Extension | MIME Type |
|---|---|
| .cjs | application/javascript |
| .css | text/css |
| .eot | font/eot |
| .gif | image/gif |
| .ico | image/x-icon |
| .jpg | image/jpeg |
| .js | application/javascript |
| .json | application/json |
| .less | text/plain |
| .map | application/json |
| .otf | font/otf |
| .png | image/png |
| .svg | image/svg+xml |
| .ttf | font/ttf |
| .txt | text/plain |
| .woff | font/woff |
| .woff2 | font/woff2 |
When serving static files, be aware of:
Directory Traversal: The standard http.FileServer prevents directory traversal attacks, but ensure you're serving from the intended root directory.
Hidden Files: By default, http.FileServer will not serve files or directories starting with a period (.) or underscore (_).
Index Files: If a directory is requested, http.FileServer will look for an index.html file to serve.