fileserver

package
v4.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package fileserver contains static files server implementation.

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

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

	// Error handlers stack.
	ErrorHandlers []ErrorHandlerFunc
}

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

Settings describes file server options.

Jump to

Keyboard shortcuts

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