package httpx

import "git.sr.ht/~pingoo/stdx/httpx"

Index

Constants

const (
	HeaderLastModified            = "Last-Modified"
	HeaderContentType             = "Content-Type"
	HeaderContentEncoding         = "Content-Encoding"
	HeaderCacheControl            = "Cache-Control"
	HeaderContentLength           = "Content-Length"
	HeaderETag                    = "ETag"
	HeaderServer                  = "Server"
	HeaderIfModifiedSince         = "If-Modified-Since"
	HeaderExpires                 = "Expires"
	HeaderIfNoneMatch             = "If-None-Match"
	HeaderAccept                  = "Accept"
	HeaderUserAgent               = "User-Agent"
	HeaderAuthorization           = "Authorization"
	HeaderAltSvc                  = "Alt-Svc"
	HeaderStrictTransportSecurity = "Strict-Transport-Security"
	HeaderContentDisposition      = "Content-Disposition"
	HeaderReferer                 = "Referer"
	HeaderDoNotTrack              = "Dnt"
	HeaderAcceptLanguage          = "Accept-Language"
)
const (
	CacheControlNoCache = "private, no-cache, no-store, must-revalidate" // "no-cache, no-store, no-transform, must-revalidate, private, max-age=0"
	CacheControlDynamic = "public, no-cache, must-revalidate"            // TODO https://web.dev/http-cache/, https://web.dev/love-your-cache/
	// CacheControlDynamic    = "max-age=0, must-revalidate, public" // TODO https://web.dev/http-cache/, https://web.dev/love-your-cache/
	// CacheControlNoCache       = "no-cache, no-store, no-transform, must-revalidate" // "no-cache, no-store, no-transform, must-revalidate, private, max-age=0"
	CacheControl30Seconds = "public, max-age=30, must-revalidate"
	CacheControl1Minute   = "public, max-age=60, must-revalidate"
	CacheControl5Minutes  = "public, max-age=300, stale-while-revalidate=30"
	CacheControl10Minutes = "public, max-age=600, stale-while-revalidate=60"
	CacheControl15Minutes = "public, max-age=900, stale-while-revalidate=90"
	CacheControl30Minutes = "public, max-age=1800, stale-while-revalidate=180"
	CacheControl1Hour     = "public, max-age=3600, stale-while-revalidate=360"
	CacheControl1Day      = "public, max-age=86400, stale-while-revalidate=600"
	CacheControl1Week     = "public, max-age=604800, stale-while-revalidate=600"
	CacheControl1Month    = "public, max-age=2592000, stale-while-revalidate=600"
	CacheControlImmutable = "public, max-age=31536000, immutable"
)

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching

const (
	MediaTypeText = "text/plain"
	MediaTypeXml  = "application/xml"
	MediaTypeJson = "application/json"

	MediaTypeHtmlUtf8 = "text/html; charset=utf-8"
	MediaTypeTextUtf8 = "text/plain; charset=utf-8"

	MediaTypeJsonFeed = "application/feed+json"
	MediaTypeRSS      = "application/rss+xml"
	MediaTypeAtom     = "application/atom+xml"

	MediaTypeAudio = "audio"
	MediaTypeVideo = "video"
	MediaTypePNG   = "image/png"
	MediaTypeJPEG  = "image/jpeg"
	MediaTypeSVG   = "image/svg+xml"

	// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
	// alternative value from the 'file' command line program: text/x-shellscript
	MediaTypeShellScript = "application/x-sh"
)
const (
	CacheControl30SecondsCdnOnly = "public, max-age=0, s-max-age=30, must-revalidate"
)

Variables

var ErrDir = errors.New("path is a folder")
var ErrInternalError = errors.New("Internal Server Error")
var ErrInvalidPath = errors.New("path is not valid")

Functions

func DefaultClient

func DefaultClient() *http.Client

DefaultClient returns an HTTP client with sane defaults

func ServerInternalErrorPlaintext

func ServerInternalErrorPlaintext(w http.ResponseWriter, message *string)

func WebappHandler

func WebappHandler(folder fs.FS, config *WebappHandlerConfig) (handler func(w http.ResponseWriter, r *http.Request), err error)

WebappHandler is an http.Handler that is designed to efficiently serve Single Page Applications. if a file is not found, it will return notFoundFile (default: index.html) with the stauscode statusNotFound WebappHandler sets the correct ETag header and cache the hash of files so that repeated requests to files return only StatusNotModified responses WebappHandler returns StatusMethodNotAllowed if the method is different than GET or HEAD

Types

type CacheRule

type CacheRule struct {
	Regexp string

	CacheControl string
	// contains filtered or unexported fields
}

type WebappHandlerConfig

type WebappHandlerConfig struct {
	// default: index.html
	NotFoundFile string
	// default: 200
	NotFoundStatus int
	// default: public, no-cache, must-revalidate
	NotFoundCacheControl string
	// default: ".js", ".css", ".woff", ".woff2"
	Cache []CacheRule
}

Source Files

client.go const.go error.go webapphandler.go webapphandler_old.go

Directories

PathSynopsis
httpx/autocertpg
httpx/corsPackage cors is net/http handler to handle CORS related requests as defined by http://www.w3.org/TR/cors/
httpx/example
httpx/hostrouter
httpx/middlewarex
Version
v0.0.0-20240218134121-094174641f6e (latest)
Published
Feb 18, 2024
Platform
linux/amd64
Imports
14 packages
Last checked
4 months ago

Tools for package owners.