or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

assets.mdconfig.mdexpfmt.mdhelpers-templates.mdindex.mdmodel.mdpromslog-flag.mdpromslog.mdroute.mdserver.mdversion.md
tile.json

server.mddocs/

Server Package

The server package provides HTTP server utilities for serving static files with appropriate MIME type handling.

Import

import "github.com/prometheus/common/server"

Overview

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.

Functions

StaticFileServer

func StaticFileServer(root http.FileSystem) http.Handler

Returns 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 from

Returns: An http.Handler that can be used with http.Handle or http.HandleFunc

Usage Example

Basic File Serving

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))
}

With URL Path Prefix

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))
}

With Prometheus Assets Package

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))
}

Combining with API Routes

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))
}

Notes

MIME Type Detection

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:

ExtensionMIME Type
.cjsapplication/javascript
.csstext/css
.eotfont/eot
.gifimage/gif
.icoimage/x-icon
.jpgimage/jpeg
.jsapplication/javascript
.jsonapplication/json
.lesstext/plain
.mapapplication/json
.otffont/otf
.pngimage/png
.svgimage/svg+xml
.ttffont/ttf
.txttext/plain
.wofffont/woff
.woff2font/woff2

Security Considerations

When serving static files, be aware of:

  1. Directory Traversal: The standard http.FileServer prevents directory traversal attacks, but ensure you're serving from the intended root directory.

  2. Hidden Files: By default, http.FileServer will not serve files or directories starting with a period (.) or underscore (_).

  3. Index Files: If a directory is requested, http.FileServer will look for an index.html file to serve.