web

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: BSD-3-Clause Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AccessLoggingMiddleware

func AccessLoggingMiddleware(logger logs.Logger) func(http.Handler) http.Handler

Access logging middleware logs incoming HTTP requests

func CatchAll

func CatchAll(_ *http.Request) bool

Defined for readability purposes, to make it explicit that the handlers intents to catch all requests. Used for handling 404s. Equivalent to not using any RequestMatcher in routes.Handle, as this will match any request.

func DisallowedRobotsTXT

func DisallowedRobotsTXT(routes []string) string

Generate robots.txt with disallowed routes

func FileServer

func FileServer(dirPath, httpPrefix string) http.Handler

func IPAddressFromRequest

func IPAddressFromRequest(r *http.Request, useXForwardedFor bool) net.IP

func MatchMethodConnect added in v0.0.6

func MatchMethodConnect(r *http.Request) bool

func MatchMethodDelete added in v0.0.6

func MatchMethodDelete(r *http.Request) bool

func MatchMethodGet added in v0.0.1

func MatchMethodGet(r *http.Request) bool

Utility request method matchers defined for convenience and conciseness.

func MatchMethodHead added in v0.0.6

func MatchMethodHead(r *http.Request) bool

func MatchMethodOptions added in v0.0.6

func MatchMethodOptions(r *http.Request) bool

func MatchMethodPatch added in v0.0.6

func MatchMethodPatch(r *http.Request) bool

func MatchMethodPost added in v0.0.1

func MatchMethodPost(r *http.Request) bool

func MatchMethodPut added in v0.0.6

func MatchMethodPut(r *http.Request) bool

func MatchMethodTrace added in v0.0.6

func MatchMethodTrace(r *http.Request) bool

func MustParseHTMLTemplate

func MustParseHTMLTemplate(commonTmpls []string, path string) *template.Template

func MustParseHTMLTemplateFS added in v0.0.3

func MustParseHTMLTemplateFS(fsys fs.FS, commonTmpls []string, path string) *template.Template

func NewServerWithDefaults

func NewServerWithDefaults(h http.Handler, port int) *http.Server

func PanicRecoveryMiddleware

func PanicRecoveryMiddleware(onPanic PanicHandler) func(h http.Handler) http.Handler

Panic recovery middleware logs the recovered error and executes the onPanic callback function.

func PermanentRedirectHandler

func PermanentRedirectHandler(toURL string) http.HandlerFunc

func ReadAndServeFile

func ReadAndServeFile(path string) http.HandlerFunc

func RenderHTMLTemplate

func RenderHTMLTemplate(
	w http.ResponseWriter,
	statusCode int,
	t *template.Template,
	tname string,
	data map[string]any,
)

Will panic when an error occurs during rendering, make sure you handle panic recovery in a middleware.

func RunServer

func RunServer(s *http.Server, onShutown func() error) error

Listens for incoming connections and await interrupt signal (or server error) for graceful shutdown

func ServeMonochromeFaviconPNG

func ServeMonochromeFaviconPNG(c color.Color) http.HandlerFunc

Generate and serve a monochrome favicon PNG. Useful for temporary favicon when prototyping.

func ServeRaw

func ServeRaw(v []byte) http.HandlerFunc

func ServeRobotsTXT added in v0.0.11

func ServeRobotsTXT(disallowedRoutes ...string) http.HandlerFunc

func ServeSitemapXML

func ServeSitemapXML(host string, routes ...string) http.HandlerFunc

func SitemapXML

func SitemapXML(host string, routes ...string) string

Generate sitemap XML

func VisitorHash

func VisitorHash(r *http.Request, checkXForwardedFor bool) string

func Wrap added in v0.0.2

func Wrap(h http.Handler, middleware func(http.Handler) http.Handler) http.Handler

Like append but for middleware functions.

Types

type JSONLDWebsiteSchema

type JSONLDWebsiteSchema struct {
	Context string `json:"@context"`
	Type    struct {
		Name string `json:"name"`
		URL  string `json:"url"`
	} `json:"@type"`
}

Website JSON+LD schema

type PanicHandler

type PanicHandler func(err any, w http.ResponseWriter, r *http.Request)

type RequestMatcher

type RequestMatcher func(r *http.Request) bool

RequestMatcher represents a function that reports whether a HTTP request matches a certain criteria.

For example: MatchPath and MatchMethod allow you to match a HTTP request with a certain path and method request to a handler.

func MatchDomainName added in v0.0.6

func MatchDomainName(s string) RequestMatcher

Matches a certain domain name (SLD + TLD).

func MatchHeader added in v0.0.6

func MatchHeader(k, v string) RequestMatcher

Matches a certain HTTP header key and value.

func MatchHostname added in v0.0.6

func MatchHostname(s string) RequestMatcher

Matches a certain host. Ignores port if present.

func MatchMethod

func MatchMethod(methods ...string) RequestMatcher

Checks if the request method is one of the provided ones.

func MatchPath

func MatchPath(path string) RequestMatcher

Checks if the request URL path is the same as the provided one.

func MatchPathPrefix

func MatchPathPrefix(prefix string) RequestMatcher

Checks if the request URL path starts with the provided prefix.

func MatchSubdomain added in v0.0.6

func MatchSubdomain(s string) RequestMatcher

Matches a certain subdomain.

NB: Treats ccSLD just as normal SLDs (cf. unit test)

type ResponseStatusRecorder added in v0.1.5

type ResponseStatusRecorder struct {
	http.ResponseWriter
	StatusCode int
}

func (*ResponseStatusRecorder) WriteHeader added in v0.1.5

func (srec *ResponseStatusRecorder) WriteHeader(statusCode int)

type Route

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

Route represents a HTTP handler with request matchers.

func (*Route) Match

func (rh *Route) Match(r *http.Request) bool

If all matchers yield true, this function returns true. If there are no matchers provided, true is also returned.

type Routes

type Routes []*Route

Routes represents a list of routes.

func (*Routes) Handle

func (rhs *Routes) Handle(h http.Handler, matchers ...RequestMatcher)

func (Routes) RequestHandler

func (rhs Routes) RequestHandler(r *http.Request) http.Handler

func (Routes) ServeHTTP

func (rhs Routes) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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