dandler

package module
v0.0.0-...-0217788 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2021 License: MIT Imports: 23 Imported by: 0

README

dandler - dak's handlers

Build Status reference Go Report Card

My handlers I've used throughout a number of packages.

Documentation

Index

Constants

View Source
const (
	ForceHTTP      = 1 << iota // force http as the redirect target
	ForceHTTPS                 // force https as the redirect target
	ForceHost                  // force the given hostname as the redirect target
	ForcePort                  // force a given port for the redirect target
	ForceTemporary             // Use a 302 for the redirect
)

These constants are to be used with the Canocial Host Handler.

View Source
const GolangGopherASCII = `` /* 3969-byte string literal not displayed */

GolangGopherASCII is a ascii drawing of a golang gopher for use in ASCIIHeader()

View Source
const Megabyte int = 1 << 20

Variables

This section is empty.

Functions

func ASCIIHeader

func ASCIIHeader(key, value, padChar string, child http.Handler) http.Handler

ASCIIHeader adds a multiline header to the response - a header for each line. It is used to add ascii art to the header of a response.

Both key and value can be a multiline string = and will be broken up as needed. Key will be repeated for the duration of value for each header line.

You need to make sure both key and value have a consistent length after white space trimming on each line, as HTTP will do that anyway. If you don't, this will split on newlines, trim spaces, then right pad the key with underscores so things line up anyway.

func CanonicalHost

func CanonicalHost(url string, options int, childHandler http.Handler) http.Handler

CanonicalHost returns a http.Handler that redirects to the canocial host based on certain options. 0 may be passed for options if so desired, or provided bits can be forced on the client with a redirect.

func ContentType

func ContentType(logger *log.Logger, basePath string) http.Handler

ContentType serves a given file back to the requester, and determines content type by algorithm only. It does not use the file's extension to determine the content type.

func DirSplit

func DirSplit(logger *log.Logger, basepath string, done <-chan struct{}, folder, other http.Handler) http.Handler

DirSplit takes two child handlers - one for the directories, one for the other locations. It then watches a directory - and sub directories - and any request that would match a directory relative to that path is routed to the handler for directories. All other requests are routed to the other handler.

func Expires

func Expires(maxAge time.Duration, child http.Handler) http.Handler

func ExpiresRange

func ExpiresRange(min, max time.Duration, child http.Handler) http.Handler
func Header(name, msg string, handler http.Handler) http.Handler

Header returns a handler that adds the given handler to the response.

func Index

func Index(logger *log.Logger, basepath string, done <-chan struct{}, templ *template.Template) http.Handler

Index lists all files in a directory, and passes them to template execution to build a directory listing. It also creates a list of directories and passes those - but symlinks to directories are not handled.

func Internal

func Internal(logger *log.Logger, fs http.FileSystem) http.Handler

Internal serves a static, in memory filesystem. The filesystem to be served should have been generated with embed or similar..

Note - this is likely a duplicate of http.FileServer. It will likely be removed.

func ResponseCode

func ResponseCode(code int, msg string, args ...interface{}) http.Handler

ResponseCode responds with an error to any request with the error code and message provided.

func Split

func Split(root, more http.Handler) http.Handler

Split allows the routing of one handler at /, and another at all locations below /.

func Success

func Success(msg string) http.Handler

Success returns a handler that responds to every request with a 200 and always the same message.

func ThumbCache

func ThumbCache(logger *log.Logger, targetWidth, targetHeight int, cacheSize int64,
	rawImageDirectory, cacheName, thumbnailExtension string) http.Handler

ThumbCache returns a handler that serves thumbnails from GroupCache. Thumbnails are generated when needed by GroupCache.

func Thumbnail

func Thumbnail(logger *log.Logger, targetWidth, targetHeight int,
	rawImageDirectory, thumbnailDirectory, thumbnailExtension string) http.Handler

Thumbnail returns a handler that generates a thumbnail of the given size of each image, stores it in the specified location, and serves back the thumbnails upon request. Thumbnails are generated when needed. File caching is used to decrease thumbnail generation.

Types

type IndexData

type IndexData struct {
	Files []string
	Dirs  []string
}

This is the struct passed to the template used with an IndexHandler

type MethodHandler

type MethodHandler map[string]http.Handler

MethodHandler splits requests to the Handler across the Method map.

func (MethodHandler) ServeHTTP

func (h MethodHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP makes MethodHandler a http.Handler.

type RedirectURIHandler

type RedirectURIHandler struct {
	TLS       bool
	Domain    string
	PrefixURI string
	Code      int
}

RedirectURIHandler creates a handler that responds with a redirect to another domain. The URI is preserved, optionally with a prefix added. https is included or not, based on the value of TLS.

func (*RedirectURIHandler) ServeHTTP

func (rh *RedirectURIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP makes RedirectURIHandler a http.Handler.

Jump to

Keyboard shortcuts

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