package module
v1.0.0 Latest Latest

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

Go to latest
Published: Mar 23, 2015 License: MIT Imports: 23 Imported by: 0



go http helper package. Includes contexts, logging, and some rendering helpers.

Documentation at


This package was extracted from production code powering StatHat, so clearly we feel that it is production-ready, but it should still be considered experimental as other uses of it could reveal issues we aren't experiencing.

Contact us

We'd love to hear from you if you are using this in your projects! Please drop us a line: @stathat or contact us here.


Written by Patrick Crosby at StatHat. Twitter: @stathat




This section is empty.


View Source
var AccessLogFilename = "/tmp/access.log"
View Source
var AfterErrorFunc func(Context, *AppError)
View Source
var ContentDir string
View Source
var ErrorLogFilename = "/tmp/error.log"
View Source
var NotifyRequestTime func(elapsed time.Duration, path string)
View Source
var SessionCookieLifetimeSeconds = 3600 * 24 * 14
View Source
var TokenSecret = []byte("CHANGEME")


func HandleContext

func HandleContext(pattern string, handler ContextHandlerFunc, builder ContextBuilder)

func HandleContextGzip

func HandleContextGzip(pattern string, handler ContextHandlerFunc, builder ContextBuilder)

func HandleFiles

func HandleFiles(path string)

pass in something like "images" to serve /images

func HandleFunc

func HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request, *Session))

func HandleReflect

func HandleReflect(pattern string, handler interface{}, builder ContextBuilder)

func IsHttps

func IsHttps(r *http.Request) bool

func ListenAndServe

func ListenAndServe(addr string)

func LogAccess

func LogAccess(req *http.Request, elapsed time.Duration)

func LogError

func LogError(req *http.Request, err *AppError)

func LogReload

func LogReload()

func Register

func Register(name string) error

func RegisterAlt

func RegisterAlt(name string) error

func RegisterLayout

func RegisterLayout(name, header, footer string) error

func RegisterLayoutAlt

func RegisterLayoutAlt(name, header, footer string) error

func RegisterMulti

func RegisterMulti(name string, filenames ...string) error

func RegisterNotFound

func RegisterNotFound(name string) error

func RegisterServerError

func RegisterServerError(name string) error

func RegisterString

func RegisterString(name, content string) error

func RemoveCookie

func RemoveCookie(w http.ResponseWriter, name string)

func SetCookie

func SetCookie(w http.ResponseWriter, name, value string)

func SetCookieWithExpiration

func SetCookieWithExpiration(w http.ResponseWriter, name, value string, seconds int)

XXX change seconds to duration?

func WantsJSON

func WantsJSON(r *http.Request) bool

func WriteSessionCookie

func WriteSessionCookie(w http.ResponseWriter, s *Session)


type AppError

type AppError struct {
	Err     error
	Message string
	Code    int

func NewServerError

func NewServerError(msg string) *AppError

func NotFoundErr

func NotFoundErr() *AppError

func Redirect

func Redirect(c Context, path string) *AppError

func RedirectToHttps

func RedirectToHttps(c Context) *AppError

func Render

func Render(layout, template string, data interface{}, c Context) *AppError

func RenderAtom

func RenderAtom(template string, data interface{}, context Context) *AppError

func RenderCSV

func RenderCSV(c Context, records [][]string, filename string) *AppError

func RenderJS

func RenderJS(c Context, js string) *AppError

func RenderJSON

func RenderJSON(c Context, obj interface{}) *AppError

func RenderJSONP

func RenderJSONP(c Context, obj interface{}, jsonpCallback string) *AppError

func RenderMulti

func RenderMulti(layout, name, innerName string, data interface{}, c Context) *AppError

func RenderNoLayout

func RenderNoLayout(template string, data interface{}, c Context) *AppError

func RenderNoLayoutNoContext

func RenderNoLayoutNoContext(template string, data interface{}, w http.ResponseWriter, s *Session) *AppError

func RenderNoLayoutToHTML

func RenderNoLayoutToHTML(template string, data interface{}) (htemplate.HTML, *AppError)

func RenderNoLayoutToString

func RenderNoLayoutToString(template string, data interface{}) (string, *AppError)

func RenderString

func RenderString(c Context, content string) *AppError

func RenderToString

func RenderToString(layout, template string, data interface{}) (string, *AppError)

func ServerError

func ServerError(err error, msg string) *AppError

func StoreAndRedirect

func StoreAndRedirect(c Context, path string) *AppError

type Context

type Context interface {
	Request() *http.Request
	Writer() http.ResponseWriter
	Session() *Session

	Before() (bool, error)

	ResultData(title string) TemplateData

type ContextBuilder

type ContextBuilder func(*http.Request, http.ResponseWriter, *Session) Context

type ContextHandlerFunc

type ContextHandlerFunc func(Context) *AppError

type Env

type Env int
const (
	EnvProduction Env = iota
var Environment Env

type GPath

type GPath []string

func Path

func Path(r *http.Request, prefixLen int) GPath

func (GPath) Basename

func (p GPath) Basename() string

func (GPath) Extension

func (p GPath) Extension() string

type Session

type Session struct {
	// contains filtered or unexported fields

func (*Session) AddErrMsg

func (s *Session) AddErrMsg(msg string)

func (*Session) AddFlash

func (s *Session) AddFlash(msg string)

func (*Session) AddRecent

func (s *Session) AddRecent(key, val string)

func (*Session) Get

func (s *Session) Get(key string) (val string, ok bool)

func (*Session) GetAndResetErrMsgs

func (s *Session) GetAndResetErrMsgs() []string

func (*Session) GetAndResetFlash

func (s *Session) GetAndResetFlash() []string

func (*Session) GetList

func (s *Session) GetList(key string) []string

func (*Session) HasErrMsgs

func (s *Session) HasErrMsgs() bool

func (*Session) HasFlash

func (s *Session) HasFlash() bool

func (*Session) Push

func (s *Session) Push(key, val string)

pushes a value onto a string list

func (*Session) Remove

func (s *Session) Remove(key string)

func (*Session) Set

func (s *Session) Set(key, val string)

func (Session) Token

func (s Session) Token() string

func (Session) ValidToken

func (s Session) ValidToken(t string) bool

type TemplateData

type TemplateData map[string]interface{}

type TemplatePool

type TemplatePool interface {
	Register(name string, leftDelim, rightDelim string) error
	RegisterLayout(name, header, footer string, leftDelim, rightDelim string) error
	RegisterString(name, content string) error
	RegisterMulti(name, leftDelim, rightDelim string, filenames ...string) error
	Render(layout, name string, data interface{}, wr io.Writer) error
	RenderNoLayout(name string, data interface{}, wr io.Writer) error
	RenderMulti(layout, name, innerName string, data interface{}, wr io.Writer) error
var DefaultPool TemplatePool

Jump to

Keyboard shortcuts

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