handler

package module
v0.0.0-...-11f3bec Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2016 License: BSD-2-Clause Imports: 10 Imported by: 0

README

context-handler

GoDoc

Go HTTP handlers using context.

Installation

go get github.com/ernestoalejo/context-handler

Features

  • Pass context to all handlers.
  • Buffer responses to return errors to users correctly.
  • Read and write JSON in POST requests easily.
  • Return errors easily from the handler.
  • Show stack traces of errors in stderr (compatible with App Engine).
  • Configurable middlewares.
  • Auto load App Engine context from the request.
  • Graceful HTTP listener.
  • Use gin in development to autoreload the server.
Usage
package main

import (
  "fmt"
  "net/http"
  "log"
  "time"

  "github.com/ernestoalejo/context-handler"
  "github.com/juju/errors"
  "golang.org/x/net/context"
)

func main() {
  http.HandleFunc("/", handler.Ctx(homeHandler))
  http.HandleFunc("/health", handler.Ctx(healthHandler))

  handler.ServeGracefully(9000, 30*time.Second)
}

func homeHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
  fmt.Fprintln(w, "Home handler")

  return errors.New("cannot handle request correctly")
}

func healthHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
  fmt.Fprintln(w, "ok")
  
  return nil
}

Documentation

Overview

Package handler implements Go HTTP handlers using context.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddMiddleware

func AddMiddleware(middleware Middleware)

AddMiddleware to the list of middlewares.

func ClearMiddlewares

func ClearMiddlewares()

ClearMiddlewares cleans the list of applied middlewares to let the app choose the order and what ones get activated with the requests.

func ClientErrorMiddleware

func ClientErrorMiddleware(ctx context.Context, w http.ResponseWriter, r *http.Request, next NextMiddlewareFn) error

ClientErrorMiddleware rejects the request with a 500 status code when an error occurs.

func Ctx

func Ctx(fn CtxHandler) http.HandlerFunc

Ctx adapts a context handler to the standard HTTP lib contract.

func LoggerMiddleware

func LoggerMiddleware(ctx context.Context, w http.ResponseWriter, r *http.Request, next NextMiddlewareFn) error

LoggerMiddleware logs to stderr the error.

func ReadJSON

func ReadJSON(r *http.Request, data interface{}) error

ReadJSON checks the request to see if it's a POST one; and reads the JSON data.

func ServeGracefully

func ServeGracefully(defaultPort int64, stopTimeout time.Duration)

ServeGracefully creates a new listener that will close gracefully without dropping client connections. If a PORT environment variable is defined it will use that port instead of the default one. If debugging is enabled a normal listener will be used instead to close it quicker in the development cycle.

func WriteJSON

func WriteJSON(w http.ResponseWriter, data interface{}) error

WriteJSON to the response using the correct Content-Type header.

Types

type AppResponseWriter

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

AppResponseWriter the package uses to cache the responses. Exposed to be able to build or own middlewares.

func (*AppResponseWriter) Header

func (w *AppResponseWriter) Header() http.Header

Header will call the same function in the standard response writer.

func (*AppResponseWriter) Reset

func (w *AppResponseWriter) Reset()

Reset removes any content in the memory buffer for the response.

func (*AppResponseWriter) Write

func (w *AppResponseWriter) Write(data []byte) (n int, err error)

Write will call the same function in the standard response writer.

func (*AppResponseWriter) WriteHeader

func (w *AppResponseWriter) WriteHeader(statusCode int)

WriteHeader will call the same function in the standard response writer.

type CtxHandler

type CtxHandler func(ctx context.Context, w http.ResponseWriter, r *http.Request) error

CtxHandler should be implemented by the handlers.

type Middleware

type Middleware func(ctx context.Context, w http.ResponseWriter, r *http.Request, next NextMiddlewareFn) error

Middleware should be implemented by the functions that want to intercept requests or responses.

type NextMiddlewareFn

type NextMiddlewareFn func(ctx context.Context) error

NextMiddlewareFn should be called after the processing in the middleware finishes to follow the chain. An instance of this function type will be passed to each middleware call.

Jump to

Keyboard shortcuts

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