Back to godoc.org
gopkg.in/webhelp.v1

Package webhelp

v1.0.0-...-3f30213
Latest Go to latest

The latest major version is v1.

Published: May 30, 2017 | License: Apache-2.0 | Module: gopkg.in/webhelp.v1

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

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

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

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())
}

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier