Categorygithub.com/avto-dev/go-simple-fileserver
modulepackage
1.0.0
Repository: https://github.com/avto-dev/go-simple-fileserver.git
Documentation: pkg.go.dev

# 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.

# Packages

No description provided by the author
No description provided by the author

# Functions

JSONErrorHandler respond with simple json-formatted response, if json format was requested (defined in `Accept` header).
NewFileServer creates new file server with default settings.
StaticHTMLPageErrorHandler allows to use user-defined local file with HTML for error page generating.

# Structs

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

# Type aliases

ErrorHandlerFunc is used as handler for errors processing.
ErrorPageTemplate is error page template in string representation.