middleware

package
v0.0.0-...-e64dc14 Latest Latest
Warning

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

Go to latest
Published: Feb 29, 2016 License: BSD-3-Clause Imports: 23 Imported by: 0

Documentation

Overview

Copyright 2011 Viktor Kojouharov. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Package middleware contains a set of middleware handlers used to augment requests before they are passed to controllers

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitializeDefault

func InitializeDefault(d *webfw.Dispatcher)

InitializeDefault creates all default middleware objects by the order of the dispatcher configuration, and registers them to the later.

func LocalizedURL

func LocalizedURL(c context.Context, r *http.Request, dispatcherPattern, language string, parts []string) (string, error)

The LocalizedURL function is equivalent to the 'localizedUrl' template function.

func URL

func URL(c context.Context, r *http.Request, dispatcherPattern string, parts []string) (string, error)

The URL function provides the functionality of the url template functions for use outside of the template context. The dispatcherPattern is the pattern used by the dispatcher responsible for handling the resulting url. In most cases it will probably be "/".

Types

type Context

type Context struct{}

The Context middleware cleans up the framework context object of any data related to the current request, after it has gone through the middleware chain.

func (Context) Handler

func (cmw Context) Handler(ph http.Handler, c context.Context) http.Handler

type Error

type Error struct {
	ShowStack bool
}

The Error middleware provides basic panic recovery for a request. For this reason, it should be at the botton of the middleware chain, to catch any raised panics along the way. If such occurs, the response writer will contain the 'Internal Server Error' message, and the stack trace will be written to the error log. It also has a ShowStack option, which will cause the stack trace to be written to the response writer if true. It is set to true if the global configuration is set to "devel".

func (Error) Handler

func (emw Error) Handler(ph http.Handler, c context.Context) http.Handler

type FileStats

type FileStats []os.FileInfo

func (FileStats) Len

func (fs FileStats) Len() int

func (FileStats) Less

func (fs FileStats) Less(i, j int) bool

func (FileStats) Swap

func (fs FileStats) Swap(i, j int)

type Gzip

type Gzip struct{}

The Gzip middleware will compress the response using the gzip format. If placed in the middleware chain, it will be triggered whenever the client states it may accept gzip via the Accept-Encoding header.

func (Gzip) Handler

func (gmw Gzip) Handler(ph http.Handler, c context.Context) http.Handler

type I18N

type I18N struct {
	Languages        []string
	Pattern          string
	Dir              string
	IgnoreURLPrefix  []string
	FallbackLanguage string
}

The I18N middleware is used to provide a translation interface for message within templates. It registers a "__" function in the 'base' renderer template, and stores the current language and all configured languages inside the context, so they can be used within the templates using the corresponding ".base.lang" and ".base.langs" dot pipelines. It also stores the current language within the session, if the session middleware is registered before it.

A Language is set for a request via different means. First, if the relative request path begins with '/' and the language code, that language will be used. The dispatcher pattern will not be included in this relative path. For example, if a dispatcher has the pattern "/", the request path may look like this:

  • "/en/example/path"

however, if it has a path "/test/", the request will be:

  • "/test/en/example/path"

If the relative request path doesn't contain the language, the language stored in the session will be used, if one is present. Otherwise, it will fall back to the first one in the Accept-Language header, then to the LANG environment variable, then to the LC_MESSAGES one, and finally to "en". If the request path doesn't contain a language, the client will be redirected to the same path, including whatever fallback language is selected. Finally, the request path is modified so that subsequent middleware and the final handler do not see the actual language.

Internally, it uses the "github.com/nicksnyder/go-i18n/i18n" package for the actual translation.

The middleware may be configured via the server configuration. The "dir" setting specifies the directory that contains *.all.json files, one per configured languages. The supported languages may be configured via the "languages" slice. If a language file doesn't exist, the middleware will panic early on. In order to ignore requests for a certain prefix, the "ignore-url-prefix" slice may be defined in the settings.

The template function "__" receives the message id as its first argument, the language as the second, and any trailing arguments will be interpretted as key-value tuples to be used for the message. The current language may be obtained using the ".base.lang" pipeline.

func (I18N) Handler

func (imw I18N) Handler(ph http.Handler, c context.Context) http.Handler

func (I18N) TemplateFuncMap

