Documentation
¶
Overview ¶
`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 JavaScript files and related assets.
Example
package main
import (
"github.com/aaronland/go-http-bootstrap"
"log"
"net/http"
)
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)
}
All of the Bootstrap files in the static/css(static/css) and static/javascript(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.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendAssetHandlers ¶
func AppendAssetHandlers(mux *http.ServeMux, opts *BootstrapOptions) error
Append all the files in the net/http FS instance containing the embedded Bootstrap assets to an *http.ServeMux instance.
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 bool
Prefix string
}
BootstrapOptions provides a list of JavaScript and CSS link to include with HTML output.
func DefaultBootstrapOptions ¶
func DefaultBootstrapOptions() *BootstrapOptions
Return a *BootstrapOptions struct with default paths and URIs.
func (*BootstrapOptions) EnableJavascript ¶ added in v0.3.0
func (opts *BootstrapOptions) EnableJavascript()
