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 ¶
- type Mux
- func (m *Mux) HTTPHandler(pattern string, handler http.Handler)
- func (m *Mux) Handle(pattern string, doc *html.Doc) error
- func (m *Mux) MustHandle(pattern string, doc *html.Doc) *Mux
- func (m *Mux) ServeFilesFrom(dir, root string, exts []string)
- func (m *Mux) ServeFilesWorkingDir(exts []string)
- func (m *Mux) ServerMux() http.Handler
- type Option
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 (*Mux) HTTPHandler ¶
HTTPHandler registers a standard http.Handler for the pattern on the http.ServeMux.
func (*Mux) Handle ¶
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 ¶
MustHandle is like Handle() except an error causes a panic. Returns the *Mux object so these can be chained.
func (*Mux) ServeFilesFrom ¶
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 ¶
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.
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.