Giter VIP home page Giter VIP logo

fs's Introduction

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")
	}))
}

fs's People

Contributors

qrtz avatar

Stargazers

metakeule avatar  avatar Dobrosław Żybort avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.