web

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2022 License: BSD-3-Clause Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ContentTypeTextHTML       = "text/html"
	ContentTypeTextPlain      = "text/plain"
	ContentTypeTextCSS        = "text/css"
	ContentTypeTextCSV        = "text/csv"
	ContentTypeTextJavaScript = "text/javascript"

	ContentTypeApplicationXML         = "application/xml"
	ContentTypeApplicationZip         = "application/zip"
	ContentTypeApplicationPDF         = "application/pdf"
	ContentTypeApplicationJSON        = "application/json"
	ContentTypeApplicationJavaScript  = "application/javascript"
	ContentTypeApplicationOctetStream = "application/octet-stream"

	ContentTypeImageAPNG = "image/apng"
	ContentTypeImageGIF  = "image/gif"
	ContentTypeImageJPEG = "image/jpeg"
	ContentTypeImagePNG  = "image/png"
	ContentTypeSVGXML    = "image/svg+xml"
	ContentTypeWebP      = "image/webp"

	ContentTypeMultiPartFormData = "multipart/form-data"
)
View Source
var (
	ErrBadStaticFilepath   = errors.New("bad static filepath")
	ErrBadTemplateFilepath = errors.New("bad template filepath")
	ErrBadTemplateSuffix   = errors.New("bad template suffix")
)
View Source
var (
	DefaultMuxConfigMaxOpts = &MuxConfig{
		StaticPath:   defaultStaticPath,
		WithLogging:  true,
		StdOutLogger: logging.NewStdOutLogger(os.Stdout),
		StdErrLogger: logging.NewStdErrLogger(os.Stderr),
	}
)
View Source
var ErrScopeExists = errors.New("scope already exists and cannot be added again")
View Source
var ErrScopeNotExists = errors.New("scope does not exist; cannot be found")

Functions

func ContentType added in v1.8.0

func ContentType(w http.ResponseWriter, ct string)

func GetFilepath added in v1.8.0

func GetFilepath() (string, string)

GetFilepath returns the absolute filepath from where it's called. It returns the result as a split string, first the root directory up until the current file and returns the two in this order: dir, file

func IfOK added in v1.8.0

func IfOK(next http.Handler, ok bool) http.Handler

func SanitizePath added in v1.8.0

func SanitizePath(path string) string

func StaticHandler added in v1.8.0

func StaticHandler(prefix, path string) http.Handler

StaticHandler is just a static folder handler helper

Types

type BufferPool

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

func OpenBufferPool

func OpenBufferPool() *BufferPool

OpenBufferPool returns a single instance of the buffer pool utilizing sync.Once which gives us a no-brainier implementation of the classic singleton pattern

func (*BufferPool) Get

func (bp *BufferPool) Get() *bytes.Buffer

func (*BufferPool) Put

func (bp *BufferPool) Put(buf *bytes.Buffer)

type Chain added in v1.8.0

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

Chain acts as a list of http.Handler middlewares. It's effectively immutable.

func NewChain added in v1.8.0

func NewChain(mw ...Middleware) *Chain

NewChain creates a new chain, memorizing the given list of middleware handlers. New serves no other function, middlewares are only called upon a call to Then().

func (*Chain) Append added in v1.8.0

func (c *Chain) Append(mw ...Middleware) *Chain

Append extends a chain, adding the specified constructors as the last ones in the request flow.

func (*Chain) Extend added in v1.8.0

func (c *Chain) Extend(chain *Chain) *Chain

Extend extends a chain by adding the specified chain as the last one in the request flow.

func (*Chain) Then added in v1.8.0

func (c *Chain) Then(handler http.Handler) http.Handler

Then chains the middleware and returns the final http.Handler. Then() treats nil as http.DefaultServeMux.

func (*Chain) ThenFunc added in v1.8.0

func (c *Chain) ThenFunc(handler http.HandlerFunc) http.Handler

ThenFunc works identically to Then, but takes a HandlerFunc instead of a Handler.

type Middleware added in v1.8.0

type Middleware func(http.Handler) http.Handler

Middleware is a piece of middleware.

type MuxConfig

type MuxConfig struct {
	StaticPath   string
	WithLogging  bool
	StdOutLogger *log.Logger
	StdErrLogger *log.Logger
}

type ServeMux

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

func NewServeMux

func NewServeMux(logger *logging.LevelLogger) *ServeMux

func (*ServeMux) ContentType

func (s *ServeMux) ContentType(w http.ResponseWriter, content string)

func (*ServeMux) Delete

func (s *ServeMux) Delete(pattern string, handler http.Handler)

func (*ServeMux) Forward

func (s *ServeMux) Forward(oldpattern string, newpattern string)

func (*ServeMux) Get

func (s *ServeMux) Get(pattern string, handler http.Handler)

func (*ServeMux) Handle

func (s *ServeMux) Handle(method string, pattern string, handler http.Handler)

func (*ServeMux) HandleFunc

func (s *ServeMux) HandleFunc(method, pattern string, handler func(http.ResponseWriter, *http.Request))

func (*ServeMux) Len

func (s *ServeMux) Len() int

func (*ServeMux) Less

func (s *ServeMux) Less(i, j int) bool

func (*ServeMux) Post

func (s *ServeMux) Post(pattern string, handler http.Handler)

func (*ServeMux) Put

func (s *ServeMux) Put(pattern string, handler http.Handler)

func (*ServeMux) Search

func (s *ServeMux) Search(x string) int

func (*ServeMux) ServeHTTP

func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*ServeMux) Static

func (s *ServeMux) Static(pattern string, path string)

func (*ServeMux) Swap

func (s *ServeMux) Swap(i, j int)

type TemplateCache

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

TemplateCache is a template engine that caches golang html/template files

func NewTemplateCache

func NewTemplateCache(conf *TemplateConfig) (*TemplateCache, error)

NewTemplateCache takes a pattern to glob and an optional logger and returns a new *TemplateCache instance. On success, it returns a nil error. An example pattern to glob would be: "web/templates/*.html" or "my-path/*.tmpl.html"

func NewTemplateCacheWithFiles added in v1.3.0

func NewTemplateCacheWithFiles(conf *TemplateConfig, files ...string) (*TemplateCache, error)

func NewTemplateCacheWithSeparateStubs added in v1.1.0

func NewTemplateCacheWithSeparateStubs(conf *TemplateConfig) (*TemplateCache, error)

func (*TemplateCache) AddSeparateStubs added in v1.1.0

func (t *TemplateCache) AddSeparateStubs(stubsPattern string) error

func (*TemplateCache) ContentType

func (t *TemplateCache) ContentType(w http.ResponseWriter, content string)

func (*TemplateCache) ExecuteTemplate

func (t *TemplateCache) ExecuteTemplate(w io.Writer, name string, data interface{}) error

func (*TemplateCache) Handle added in v1.2.0

func (t *TemplateCache) Handle(name string) http.Handler

func (*TemplateCache) HandleWithData added in v1.2.0

func (t *TemplateCache) HandleWithData(name string, data interface{}) http.Handler

func (*TemplateCache) Lookup

func (t *TemplateCache) Lookup(name string) *template.Template

func (*TemplateCache) Raw

func (t *TemplateCache) Raw(w http.ResponseWriter, format string, data ...interface{})

func (*TemplateCache) Render

func (t *TemplateCache) Render(w http.ResponseWriter, r *http.Request, tmpl string, data interface{})

func (*TemplateCache) RenderWithBuffer

func (t *TemplateCache) RenderWithBuffer(w http.ResponseWriter, r *http.Request, tmpl string, data interface{})

func (*TemplateCache) Templates

func (t *TemplateCache) Templates() ([]*template.Template, string)

func (*TemplateCache) Use

func (t *TemplateCache) Use(name string) *template.Template

type TemplateConfig

type TemplateConfig struct {
	StubsPattern    string
	TemplatePattern string
	StdErrLogger    *log.Logger
	FuncMap         template.FuncMap
}

type TemplateManager added in v1.1.0

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

func NewTemplateManager added in v1.1.0

func NewTemplateManager() *TemplateManager

func (*TemplateManager) AddCache added in v1.1.0

func (tm *TemplateManager) AddCache(scope, base, tmplPattern, stubPattern string) error

func (*TemplateManager) AddCacheWithFiles added in v1.3.0

func (tm *TemplateManager) AddCacheWithFiles(scope, base string, files ...string) error

func (*TemplateManager) GetCache added in v1.1.0

func (tm *TemplateManager) GetCache(scope string) *TemplateCache

func (*TemplateManager) Lookup added in v1.1.0

func (tm *TemplateManager) Lookup(scope, name string) *template.Template

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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