handler

package module
v0.8.6 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2018 License: MIT Imports: 7 Imported by: 0

README

handler Build Status GoDoc

http.Handlers for Go

Domains

Handlers are separated into their own domains. They are:

  • log - handlers used for logging purposes
    • Access - logs each request to the provided logger.
    • Panic - catches panics, logs the stack traces to a provided logger, and returns an Internal Server Error. Optionally prints the stack trace in the response body.
  • encoding - handlers dealing with encoding
    • Gzip - compresses the response body
  • lang - handlers for language/translation support
    • I18N - deals with language handling, redirecting to a url with a supported language code. Provides the supported languages and current one in the request context.

Example

Using the I18N handler to process a requested language.

When a user visits '/', a language will be picked from the list of supported ones, based on what the browser has set in the Accepted-Language header, or fall back to the first one in the list. If, for example, the selected language was English, a redirect will be sent to '/en/', where the final handler extracts the selected language from the request context.

package main

import (
	"net/http"

	"golang.org/x/text/language"
	"golang.org/x/text/language/display"

	"github.com/urandom/handler/lang"
)

func main() {
	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		val := r.Context().Value(lang.ContextKey).(lang.ContextValue)

		w.Write([]byte("Current language: " + display.Self.Name(val.Current)))
	})
  
	http.Handle("/", lang.I18N(handler, lang.I18NOpts{
		Languages: []language.Tag{language.German, language.French, language.English},
	}))
  
	http.ListenAndServe(":8080", nil)
}

Documentation

Overview

Package handler is a collection of useful http.Handlers

The following subpackages contain:

  • log - handlers for logging requests and panics
  • encoding - a handler for using gzip compression on the response
  • lang - a handler for setting up i18n urls

The package itself contains some common interfaces and useful types used by all handlers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger interface {
	Print(v ...interface{})
}

A Logger interface is used by handlers to when some kind of output needs to be provided

func ErrLogger

func ErrLogger() Logger

ErrLogger returns a Logger implementation that outputs to os.Stderr

func NopLogger

func NopLogger() Logger

NopLogger returns a Logger implementation that does nothing

func OutLogger

func OutLogger() Logger

OutLogger returns a Logger implementation that outputs to os.Stdout

type ResponseWrapper

type ResponseWrapper struct {
	*httptest.ResponseRecorder

	// Hijacked will be set to true if the original http.ResponseWriter was
	// hijacked successfully.
	Hijacked bool
	// contains filtered or unexported fields
}

ResponseWrapper is a simple wrapper around httptest.ResponseRecorder, implementing the http.Hijacker interface.

func NewResponseWrapper

func NewResponseWrapper(w http.ResponseWriter) *ResponseWrapper

NewResponseWrapper creates a new wrapper. The passed http.ResponseWriter is used in case the wrapper needs to be hijacked.

func (*ResponseWrapper) CloseNotify added in v0.8.3

func (w *ResponseWrapper) CloseNotify() <-chan bool

CloseNotify tries to use the original http.ResponseWriter for close notification. If the original writer doesn't implement http.CloseNotifier, it returns a channel that will never close.

func (*ResponseWrapper) Hijack

func (w *ResponseWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack tries to use the original http.ResponseWriter for hijacking. If the original writer doesn't implement http.Hijacker, it returns an error.

type Session

type Session interface {
	Get(r *http.Request, key string) (string, error)
	Set(r *http.Request, key string, value string) error
}

Session allows storing arbitrary data in between requests.

Directories

Path Synopsis
Package encoding provides an handler that compresses the response body using gzip.
Package encoding provides an handler that compresses the response body using gzip.
Package language contains a handler that provides support for handling requests in specific languages.
Package language contains a handler that provides support for handling requests in specific languages.
Package log provides handlers for logging request access and panics.
Package log provides handlers for logging request access and panics.

Jump to

Keyboard shortcuts

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