webhelp.v1: gopkg.in/webhelp.v1 Index | Examples | Files | Directories

package webhelp

import "gopkg.in/webhelp.v1"

package webhelp is a bunch of useful utilities for doing web programming in Go. webhelp encourages you to use the standard library for web programming, but provides some oft-needed tools to help simplify the task.

webhelp tightly integrates with the new Go 1.7 Request Context support, but has backported the functionality to previous Go releases in the whcompat subpackage.

Recently I wrote a long blog post about how to use webhelp: http://www.jtolds.com/writing/2017/01/writing-advanced-web-applications-with-go/

Code:

package main

import (
    "fmt"
    "net/http"

    "gopkg.in/webhelp.v1/whcompat"
    "gopkg.in/webhelp.v1/whlog"
    "gopkg.in/webhelp.v1/whmux"
)

var (
    pageName = whmux.NewStringArg()
)

func page(w http.ResponseWriter, r *http.Request) {
    name := pageName.Get(whcompat.Context(r))

    w.Header().Set("Content-Type", "text/plain")
    fmt.Fprintf(w, "Welcome to %s", name)
}

func main() {
    pageHandler := pageName.Shift(whmux.Exact(http.HandlerFunc(page)))

    whlog.ListenAndServe(":0", whmux.Dir{
        "wiki": pageHandler,
    })
}

Index

Examples

Package Files

gensym.go pkg.go

type ContextKey Uses

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

ContextKey is only useful via the GenSym() constructor. See GenSym() for more documentation

func GenSym Uses

func GenSym() ContextKey

GenSym generates a brand new, never-before-seen ContextKey for use as a Context.WithValue key. Please see the example.

Code:

package main

import (
    "fmt"
    "net/http"

    "golang.org/x/net/context"
    "gopkg.in/webhelp.v1"
    "gopkg.in/webhelp.v1/whcompat"
    "gopkg.in/webhelp.v1/wherr"
    "gopkg.in/webhelp.v1/whlog"
    "gopkg.in/webhelp.v1/whroute"
)

var (
    UserKey = webhelp.GenSym()
)

type User struct {
    Name string
}

func loadUser(r *http.Request) (user *User, err error) {
    return nil, wherr.InternalServerError.New("not implemented yet")
}

// myWrapper will load the user from a request, serving any detected errors,
// and otherwise passing the request along to the wrapped handler with the
// user bound inside the context.
func myWrapper(h http.Handler) http.Handler {
    return whroute.HandlerFunc(h,
        func(w http.ResponseWriter, r *http.Request) {

            user, err := loadUser(r)
            if err != nil {
                wherr.Handle(w, r, err)
                return
            }

            h.ServeHTTP(w, whcompat.WithContext(r,
                context.WithValue(whcompat.Context(r), UserKey, user)))
        })
}

// myHandler is a standard http.HandlerFunc that expects to be able to load
// a user out of the request context.
func myHandler(w http.ResponseWriter, r *http.Request) {
    ctx := whcompat.Context(r)
    if user, ok := ctx.Value(UserKey).(*User); ok {
        // do something with the user
        fmt.Fprint(w, user.Name)
    }
}

// Routes returns an http.Handler. You might have a whmux.Dir or something
// in here.
func Routes() http.Handler {
    return myWrapper(http.HandlerFunc(myHandler))
}

func main() {
    whlog.ListenAndServe(":0", Routes())
}

Directories

PathSynopsis
whauthPackage whauth provides some helper methods and handlers for dealing with HTTP basic auth
whcachePackage whcache provides a mechanism for per-request computation caching
whcompatPackage whcompat provides webhelp compatibility across different Go releases.
wherrPackage wherr provides a unified error handling framework for http.Handlers.
whfatalPackage whfatal uses panics to make early termination of http.Handlers easier.
whglsPackage whgls provides webhelp tools that use grossness enabled by the github.com/jtolds/gls package.
whjsonPackage whjson provides some nice utilities for dealing with JSON-based APIs, such as a good JSON wherr.Handler.
whlogPackage whlog provides functionality to log incoming requests and results.
whmonPackage whmon provides a means to monitor various aspects of how the request and response is going.
whmuxPackage whmux provides some useful request mux helpers for demultiplexing requests to one of a number of handlers.
whparsePackage whparse provides some convenient input parsing helpers.
whredirPackage whredir provides some helper methods and handlers for redirecting incoming requests to other URLs.
whroutePackage whroute provides utilities to implement route listing, whereby http.Handlers that opt in can list what routes they understand.
whsesspackage whsess is a lightweight session storage mechanism for the webhelp package.
whtmplPackage whtmpl provides some helpful utilities for constructing and using lots of html/templates

Package webhelp imports 1 packages (graph) and is imported by 6 packages. Updated 2017-05-31. Refresh now. Tools for package owners.