func (imw I18N) TemplateFuncMap() template.FuncMap

type Logger

type Logger struct {
	AccessLogger webfw.Logger
}

The Logger middleware generates an access log entry for each request. The format is similar to the one used by nginx. It may receive a webfw.Logger object, which by default is a Stdout logger.

func (Logger) Handler

func (lmw Logger) Handler(ph http.Handler, c context.Context) http.Handler

type Session

type Session struct {
	Path            string
	Secret          []byte
	Cipher          []byte
	MaxAge          string
	CleanupInterval string
	CleanupMaxAge   string
	Pattern         string
	IgnoreURLPrefix []string

	SessionGenerator context.SessionGenerator
}

The Session middleware is responsible for initializing the persistent user session for each request. Most of its configuration is passed to the underlying session object.

The middleware may be configured using the server configuration. The "dir", "secret", "max-age" and "cleanup-max-age" are passed to the session object itself. "max-age", "cleanup-interval" and "cleanup-max-age" use the time.Duration string format. The "cleanup-interval" setting specifies a time.Ticker duration. On each tick, any file system session data will be removed, if its older than "cleanup-max-age". If the later setting is empty, all session data will be deleted.

If the session middleware is initialized and registered to a dispatcher manually, it is possible to set the 'SessionGenerator' struct field, so that a different session implementation may be used. If that is not set, session.NewSession will be used.

func (Session) Handler

func (smw Session) Handler(ph http.Handler, c context.Context) http.Handler

type Sitemap

type Sitemap struct {
	Pattern          string
	Prefix           string
	RelativeLocation string
	Controllers      []webfw.Controller
	// contains filtered or unexported fields
}

func (Sitemap) Handler

func (mw Sitemap) Handler(ph http.Handler, c context.Context) http.Handler

type Static

type Static struct {
	Path     string
	Prefix   string
	Index    string
	Expires  string
	FileList bool
}

The Static middleware serves static files, and optionally provides directory listings. Any parent handler which sets the response code to http.StatusNotFound will cause the middleware to try and handle the request as a static file. Furthermore, only "HEAD" and "GET" methods will be handled.

The following server configuration variables may be set:

  • "dir" specifies the directory from where to serve static files
  • "prefix" is a request path prefix, which may be used to limit the static handling for only those requests
  • "index" specifies the filename to try and serve if the request is for a directory
  • "expires" specifies a time.Duration string format, which will be used to set the Expires and Cache-Control header fields. If left empty, those fields will not be set.
  • "file-list" is a boolean flag, which will cause the middleware to show the directory listing if the request is for a directory, and it doesn't contain an index file.

func (Static) Handler

func (mw Static) Handler(ph http.Handler, c context.Context) http.Handler

type Url

type Url struct {
	Pattern string
}

The Url middleware provides helper template functions, registered on the base renderer template. The following functions are currently provided:

  • "url" - expectes a series of string arguments, which will be used to generate a url. A trailing request object must be provided. The first string argument will be used as the url path, and the subsequent ones will be joined as a query string. If the path argument does not begin with a '/', it will be treated as a relative path, and the current request path will be prepended. The same goes if the the path argument is the empty string. In both cases, if the current request has query params, those will be added to the generated url. The string arguments may also contain one or more ':here:' strings, which will be replaces with the current RequestURI. Finally, if a previous middleware has set the language in the context, it will be added to the url. Examples for a request /example/request and language "en":
  • {{ url "" .base.r }} -> /en/example/request
  • {{ url "test" .base.r }} -> /en/example/request/test
  • {{ url "/test" .base.r }} -> /en/test
  • {{ url "/test/:here:" .base.r }} -> /en/test/example/request
  • {{ url "/test" "backto=:here:" .base.r }} -> /en/test?backto=/example/request
  • "localizedUrl" - very similar to the "url" function. The only difference is that the last string argument will be treated as the language to be used when building the url. An example for the same request:
  • {{ localizedUrl "/foo" "de" .base.r }} -> /de/foo

func (Url) Handler

func (mw Url) Handler(ph http.Handler, c context.Context) http.Handler

func (Url) TemplateFuncMap

func (mw Url) TemplateFuncMap(c context.Context) template.FuncMap

Jump to

Keyboard shortcuts

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