static

package
v5.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2026 License: BSD-3-Clause Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-http/v4/static

Go package for serving embedded static assets. To reference those assets from HTML, use the rewrite package (see below).

Functions

  • StaticAssetsHandler(fs) and StaticAssetsHandlerWithPrefix(fs, prefix) return an http.Handler that serves the files in an fs.FS. The prefix is removed from the request path before looking up a file.
  • AppendStaticAssetHandlers(mux, fs) and AppendStaticAssetHandlersWithPrefix(mux, fs, prefix) register that handler on an http.ServeMux for each file in the fs.FS.

Example

Error handling omitted for brevity.

package main

import (
	"embed"
	"net/http"

	"github.com/aaronland/go-http/v5/rewrite"
	"github.com/aaronland/go-http/v5/static"
)

//go:embed css/*.css javascript/*.js
var static_fs embed.FS

func main() {

	mux := http.NewServeMux()
	static.AppendStaticAssetHandlersWithPrefix(mux, static_fs, "/static")

	// Assets are served under "/static" so include that prefix in the URIs
	// that rewrite appends to the HTML.
	opts := &rewrite.AppendResourcesOptions{
		Stylesheets: []string{"/static/css/app.css"},
		JavaScript:  []string{"/static/javascript/app.js"},
		// AppendJavaScriptAtEOF: true, // add scripts at the end of the document rather than in <head>
	}

	handler := rewrite.AppendResourcesHandler(mux, opts)
	http.ListenAndServe("localhost:8080", handler)
}

See also

  • bootstrap, which uses this package to serve embedded Bootstrap assets and the rewrite package to reference them.
  • rewrite
  • rollup

Documentation

Overview

Package static provides handlers for serving static assets from an `fs.FS` instance and for appending references to those assets to HTML output.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendStaticAssetHandlers

func AppendStaticAssetHandlers(mux *http.ServeMux, static_fs fs.FS) error

AppendStaticAssetHandlers registers a handler on 'mux' for each file in 'static_fs'.

func AppendStaticAssetHandlersWithPrefix

func AppendStaticAssetHandlersWithPrefix(mux *http.ServeMux, static_fs fs.FS, prefix string) error

AppendStaticAssetHandlersWithPrefix registers a handler on 'mux' for each file in 'static_fs', with 'prefix' prepended to the path of each file.

func StaticAssetsHandler

func StaticAssetsHandler(static_fs fs.FS) (http.Handler, error)

StaticAssetsHandler returns an `http.Handler` that serves the files in 'static_fs'.

func StaticAssetsHandlerWithPrefix

func StaticAssetsHandlerWithPrefix(static_fs fs.FS, prefix string) (http.Handler, error)

StaticAssetsHandlerWithPrefix returns an `http.Handler` that serves the files in 'static_fs' after removing 'prefix' from the request path.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL