Documentation ¶
Index ¶
- Constants
- Variables
- func CacheMarshal(value interface{}) ([]byte, error)
- func CacheUnmarshal(raw []byte, dest interface{}) error
- func CsrfField(ctx context.Context) template.HTML
- func CsrfToken(ctx context.Context) string
- func LogError(ctx context.Context, err error, message string, keyvals ...string)
- func LogInfo(ctx context.Context, message string, keyvals ...string)
- func NewHTTPApplication(app interface{}, logger Logger, debug bool) http.Handler
- func NewRouter() *router
- func PathArg(r *http.Request, index int) string
- func PathArgInt(r *http.Request, index int) int
- func PathArgInt64(r *http.Request, index int) int64
- func RunCacheImplementationTest(t *testing.T, c CacheService)
- type CacheMarshaler
- type CacheService
- type EnvConf
- func (c *EnvConf) Bool(name string, fallback bool, description string) bool
- func (c *EnvConf) Duration(name string, fallback time.Duration, description string) time.Duration
- func (c *EnvConf) Int(name string, fallback int, description string) int
- func (c *EnvConf) Secret(name, fallback, description string) string
- func (c *EnvConf) Str(name, fallback, description string) string
- func (c *EnvConf) WriteHelp(w io.Writer)
- type HTMLRenderer
- type Handler
- type HandlerFunc
- type LocalMemCache
- func (c *LocalMemCache) Del(ctx context.Context, key string) error
- func (c *LocalMemCache) Flush()
- func (c *LocalMemCache) Get(ctx context.Context, key string, dest interface{}) error
- func (c *LocalMemCache) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (c *LocalMemCache) Set(ctx context.Context, key string, value interface{}, exp time.Duration) error
- func (c *LocalMemCache) SetNx(ctx context.Context, key string, value interface{}, exp time.Duration) error
- type Logger
- type Middleware
- type Paginator
- type PaginatorPage
- type Response
- func JSONErr(code int, errText string) Response
- func JSONErrs(code int, errs []string) Response
- func JSONResp(code int, content interface{}) Response
- func Redirect(url string, responseCode int) Response
- func StdJSONResp(code int) Response
- func StdResponse(ctx context.Context, r HTMLRenderer, responseCode int) Response
- type Route
- type TraceSpan
- type URLPath
- type UnboundCacheService
Constants ¶
const CsrfKey = "csrftoken"
CsrfKey is used for lookup of the csrf token, for example inside of the request's header or form
Variables ¶
var ( // ErrMiss is returned when performing operation on key is not in use. // This is a not found error narrowed to cache cases only. ErrMiss = errors.Wrap(ErrNotFound, "cache miss") // ErrCacheMalformed is returned whenever an operation cannot be // completed because value cannot be serialized or deserialized. ErrCacheMalformed = errors.Wrap(ErrMalformed, "cache malformed") )
var ( ErrInternal = errors.New("internal") ErrNotFound = errors.New("not found") ErrMalformed = errors.New("malformed") ErrValidation = errors.New("invalid") ErrConstraint = errors.Wrap(ErrValidation, "constraint") ErrPermission = errors.Wrap(ErrValidation, "permission denied") ErrConflict = errors.Wrap(ErrValidation, "conflict") )
Register all base errors that could be used to created more specific instances.
Functions ¶
func CacheMarshal ¶
CacheMarshal returns serialized representation of given value.
Unless given destination implements CacheMarshaler interface, JSON is used to marshal the value.
func CacheUnmarshal ¶
CacheUnmarshal deserialize given raw data into given destination.
Unless given destination implements CacheMarshaler interface, JSON is used to unmarshal the value.
func CsrfToken ¶
CsrfToken returns CSRF protection token attached to given context. Handler must be protected by CsrfMiddleware to have csrf token present in the context.
func LogError ¶
LogError writes info log message to logger present in given context. Message is discarded if no logger is present in context.
func LogInfo ¶
LogInfo writes info log message to logger present in given context. Message is discarded if no logger is present in context.
func NewHTTPApplication ¶
func PathArg ¶
PathArg return value as matched by path regexp at given index. Indexing of matched values starts with 0. If requested argument is out of index, empty string is returned.
func PathArgInt ¶
PathArgInt returns integer value of given path argument. If requested path argument is not valid integer value, 0 is returned. Use correct regular expression to ensure represented value is a valid number.
func PathArgInt64 ¶
PathArgInt64 returns integer value of given path argument. If requested path argument is not valid integer value, 0 is returned. Use correct regular expression to ensure represented value is a valid number.
func RunCacheImplementationTest ¶
func RunCacheImplementationTest(t *testing.T, c CacheService)
Types ¶
type CacheMarshaler ¶
type CacheService ¶
type CacheService interface { // Get value stored under given key. Returns ErrMiss if key is not // used. Get(ctx context.Context, key string, dest interface{}) error // Set value under given key. If key is already in use, overwrite it's // value with given one and set new expiration time. Set(ctx context.Context, key string, value interface{}, exp time.Duration) error // SetNx set value under given key only if key is not used. It returns // ErrConflict if trying to set value for key that is already in use. SetNx(ctx context.Context, key string, value interface{}, exp time.Duration) error // Del deletes value under given key. It returns ErrCacheMiss if given // key is not used. Del(ctx context.Context, key string) error }
func NewFilesystemCache ¶
func NewFilesystemCache(rootDir string) CacheService
func PrefixCache ¶
func PrefixCache(cache CacheService, prefix string) CacheService
func StampedeProtect ¶
func StampedeProtect(cache CacheService) CacheService
func TraceCache ¶
func TraceCache(cache CacheService, prefix string) CacheService
type EnvConf ¶
type EnvConf struct { OnErr func(error) // contains filtered or unexported fields }
func NewEnvConf ¶
func NewEnvConf() *EnvConf
NewEnvConf returns configuration instance that use environment variable for configuration.
type HTMLRenderer ¶
type HTMLRenderer interface {
Response(ctx context.Context, statusCode int, templateName string, templateContext interface{}) Response
}
func NewHTMLRenderer ¶
func NewHTMLRenderer(templatesGlob string, debug bool, funcs template.FuncMap) HTMLRenderer
NewHTMLRenderer returns HTMLRenderer instance. Default surf template set is extended by templates found in provided path and by passed function mapping.
When running in debug mode, templates are always compiled before rendering. In non debug mode, templates are compiled once and reused to achieve better performance. When in debug mode, all template errors are rendered with explanation and additional information, instead of generic error page.
type Handler ¶
type Handler interface {
HandleHTTPRequest(http.ResponseWriter, *http.Request) Response
}
func AsHandler ¶
func AsHandler(h interface{}) Handler
AsHandler takes various handler notations and converts them to surf's Handler. If given handler does not implement any known interface, nil and false is returned
func WithMiddlewares ¶
func WithMiddlewares(handler interface{}, middlewares []Middleware) Handler
WithMiddlewares attach given collection of middlewares. First one is called last.
type HandlerFunc ¶
type HandlerFunc func(http.ResponseWriter, *http.Request) Response
func (HandlerFunc) HandleHTTPRequest ¶
func (fn HandlerFunc) HandleHTTPRequest(w http.ResponseWriter, r *http.Request) Response
type LocalMemCache ¶
type LocalMemCache struct {
// contains filtered or unexported fields
}
func NewLocalMemCache ¶
func NewLocalMemCache() *LocalMemCache
NewLocalMemCache returns local memory cache intance. This is strictly for testing and must not be used for end application.
func (*LocalMemCache) Flush ¶
func (c *LocalMemCache) Flush()
func (*LocalMemCache) Get ¶
func (c *LocalMemCache) Get(ctx context.Context, key string, dest interface{}) error
func (*LocalMemCache) ServeHTTP ¶
func (c *LocalMemCache) ServeHTTP(w http.ResponseWriter, r *http.Request)
type Logger ¶
type Logger interface { Info(context.Context, string, ...string) Error(context.Context, error, string, ...string) }
type Middleware ¶
type Middleware func(handler interface{}) Handler
Middleware is a function that takes handler in one of recognized by surf notations and returns new handler, wrapping original one with additional functionality.
func CsrfMiddleware ¶
func CsrfMiddleware( cache UnboundCacheService, tmpl HTMLRenderer) Middleware
func DebugToolbarMiddleware ¶
func DebugToolbarMiddleware(rootPath string) Middleware
func LoggingMiddleware ¶
func LoggingMiddleware(logger Logger) Middleware
func PanicMiddleware ¶
func PanicMiddleware(logger Logger) Middleware
func TracingMiddleware ¶
func TracingMiddleware(frequency time.Duration) Middleware
TracingMiddleware provides trace in request's context with given frequency.
type Paginator ¶
func (*Paginator) CurrentPage ¶
func (*Paginator) HasNextPage ¶
func (*Paginator) HasPrevPage ¶
func (*Paginator) Pages ¶
func (p *Paginator) Pages() []PaginatorPage
type Response ¶
func StdJSONResp ¶
StdJSONResp write JSON encoded, standard HTTP response text for given status code. Depending on status, either error or successful response format is used.
func StdResponse ¶
func StdResponse(ctx context.Context, r HTMLRenderer, responseCode int) Response
StdResponse returns Response instance with generic HTML page for given return code.
type Route ¶
type Route interface { Use(middlewares ...Middleware) Route Add(method string, handler interface{}) Route Get(handler interface{}) Route Post(handler interface{}) Route Put(handler interface{}) Route Delete(handler interface{}) Route Head(handler interface{}) Route Options(handler interface{}) Route Trace(handler interface{}) Route Patch(handler interface{}) Route }
type TraceSpan ¶
type TraceSpan interface { // Begin creates and remove new measurement span. Current span is set // as parent of newly created and returned one. // // It is users responsibility to finish span. Begin(description string, keyvalues ...string) TraceSpan // Finish close given span and finalize measurement. Finish(keyvalues ...string) }
func CurrentTrace ¶
CurrentTrace returns TraceSpan that is attached to given context. This function always returns valid TraceSpan implementation and it is safe to use the result. When no trace is attached to context, implementation that is discarding results is provided. CurrentTrace alwasys returns first TraceSpan that covers the whole measurement period.
type UnboundCacheService ¶
type UnboundCacheService interface {
Bind(http.ResponseWriter, *http.Request) CacheService
}
func NewCookieCache ¶
func NewCookieCache(prefix string, secret []byte) (UnboundCacheService, error)
func NewUnboundCache ¶
func NewUnboundCache(cache CacheService, key string) UnboundCacheService
Source Files ¶
- application.go
- cache.go
- cache_cookie.go
- cache_filesystem.go
- cache_implementationtest.go
- cache_localmemory.go
- cache_prefix.go
- cache_stampede.go
- cache_trace.go
- cache_unbound.go
- csrf.go
- debugtoolbar.go
- envconf.go
- errors.go
- id.go
- json_response.go
- log.go
- middleware.go
- paginator.go
- path.go
- router.go
- template.go
- trace.go