httpfileserver

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2023 License: MIT Imports: 11 Imported by: 0

README

httpfileserver

go report card godocs

Fork from https://github.com/schollz/httpfileserver . This branch supports deflate, brotli, zstd, gzip and other methods.

This is a drop-in replacement for the stdlib http.FileServer that automatically provides gzipping as well as serving from memory instead of from disk. This library wraps the stdlib http.FileServer so you get all the benefits of that code, while also providing gzipping and keeping track of bytes and storing served files from memory when they come available.

To use, you can just replace the http.Fileserver in your http.Handle or http.HandlerFunc with httpfileserver.New(route,directory). For example, to serve static assets, you can replace the std lib version

http.Handle("/static/", http.FileServer(http.Dir(".")))

with this version

http.Handle("/static/", httpfileserver.New("/static/", "."))

Usage

go get -u github.com/3JoB/httpfileserver

In order to serve files from a different directory other than specified by the route, you need to include the route when specifying a file server. For example, if you want to serve /static files from a local directory called /tmp/myassets then you can specify a new file server in the following:

http.Handle("/static/", httpfileserver.New("/static/", "/tmp/myassets"))

The route is in the handle as well as the instance of the file server so that it can trim it and then server from the directory as intended.

Example

see example

Benchmarks

Using the example in this repo I tested both the stdlib and this version for serving a file. This version is about 22% faster (since it is reading from memory) and automatically uses gzip when capable.

Using the Go stdlib (http.Handle("/", http.FileServer(http.Dir(".")))):

$ ab -n 20000 -H "Accept-Encoding: gzip,deflate" http://localhost:1113/main.go # stdlib
...
HTML transferred:       4640000 bytes
Requests per second:    3575.56 [#/sec] (mean)
...

Using this library http.Handle("/new/", httpfileserver.New("/new", ".")):

$ ab -n 20000 -H "Accept-Encoding: gzip,deflate" http://localhost:1113/new/main.go # this lib
...
HTML transferred:       3680000 bytes
Requests per second:    4544.44 [#/sec] (mean)
...

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileServer

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

func New

func New(route, dir string, options ...Option) *FileServer

New returns a new file server that can handle requests for files using an in-memory store with gzipping

func (*FileServer) Delete added in v0.4.0

func (fs *FileServer) Delete(key string) error

func (*FileServer) Flush added in v0.4.0

func (fs *FileServer) Flush() error

Flush clears all data from cache

func (*FileServer) Handle

func (fs *FileServer) Handle() http.HandlerFunc

Handle gives a handlerfunc for the file server

func (*FileServer) ServeHTTP

func (fs *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is the server of the file server

type Option

type Option func(fs *FileServer)

Option is the type all options need to adhere to

func OptionMaxBytes

func OptionMaxBytes(optionMaxBytesPerFile int) Option

OptionMaxBytes sets the maximum number of bytes per file to cache, the default is 10 MB

func OptionNoCache

func OptionNoCache(disable bool) Option

OptionNoCache disables the caching

Directories

Path Synopsis
example
gin

Jump to

Keyboard shortcuts

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