fileserver

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2020 License: MIT Imports: 13 Imported by: 0

README

Simple golang file server

Release version Project language Build Status Coverage Go Report License

This package provides basic file server functionality with:

  • In memory caching with TTL and limits (like maximal cached files count and maximal file size)
  • Overridable error handlers
  • "Index" file serving (like index nginx directive)
  • Redirection to the "parent" directory, when index file requested
  • "Allowed methods" list

Most use-case is SPA assets serving.

Example

fs, err := fileserver.NewFileServer(fileserver.Settings{
    FilesRoot:               "./web",
    IndexFileName:           "index.html",
    ErrorFileName:           "__error__.html",
    RedirectIndexFileToRoot: true,
    AllowedHTTPMethods:      []string{http.MethodGet},
    CacheEnabled:            true,
    CacheTTL:                time.Second * 5,
    CacheMaxFileSize:        1024 * 64, // 64 KiB
    CacheMaxItems:           512,
})

if err != nil {
    log.Fatal(err)
}

log.Fatal(http.ListenAndServe(":9000", fs))

To run this example execute go run . in ./examples directory.

More information can be found in the godocs: http://godoc.org/github.com/avto-dev/go-simple-fileserver

Testing

For package testing we use built-in golang testing feature and docker-ce + docker-compose as develop environment. So, just write into your terminal after repository cloning:

$ make test

Changelog

Release date Commits since latest release

Changes log can be found here.

Support

Issues Issues

If you will find any package errors, please, make an issue in current repository.

License

This is open-sourced software licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorHandlerFunc

type ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, fs *FileServer, errorCode int) (doNotContinue bool)

ErrorHandlerFunc is used as handler for errors processing. If func return `true` - next handler will be NOT executed.

func JSONErrorHandler

func JSONErrorHandler() ErrorHandlerFunc

JSONErrorHandler respond with simple json-formatted response, if json format was requested (defined in `Accept` header).

func StaticHTMLPageErrorHandler

func StaticHTMLPageErrorHandler() ErrorHandlerFunc

StaticHTMLPageErrorHandler allows to use user-defined local file with HTML for error page generating.

type ErrorPageTemplate

type ErrorPageTemplate string

ErrorPageTemplate is error page template in string representation. Is allowed to use basic "replacing patterns" like `{{ code }}` or `{{ message }}`

func (ErrorPageTemplate) Build

func (t ErrorPageTemplate) Build(errorCode int) string

Build makes registered patterns replacing.

func (ErrorPageTemplate) String

func (t ErrorPageTemplate) String() string

String converts template into string representation.

type FileServer

type FileServer struct {
	// Server settings (some of them can be changed in runtime).
	Settings Settings

	// Cacher instance.
	Cache cache.Cacher // nil, if caching disabled

	// If all error handlers fails - this content will be used as fallback for error page generating.
	FallbackErrorContent string

	// Error handlers stack.
	ErrorHandlers []ErrorHandlerFunc
	// contains filtered or unexported fields
}

FileServer is a main file server structure (implements `http.Handler` interface).

func NewFileServer

func NewFileServer(s Settings) (*FileServer, error)

NewFileServer creates new file server with default settings. Feel free to change default behavior.

func (*FileServer) CacheAvailable

func (fs *FileServer) CacheAvailable() bool

CacheAvailable checks cache availability.

func (*FileServer) ServeHTTP

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

ServeHTTP responds to an HTTP request.

type Settings

type Settings struct {
	// Directory path, where files for serving is located.
	FilesRoot string

	// File name (relative path to the file) that will be used as an index (like <https://bit.ly/356QeFm>).
	IndexFileName string

	// File name (relative path to the file) that will be used as error page template.
	ErrorFileName string

	// Respond "index file" request with redirection to the root (`example.com/index.html` -> `example.com/`).
	RedirectIndexFileToRoot bool

	// Allowed HTTP methods (eg.: `http.MethodGet`).
	AllowedHTTPMethods []string

	// Enables caching engine.
	CacheEnabled bool

	// Maximal data caching lifetime.
	CacheTTL time.Duration

	// Maximum file size (in bytes), that can be placed into the cache.
	CacheMaxFileSize int64

	// Maximum files count, that can be placed into the cache.
	CacheMaxItems uint32
}

Settings describes file server options.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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