bootstrap

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2023 License: BSD-3-Clause Imports: 10 Imported by: 21

README

go-http-bootstrap

go-http-bootstrap is an HTTP middleware package for including Bootstrap (v5.0.0) 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 (
	"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)
}

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

`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
	Logger                *log.Logger
}

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

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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