Documentation ¶
Index ¶
- Variables
- func AtoiAnyDef(s interface{}, vdef int) int
- func AtoiDef(s string, vdef int) int
- func BuildHandlerChain(hl HandlerList) http.Handler
- func CheckValidToken(t string) error
- func CtxIsHTTPS(ctx context.Context) bool
- func CtxWithCombinedPath(ctx context.Context, pathType string, resourcePath string) context.Context
- func CtxWithHTTPS(ctx context.Context) context.Context
- func CtxWithHTTPSHandler(next http.Handler) http.Handler
- func CtxWithPageMeta(ctx context.Context, pageMeta *PageMeta) context.Context
- func DefaultPathExpand(p string) []string
- func DefaultRedirects(w http.ResponseWriter, r *http.Request)
- func GuessMIMEType(p string) string
- func HTTPError(w http.ResponseWriter, r *http.Request, err error, publicMessage string, ...) error
- func IsPEMCertData(v []byte) bool
- func IsPEMKeyData(v []byte) bool
- func JSONMarshalResponse(w http.ResponseWriter, status int, v interface{}) error
- func JSONUnmarshalRequest(r *http.Request, v interface{}) error
- func LoadKeyPairAuto(cert, key string) (tls.Certificate, error)
- func LogToSyslog(prgName string) error
- func NewDefaultDBBlogSite()
- func NewDefaultDBOnlySite()
- func NewDefaultFSOnlySite()
- func NewDefaultRedirects() http.Handler
- func NewHTTPFuncFS(f func(name string) (http.File, error)) http.FileSystem
- func ParseMetaAndText(b []byte, meta interface{}) (tmpltext []byte, e error)
- func ReadJSON(r *http.Request, va interface{}) error
- func TmplIncludeAll(fs http.FileSystem, t *template.Template) error
- func TmplIncludeNode(fs http.FileSystem, t *template.Template, node parse.Node) error
- func WalkRenderAndIndex(bodyFs http.FileSystem, fileExts []string, renderer Renderer, ...) error
- func WriteJSON(w http.ResponseWriter, va interface{}, code int)
- type AssetDirFunc
- type AssetFunc
- type AssetInfoFunc
- type BodyTemplater
- type ChainedHandler
- type CtxMapHandler
- type DebugResponseWriterCloser
- type DefaultCacheHeadersHandler
- type DummyResponseWriterCloser
- type FileCombiner
- type GoBodyTemplater
- type GzipHandler
- type GzipResponseWriterCloser
- type HTTPBindataFile
- type HTTPBindataFs
- type HTTPBytesFile
- type HTTPFileInfo
- type HTTPFuncFS
- type HTTPStackedFileSystem
- type HandlerList
- type MarkdownBodyTemplater
- type PageIndex
- type PageMeta
- type PathParser
- func (pp *PathParser) ArgInt(n int) (ret int)
- func (pp *PathParser) ArgIntDef(n int, defaultVal int) (ret int)
- func (pp *PathParser) ArgString(n int) (ret string)
- func (pp *PathParser) ArgStringDef(n int, defaultVal string) (ret string)
- func (pp *PathParser) ArgUUIDB64(n int) gouuidv6.UUIDB64
- func (pp *PathParser) Match(path string, pattern string) bool
- type RenderHandler
- type Renderer
- type ResponseWriterCloser
- type StaticFileServer
Constants ¶
This section is empty.
Variables ¶
var ErrExceededLimit = fmt.Errorf("exceeded limit")
var ErrNotFound = errors.New("not found")
var ErrNotJSONContent = errors.New("cannot parse, not json content")
var VALID_TOKEN_REGEXP = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`)
Functions ¶
func AtoiAnyDef ¶
func BuildHandlerChain ¶
func BuildHandlerChain(hl HandlerList) http.Handler
BuildHandlerChain takes a HandlerList (slice of http.Handler instances) and finds any that implement ChainedHandler and calls SetNextHandler() with the appropriate input to form a chain. The return value is a handler that calls the other handlers in the correct sequence, taking into account the intended behavior of SetNextHandler.
func CheckValidToken ¶
func CtxIsHTTPS ¶
func CtxWithCombinedPath ¶
func CtxWithPageMeta ¶
func DefaultPathExpand ¶
func DefaultRedirects ¶
func DefaultRedirects(w http.ResponseWriter, r *http.Request)
func GuessMIMEType ¶
GuessMIMEType is a thin wrapper around mime.TypeByExtension(), but with some common sense defaults that are sometimes different/wrong on different platforms for no good reason.
func HTTPError ¶
func HTTPError(w http.ResponseWriter, r *http.Request, err error, publicMessage string, code int) error
Reads and reports an http error - does not expose anything to the outside world except a unique ID, which can be matched up with the appropriate log statement which has the details.
func IsPEMCertData ¶
IsPEMCertData returns true if v contains "-----BEGIN", "CERTIFICATE-----" and at least one newline.
func IsPEMKeyData ¶
IsPEMKeyData returns true if v contains "-----BEGIN", "PRIVATE KEY-----" and at least one newline.
func JSONMarshalResponse ¶
func JSONMarshalResponse(w http.ResponseWriter, status int, v interface{}) error
func JSONUnmarshalRequest ¶
func LoadKeyPairAuto ¶
func LoadKeyPairAuto(cert, key string) (tls.Certificate, error)
LoadKeyPairAuto looks at the parameters and loads the certificate(s) and key either from the path pointed to on disk or directly as PEM data. IsPEMKeyData and IsPEMCertData to detect key and cert data in PEM format. This is a simple wrapper around tls.X509KeyPair.
func LogToSyslog ¶
func NewDefaultDBBlogSite ¶
func NewDefaultDBBlogSite()
pages are in db but the rest is in fs? - figure out the variations here...
func NewDefaultDBOnlySite ¶
func NewDefaultDBOnlySite()
func NewDefaultFSOnlySite ¶
func NewDefaultFSOnlySite()
func NewDefaultRedirects ¶
NewDefaultRedirects returns a helper handler to normalize URLs by 301 redirecting common variations that would otherwise create duplicate URLs.
func NewHTTPFuncFS ¶
func ParseMetaAndText ¶
func TmplIncludeAll ¶
func TmplIncludeAll(fs http.FileSystem, t *template.Template) error
TmplIncludeAll - see https://stackoverflow.com/questions/20716726/golang-text-html-template-call-other-templates-with-dynamic-name/42969242#42969242
func TmplIncludeNode ¶
TmplIncludeNode - see https://stackoverflow.com/questions/20716726/golang-text-html-template-call-other-templates-with-dynamic-name/42969242#42969242
func WalkRenderAndIndex ¶
func WalkRenderAndIndex(bodyFs http.FileSystem, fileExts []string, renderer Renderer, pageIndex *PageIndex) error
WalkRenderAndIndex is a helper to glue together the Renderer and PageIndex, since these two are intentionally kept unaware of each other. This walks the file system and calls the Renderer for each file found, adding the approrpiate PageMeta to the PageIndex. FIXME: fileExts moves to some kinda of Options struct and we also need a way to regexp exclude, as well as specify removal of file extension.
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, va interface{}, code int)
FIXME de-duplicate between this and JSONMarshalResponse
Types ¶
type AssetDirFunc ¶
type BodyTemplater ¶
BodyTemplater reads the body of a page and returns a string with the appropriate Go template. Pages that are already Go templates can be returned as-is but this interface allows us to implement markdown or other languages by converting them to Go templates.
type ChainedHandler ¶
type ChainedHandler interface { http.Handler SetNextHandler(next http.Handler) (self ChainedHandler) }
type CtxMapHandler ¶
CtxMapHandler sets static items in the context during each request.
func NewCtxMapHandler ¶
func NewCtxMapHandler(ctxMap map[string]interface{}) *CtxMapHandler
func (*CtxMapHandler) ServeHTTP ¶
func (h *CtxMapHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
func (*CtxMapHandler) SetNextHandler ¶
func (h *CtxMapHandler) SetNextHandler(next http.Handler) ChainedHandler
type DebugResponseWriterCloser ¶
type DebugResponseWriterCloser struct { http.ResponseWriter // contains filtered or unexported fields }
DebugResponseWriterCloser
func DebugCtxWrap ¶
func DebugCtxWrap(w http.ResponseWriter, r *http.Request) (*DebugResponseWriterCloser, context.Context)
FIXME: figure out if we're going to keep the debug/dumping stuff in here or not - might be useful but needs think-through DebugCtxWrap is like GzipCtxWrap but instead of gzipping it implements debug logging that dumps full responses to the log. Use DumpRequest to dump the request in the same format.
func (*DebugResponseWriterCloser) Close ¶
func (w *DebugResponseWriterCloser) Close() error
Close dumps the response using log.Printf
func (*DebugResponseWriterCloser) DumpRequest ¶
func (w *DebugResponseWriterCloser) DumpRequest(r *http.Request)
DumpRequest is a helper to dump a request in the same format that Close() dumps the response
func (*DebugResponseWriterCloser) Flush ¶
func (w *DebugResponseWriterCloser) Flush()
func (*DebugResponseWriterCloser) Write ¶
func (w *DebugResponseWriterCloser) Write(p []byte) (int, error)
func (*DebugResponseWriterCloser) WriteHeader ¶
func (w *DebugResponseWriterCloser) WriteHeader(c int)
type DefaultCacheHeadersHandler ¶
type DefaultCacheHeadersHandler struct{}
TODO: we should add a good HTTPError here, it's pretty darned useful
func (*DefaultCacheHeadersHandler) ServeHTTP ¶
func (h *DefaultCacheHeadersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type DummyResponseWriterCloser ¶
type DummyResponseWriterCloser struct { http.ResponseWriter // contains filtered or unexported fields }
DummyResponseWriterCloser implements only the context cancellation and otherwise does nothing.
func (*DummyResponseWriterCloser) Close ¶
func (w *DummyResponseWriterCloser) Close() error
func (*DummyResponseWriterCloser) Flush ¶
func (w *DummyResponseWriterCloser) Flush()
func (*DummyResponseWriterCloser) Write ¶
func (w *DummyResponseWriterCloser) Write(p []byte) (int, error)
func (*DummyResponseWriterCloser) WriteHeader ¶
func (w *DummyResponseWriterCloser) WriteHeader(c int)
type FileCombiner ¶
func (*FileCombiner) ServeHTTP ¶
func (fc *FileCombiner) ServeHTTP(w http.ResponseWriter, r *http.Request)
func (*FileCombiner) SetNextHandler ¶
func (fc *FileCombiner) SetNextHandler(next http.Handler) ChainedHandler
type GzipHandler ¶
func NewGzipHandler ¶
func NewGzipHandler() *GzipHandler
func (*GzipHandler) ServeHTTP ¶
func (h *GzipHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
func (*GzipHandler) SetNextHandler ¶
func (h *GzipHandler) SetNextHandler(next http.Handler) ChainedHandler
type GzipResponseWriterCloser ¶
type GzipResponseWriterCloser struct {
// contains filtered or unexported fields
}
GzipResponseWriterCloser implements gzip writing and context cancellation.
func (*GzipResponseWriterCloser) Close ¶
func (w *GzipResponseWriterCloser) Close() error
func (*GzipResponseWriterCloser) Flush ¶
func (w *GzipResponseWriterCloser) Flush()
func (*GzipResponseWriterCloser) Header ¶
func (w *GzipResponseWriterCloser) Header() http.Header
func (*GzipResponseWriterCloser) Write ¶
func (w *GzipResponseWriterCloser) Write(p []byte) (int, error)
func (*GzipResponseWriterCloser) WriteHeader ¶
func (w *GzipResponseWriterCloser) WriteHeader(c int)
type HTTPBindataFile ¶
func (*HTTPBindataFile) Close ¶
func (f *HTTPBindataFile) Close() error
func (*HTTPBindataFile) Name ¶
func (f *HTTPBindataFile) Name() string
type HTTPBindataFs ¶
type HTTPBindataFs struct { AssetFunc AssetDirFunc AssetInfoFunc Prepend string // require this in front of a name request in order to satisfy it (i.e. make it appear like all files are under this dir) }
type HTTPBytesFile ¶
func NewHTTPBytesFile ¶
func NewHTTPBytesFile(name string, modTime time.Time, data []byte) *HTTPBytesFile
func (*HTTPBytesFile) Close ¶
func (f *HTTPBytesFile) Close() error
type HTTPFileInfo ¶
type HTTPFileInfo struct {
// contains filtered or unexported fields
}
HTTPFileInfo implements os.FileInfo
func NewHTTPFileInfo ¶
func (*HTTPFileInfo) IsDir ¶
func (i *HTTPFileInfo) IsDir() bool
IsDir is an abbreviation for Mode().IsDir()
func (*HTTPFileInfo) ModTime ¶
func (i *HTTPFileInfo) ModTime() time.Time
ModTime returns the modification time
func (*HTTPFileInfo) Name ¶
func (i *HTTPFileInfo) Name() string
Name returns the base name of the file
func (*HTTPFileInfo) Sys ¶
func (i *HTTPFileInfo) Sys() interface{}
Sys is underlying data source (can return nil)
type HTTPFuncFS ¶
type HTTPStackedFileSystem ¶
type HTTPStackedFileSystem struct {
Stack []http.FileSystem
}
HTTPStackedFileSystem implements http.FileSystem by falling back through multiple layers to find where a file exists and using the first match. FileSystems are checked in the order provided, i.e. index 0 takes precedence over all others, etc.
func NewHTTPStackedFileSystem ¶
func NewHTTPStackedFileSystem(subfss ...http.FileSystem) *HTTPStackedFileSystem
type HandlerList ¶
func (HandlerList) ServeHTTP ¶
func (hl HandlerList) ServeHTTP(w http.ResponseWriter, r *http.Request)
type MarkdownBodyTemplater ¶
func NewMarkdownBodyTemplater ¶
func NewMarkdownBodyTemplater() *MarkdownBodyTemplater
func (*MarkdownBodyTemplater) BodyTemplate ¶
type PathParser ¶
type PathParser struct {
Args []string // arguments matched, 0 indexed
}
PathParser is a simple helper to match paths and extract parts from them.
func (*PathParser) ArgInt ¶
func (pp *PathParser) ArgInt(n int) (ret int)
func (*PathParser) ArgString ¶
func (pp *PathParser) ArgString(n int) (ret string)
func (*PathParser) ArgStringDef ¶
func (pp *PathParser) ArgStringDef(n int, defaultVal string) (ret string)
func (*PathParser) ArgUUIDB64 ¶
func (pp *PathParser) ArgUUIDB64(n int) gouuidv6.UUIDB64
type RenderHandler ¶
type RenderHandler interface {
ServeHTTPPath(w http.ResponseWriter, r *http.Request, p string)
}
type Renderer ¶
type Renderer struct { BodyFs http.FileSystem // read body templates from here IncludeFs http.FileSystem // read included templates from here BodyTemplaters map[string]BodyTemplater // map of file extension to BodyTemplater PathExpandFunc func(string) []string // translate a path into a list of other possible ones that should be checked (in sequence) and the first one found rendered }
func NewDefaultRenderer ¶
func NewDefaultRenderer(bodyFs http.FileSystem, includeFs http.FileSystem) *Renderer
NewDefaultRenderer creates a renderer with sensible defaults and supports Go templates and markdown.
func (*Renderer) ServeHTTP ¶
func (rr *Renderer) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler
func (*Renderer) ServeHTTPPath ¶
ServeHTTPPath implemented RenderHandler and is like ServeHTTP but lets you override which file it will look for. This is a simple way to "rewrite" URLs based on custom logic - you just call the Renderer with the appropriate custom path.
type ResponseWriterCloser ¶
type ResponseWriterCloser interface { http.ResponseWriter io.Closer }
func GzipCtxWrap ¶
func GzipCtxWrap(w http.ResponseWriter, r *http.Request) (ResponseWriterCloser, context.Context)
GzipCtxWrap returns a ResponseWriterCloser that implements gzip response writing if possible and a context that is cancelled once the response header is written. This makes it simple to ask the context if the request has been handled yet.
type StaticFileServer ¶
StaticFileServer serves static files from a directory.
func NewStaticFileServer ¶
func NewStaticFileServer(fs afero.Fs, dir string) *StaticFileServer
FIXME: this should be http.FileSystem - we don't need write functionality for static file serving...
func (*StaticFileServer) ServeHTTP ¶
func (sfs *StaticFileServer) ServeHTTP(w http.ResponseWriter, r *http.Request)