Skip to content
/ fs Public

Package fs Simple wrapper for net/http.FileServer. Provides helper function to configure the behavior of the server

License

Notifications You must be signed in to change notification settings

qrtz/fs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 

Repository files navigation

fs

Package fs is a simple wrapper for net/http.FileServer. Provides helper functions to configure the behavior of the server

go get -u github.com/qrtz/fs

Features

  • Set Custom Error Handlers
  • Set Custom Index pages
  • Enable or disable directory listing. Default disabled
// Set http error handler
// handler with code=0 is used as default handler
func (f *FileServer) ErrorHandler(code int, fn ErrorHandlerFunc)
// Set autoindex to indicate whether to allow directory listing
func (f *FileServer) AutoIndex(autoIndex bool)
// Set index page. Defaults to "index.html"
func (f *FileServer) Index(index ...string)
// Set a handler that remove the prefix from
// the request URL before attempting to serve the request
func (f *FileServer) StripPrefix(prefix string)
// FileServer returns a handler that serves HTTP requests
// with the contents of the directory rooted at root
// calling each option on the server before returning it
func New(root string, options ...func(*FileServer)) http.Handler 

Usage

package main

import (
	"flag"
	"github.com/qrtz/fs"
	"html/template"
	"net/http"
)

func main() {
	addr := flag.String("addr", ":8080", "Server HTTP address")
	root := flag.String("r", ".", "Base directory path for static contents")

	flag.Parse()

	notfound := template.Must(template.New("404").Parse(`<!DOCTYPE html><html><head><title>[404]</title></head><body><h1>[404]</h1></body></html>`))

	http.ListenAndServe(*addr, fs.New(*root, func(f *fs.FileServer) {
		// Set the default error handler
		f.ErrorHandler(0, func(w http.ResponseWriter, r *http.Request, code int) {
			// We can set the content type base on the request
			w.Header().Set("Content-Type", "text/html; charset=utf-8")

			// We control the error code to return
			w.WriteHeader(code)

			// We can now respond with our awesome error page.
			notfound.Execute(w, nil)
		})

		// Turn directory listing on. This is off by default
		f.AutoIndex(true)

		// Overwrite the default index page with one or more possible choices
		f.Index("index.html", "default.html", "index.txt")
	}))
}

About

Package fs Simple wrapper for net/http.FileServer. Provides helper function to configure the behavior of the server

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages