README

webhelp

A bunch of useful utilities for whenever I do web programming in Go. Like a framework, but better, cause it's not.

Docs

https://godoc.org/gopkg.in/webhelp.v1

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/

See also the OAuth2 extensions (https://github.com/go-webhelp/whoauth2) or the Goth extensions (https://github.com/go-webhelp/whgoth).

Documentation

Overview

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/

Example
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,
	})
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContextKey

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

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

func GenSym

func GenSym() ContextKey

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

Example
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())
}
Output:

Source Files

Directories

Path Synopsis
Package whauth provides some helper methods and handlers for dealing with HTTP basic auth
Package whauth provides some helper methods and handlers for dealing with HTTP basic auth
Package whcache provides a mechanism for per-request computation caching Sometimes you have a helper method that performs a computation or interacts with a datastore or remote resource, and that helper method gets called repeatedly.
Package whcache provides a mechanism for per-request computation caching Sometimes you have a helper method that performs a computation or interacts with a datastore or remote resource, and that helper method gets called repeatedly.
Package whcompat provides webhelp compatibility across different Go releases.
Package whcompat provides webhelp compatibility across different Go releases.
Package wherr provides a unified error handling framework for http.Handlers.
Package wherr provides a unified error handling framework for http.Handlers.
Package whfatal uses panics to make early termination of http.Handlers easier.
Package whfatal uses panics to make early termination of http.Handlers easier.
Package whgls provides webhelp tools that use grossness enabled by the github.com/jtolds/gls package.
Package whgls provides webhelp tools that use grossness enabled by the github.com/jtolds/gls package.
Package whjson provides some nice utilities for dealing with JSON-based APIs, such as a good JSON wherr.Handler.
Package whjson provides some nice utilities for dealing with JSON-based APIs, such as a good JSON wherr.Handler.
Package whlog provides functionality to log incoming requests and results.
Package whlog provides functionality to log incoming requests and results.
Package whmon provides a means to monitor various aspects of how the request and response is going.
Package whmon provides a means to monitor various aspects of how the request and response is going.
Package whmux provides some useful request mux helpers for demultiplexing requests to one of a number of handlers.
Package whmux provides some useful request mux helpers for demultiplexing requests to one of a number of handlers.
Package whparse provides some convenient input parsing helpers.
Package whparse provides some convenient input parsing helpers.
Package whredir provides some helper methods and handlers for redirecting incoming requests to other URLs.
Package whredir provides some helper methods and handlers for redirecting incoming requests to other URLs.
Package whroute provides utilities to implement route listing, whereby http.Handlers that opt in can list what routes they understand.
Package whroute provides utilities to implement route listing, whereby http.Handlers that opt in can list what routes they understand.
package whsess is a lightweight session storage mechanism for the webhelp package.
package whsess is a lightweight session storage mechanism for the webhelp package.
Package whtmpl provides some helpful utilities for constructing and using lots of html/templates
Package whtmpl provides some helpful utilities for constructing and using lots of html/templates