gojiutil

package module
v0.0.0-...-5c816da Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2015 License: MIT Imports: 15 Imported by: 2

README

Goji Utils

Middlewares and utilities to go with the golang goji web framework.

  • Master: Build Status Coverage
  • v1.0.0: Build Status Coverage

Installation

go get gopkg.in/rightscale/gojiutil.v1 import "gopkg.in/rightscale/gojiutil.v1"

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ApplicationJSON = "application/json"
View Source
var ContextLog string = "log"

ContextLog is the hash key in which ContextLogger places the log15 context logger

View Source
var RequestIDHeader = "X-Request-Id"

Functions

func AddCommon

func AddCommon(mx *web.Mux)

Add the following common middlewares: EnvInit, RealIP, RequestID

func AddCommon15

func AddCommon15(mx *web.Mux, log log15.Logger)

Add the following common middlewares: EnvInit, RealIP, RequestID, Logger15, Recoverer, FormParser

func ContextLogger

func ContextLogger(c *web.C, h http.Handler) http.Handler

ContextLogger injects a log15 logger that is initialized to print the request ID. It assumes that c.Env[middleware.RequestIDKey] is set (e.g. by the RequestID middleware). It puts the logger into c.Env[gojiutil.ContextLogger].

func EnvAdd

func EnvAdd(m map[string]interface{}) web.MiddlewareType

Create a simple middleware that merges a map into c.Env

func ErrorInternal

func ErrorInternal(c web.C, rw http.ResponseWriter, err error)

Convenience function to produce an internal error based on the err argument

func ErrorString

func ErrorString(c web.C, rw http.ResponseWriter, code int, str string)

Produce a text/plain error response into the responseWriter and also sets the context to reflect the error in a way that the logger groks properly. For 500 errors a generic error is returned and the details are only logged.

func Errorf

func Errorf(c web.C, rw http.ResponseWriter, code int, message string, args ...interface{})

Convenience function to call ErrorString with a format string

func FormParser

func FormParser(c *web.C, h http.Handler) http.Handler

FormParser simply calls Request.FormParse to get all params into the request

func GetJSONBody

func GetJSONBody(c *web.C, h http.Handler) http.Handler

GetJSONBody is a middleware to read and parse an application/json body and store it in c.Env["json"] as a map[string]interface{}, which can be easily mapped to a proper struct using github.com/mitchellh/mapstructure. This middleware is pretty permissive: it allows for having no content-length and no content-type as long as either there's no body or the body parses as json.

func Logger15

func Logger15(logger log15.Logger) web.MiddlewareType

Create a logger middleware that logs HTTP requests and results to log15 Assumes that c.Env is allocated, use goji/middleware.EnvInit for that Prints a requestID if one is present, use goji/middleware.RequestID Prints the requestor's IP address, use goji/middleware.RealIP

func ParamsLogger

func ParamsLogger(verbose bool) web.MiddlewareType

ParamsLogger logs all query string / form parameters primarily for debug purposes. It logs at the start of a request using log15.Debug (or c.Env[ContextLog].Debug if defined) unlike the Logger15 middleware, which logs at the end. If verbose is true then the c.URLParams and the c.Env hashes are also logged

func Printf

func Printf(rw http.ResponseWriter, code int, message string, args ...interface{})

func Recoverer

func Recoverer(c *web.C, h http.Handler) http.Handler

Create a panic-catching middleware for Echo that ensures the server doesn't die if one of the handlers panics. Also puts the call stack into the Echo Context which causes the logger middleware to log it.

func RequestID

func RequestID(c *web.C, h http.Handler) http.Handler

RequestID injects a request ID into the context of each request. Retrieve it using goji's GetReqID(). If the incoming request has a header of RequestIDHeader then that value is used, else a random value is generated

func WriteJSON

func WriteJSON(c web.C, rw http.ResponseWriter, code int, obj interface{})

func WriteString

func WriteString(rw http.ResponseWriter, code int, str string)

Types

This section is empty.

Jump to

Keyboard shortcuts

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