bootstrap

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: 9 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/bootstrap

go-http/v4/bootstrap is an HTTP middleware package for including Bootstrap (v5.3.3) assets in web applications.

Documentation

Go Reference

go-http-bootstrap is an HTTP middleware package for including Bootstrap.js assets in web applications. It exports two principal methods:

  • bootstrap.AppendAssetHandlers(*http.ServeMux) which is used to append HTTP handlers to a http.ServeMux instance for serving Bootstrap CSS and JavaScript files, and related assets.
  • bootstrap.AppendResourcesHandler(http.Handler, *BootstrapOptions) which is used to rewrite any HTML produced by previous handler to include the necessary markup to load Bootstrap

This package doesn't specify any code or methods for how Bootstrap.js is used. It only provides method for making Bootstraps available to existing applications.

Example

package main

import (
	"log"
	"net/http"

	"github.com/aaronland/go-http/v5/bootstrap"
)

func Handler() http.Handler {

	index := `
<!doctype html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Bootstrap</title>
  </head>

  <body>
   <div class="card">
   	<h1 class="card-header">Card header</h1>
	<div class="card-body">Card body</div>
	<div class="card-footer">Card footer</div>
   </div>
  </body>
</html>`

	fn := func(rsp http.ResponseWriter, req *http.Request) {

		rsp.Write([]byte(index))
	}

	return http.HandlerFunc(fn)
}

func main() {

	mux := http.NewServeMux()
	
	idx_handler := Handler()

	bootstrap_opts := bootstrap.DefaultBootstrapOptions()
	idx_handler = bootstrap.AppendResourcesHandler(idx_handler, bootstrap_opts)

	mux.Handle("/", idx_handler)

	bootstrap.AppendAssetHandlers(mux)

	endpoint := "localhost:8080"
	log.Printf("Listening for requests on %s\n", endpoint)

	http.ListenAndServe(endpoint, mux)
}

Error handling omitted for brevity.

You can see an example of this application by running the cmd/example application. You can do so by invoking the example Makefile target. For example:

$> make example
go run -mod vendor cmd/example/main.go 
2021/05/05 13:54:07 Listening for requests on localhost:8080

The when you open the URL http://localhost:8080 in a web browser you should see the following:

Notes

All of the Bootstrap files in the static/css and static/javascript are registered with your http.ServeMux instance when you call bootstrap.AppendAssetHandlers but by default only the css/bootstrap.min.css is included in the list of CSS and Javascript resources to append to HTML content when you call the bootstrap.DefaultBootstrapOptions() method. If there are other Bootstrap-related files you need to access in your application you will need to add them to the BootstrapOptions.CSS and Bootstrap.JS properties manually.

See also

Documentation

Overview

Package bootstrap provides middleware and HTTP handlers for serving embedded Bootstrap assets and appending references to them to HTML output.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendAssetHandlers

func AppendAssetHandlers(mux *http.ServeMux, opts *BootstrapOptions) error

AppendAssetHandlers appends handlers for the embedded Bootstrap assets to 'mux'. If 'opts.RollupAssets' is true multiple JavaScript or CSS assets are served as a single minified rollup file.

func AppendResourcesHandler

func AppendResourcesHandler(next http.Handler, opts *BootstrapOptions) http.Handler

AppendResourcesHandler will rewrite any HTML produced by previous handler to include the necessary markup to load Bootstrap JavaScript files and related assets.

Types

type BootstrapOptions

type BootstrapOptions struct {
	// A list of relative Bootstrap Javascript URLs to append as resources in HTML output.
	JS []string
	// A list of relative Bootstrap CSS URLs to append as resources in HTML output.
	CSS []string
	// AppendJavaScriptAtEOF is a boolean flag to append JavaScript markup at the end of an HTML document
	// rather than in the <head> HTML element. Default is false
	AppendJavaScriptAtEOF bool
	// RollupAssets is a boolean flag to serve multiple JS and CSS assets as single minified "rollup" files.
	RollupAssets bool
	// Prefix is an optional URL path prefix to prepend to all asset URLs.
	Prefix string
}

BootstrapOptions provides a list of JavaScript and CSS links to include with HTML output.

func DefaultBootstrapOptions

func DefaultBootstrapOptions() *BootstrapOptions

DefaultBootstrapOptions returns a *BootstrapOptions struct with default paths and URIs.

func (*BootstrapOptions) EnableJavascript

func (opts *BootstrapOptions) EnableJavascript()

EnableJavascript appends the Bootstrap JavaScript bundle to the list of JavaScript URLs in 'opts'.

Directories

Path Synopsis
Package static contains the embedded Bootstrap CSS and JavaScript assets.
Package static contains the embedded Bootstrap CSS and JavaScript assets.

Jump to

Keyboard shortcuts

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