handlers

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2021 License: MIT Imports: 9 Imported by: 2

Documentation

Overview

Package handlers provides http.Handler(s) which can execute pages defined in the webgear package. All data is returned gzip compressed.

Usage is as follows:

// Create new handlers.Mux object with an option to tell clients not to cache results.
h := handlers.New(handlers.DoNotCache())

// Serve all files from the the binary working directory and below it (recursively) that have
// the file extensions listed.
h.ServeFilesWorkingDir([]string{".css", ".jpg", ".svg", ".png"})

// Create a *html.Doc object that we want to serve from "/".
index, err := index.New(conf)
if err != nil {
	panic(err)
}

// Attach that object to /.
h.MustHandle("/", index)

// Serve the content using the http.Server.
server := &http.Server{
	Addr:           fmt.Sprintf(":%d", *port),
	Handler:        h.ServerMux(),
	ReadTimeout:    10 * time.Second,
	WriteTimeout:   10 * time.Second,
	MaxHeaderBytes: 1 << 20,
}

log.Printf("http server serving on :%d", *port)

log.Fatal(server.ListenAndServe())

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mux

type Mux struct {
	// contains filtered or unexported fields
}

Mux allows building a http.ServeMux for use in serving html.Doc object output.

func New

func New(options ...Option) *Mux

New creates a new instance of Mux.

func (*Mux) HTTPHandler

func (m *Mux) HTTPHandler(pattern string, handler http.Handler)

HTTPHandler registers a standard http.Handler for the pattern on the http.ServeMux.

func (*Mux) Handle

func (m *Mux) Handle(pattern string, doc *html.Doc) error

Handle registers the doc for a given pattern. If a handler already exists for pattern, Handle panics. All handles will be gzip compressed by default.

func (*Mux) MustHandle

func (m *Mux) MustHandle(pattern string, doc *html.Doc) *Mux

MustHandle is like Handle() except an error causes a panic. Returns the *Mux object so these can be chained.

func (*Mux) ServeFilesFrom

func (m *Mux) ServeFilesFrom(dir, root string, exts []string)

ServeFilesFrom will serve all files with the following file extensions that are in the directory dir. It will never serve ., .. or .go files. All files are served out of the /{{root}}/ path. If root == "", /static/ will be used. Note: if called multiple times or used with ServeFilesWorkingDir(), if there are two directories within the top level directory with the same name and same root, you will get a collision that will cause a panic. Aka, if you do: ServeFilesFrom("/some_dir", "", []string{".img"}}) and ServeFilesFrom("/another_dir", "", []string{".img"}}), where /some_dir and /another_dir both contain img/, this will panic.

func (*Mux) ServeFilesWorkingDir

func (m *Mux) ServeFilesWorkingDir(exts []string)

ServeFilesWorkingDir will serve all files with the following file extensions that are in the working directory or in any directory lower in the tree. It will never serve ., .. or .go files. These files are all served from pattern. All files are served out of the /static/ path.

func (*Mux) ServerMux

func (m *Mux) ServerMux() http.Handler

ServerMux returns an http.ServerMux wrapped in various handlers. Use this with http.Server{} to serve the content.

type Option

type Option func(m *Mux)

Option is an optional argument to the New constructor.

func DoNotCache

func DoNotCache() Option

DoNotCache tells the brower not to cache content. This is extremely useful when you are doing development.

func DoNotCompress

func DoNotCompress() Option

DoNotCompress prevents the muxer from gzip compressing content.

Jump to

Keyboard shortcuts

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