web

package
v0.0.0-...-ed9fe1d Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2018 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Overview

Package web provides simple web application framework for golang

Index

Examples

Constants

View Source
const (
	RouterLoggerKey = "web.router"
)

LoggerKeys

Variables

View Source
var DefaultOption = &Option{
	HMACKey: xhmac.NewBase64([]byte("speedland"), sha256.New),
}

NotFound is the default response for 404

Functions

This section is empty.

Types

type Handler

type Handler interface {
	// Process serve http request and return the new http request and/or *response.Response value,
	Process(*Request, NextHandler) *response.Response
}

Handler is an interface to process the request and make a *response.Response

type HandlerFunc

type HandlerFunc func(*Request, NextHandler) *response.Response

HandlerFunc is a func to implement Handler interface.

func (HandlerFunc) Process

func (h HandlerFunc) Process(r *Request, next NextHandler) *response.Response

Process implements Handler.Process

type NextHandler

type NextHandler func(*Request) *response.Response

NextHandler is an alias to call the next handler in pipeline

type Option

type Option struct {
	// Option for hmac signature key, must not be nil. The default key is "speedland"
	HMACKey *xhmac.Base64
	// Option to initialize the request context. The default is nil.
	InitContext func(r *http.Request) context.Context
}

Option provies the option fields for web package.

type PathPattern

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

PathPattern is a struct to support path parameters and matches incoming request paths.

func CompilePathPattern

func CompilePathPattern(pattern string) (*PathPattern, error)

CompilePathPattern compiles the path pattern string to *PathPattern A path parameter name must be [a-zA-Z0-9_]+ with : and * prefix to define the matching storategy.

  • /:something/ is a pattern to match something (except '/') on the path and capture the parameter value as 'something'.
  • /*anything/ is a pattern to match anything (including '/') on the path and capture the parameter value as 'anything'
  • /*/ is a pattern to match anything and no parameter capturing.

func MustCompilePathPattern

func MustCompilePathPattern(pattern string) *PathPattern

MustCompilePathPattern is like CompilePathPattern but panics if an error occurrs.

func (*PathPattern) Match

func (pattern *PathPattern) Match(path string) (*keyvalue.GetProxy, bool)

Match execute the matching with the given path and return the parameter values or nil

type Request

type Request struct {
	*http.Request

	// common request scoped values
	ID      uuid.UUID
	Params  *keyvalue.GetProxy
	Query   *keyvalue.GetProxy
	Form    *keyvalue.GetProxy
	Cookies *keyvalue.GetProxy

	Option *Option
}

Request is a wrapper for net/http.Request The original `*net/http.Request` functions and fields are embedded in struct and provides some utility functions (especially to support context.Context)

func FromContext

func FromContext(ctx context.Context) *Request

FromContext returns a *Request associated with the context.

func NewRequest

func NewRequest(r *http.Request, option *Option) *Request

NewRequest returns a new *Request

func (*Request) Get

func (r *Request) Get(key interface{}) (interface{}, error)

Get implements keyvalue.Getter to enable keyvalue.GetProxy for context values.

func (*Request) WithContext

func (r *Request) WithContext(ctx context.Context) *Request

WithContext returns a shallow copy of r with its context changed to ctx. The provided ctx must be non-nil.

func (*Request) WithValue

func (r *Request) WithValue(key interface{}, value interface{}) *Request

WithValue sets the request-scoped value with the in-flight http request and return a shallow copied request. This is shorthand for `req.WithContext(context.WithValue(req.Context(), key, value))`

type Router

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

Router is a http traffic router

Example
router := NewRouter(nil)
router.Get("/path/to/:page.html",
	HandlerFunc(func(req *Request, _ NextHandler) *response.Response {
		return response.NewText(req.Params.GetStringOr("page", ""))
	}),
)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/path/to/bar.html", nil)
router.ServeHTTP(w, req)
fmt.Printf("*response.Response: %q", w.Body)
Output:

*response.Response: "bar"
Example (MultipleHandlerPipeline)
router := NewRouter(nil)
router.Get("/path/to/:page.html",
	HandlerFunc(func(req *Request, next NextHandler) *response.Response {
		if req.Params.GetStringOr("page", "") == "first" {
			return response.NewText("First Handler")
		}
		return next(req)
	}),
	HandlerFunc(func(req *Request, next NextHandler) *response.Response {
		// This handler is reached only when the first handler returns nil
		if req.Params.GetStringOr("page", "") == "second" {
			return response.NewText("Second Handler")
		}
		return nil
	}),
)
for _, s := range []string{"first", "second"} {
	w := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", fmt.Sprintf("/path/to/%s.html", s), nil)
	router.ServeHTTP(w, req)
	fmt.Printf("*response.Response: %q\n", w.Body)
}
Output:

*response.Response: "First Handler"
*response.Response: "Second Handler"

func NewRouter

func NewRouter(option *Option) *Router

NewRouter returns a new *Router

func (*Router) Delete

func (r *Router) Delete(pattern string, handlers ...Handler)

Delete adds handlers for "DELETE {pattern}" requests

func (*Router) Get

func (r *Router) Get(pattern string, handlers ...Handler)

Get adds handlers for "GET {pattern}" requests

func (*Router) Post

func (r *Router) Post(pattern string, handlers ...Handler)

Post adds handlers for "POST {pattern}" requests

func (*Router) Put

func (r *Router) Put(pattern string, handlers ...Handler)

Put adds handlers for "PUT {pattern}" requests

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

Dispatch dispaches *http.Request to the matched handlers and return Response

func (*Router) Use

func (r *Router) Use(handlers ...Handler)

Use adds middleware handlers to process on every request before all handlers are processed.

Example
router := NewRouter(nil)
router.Use(HandlerFunc(func(req *Request, next NextHandler) *response.Response {
	return next(req.WithValue(
		"my-middleware-key",
		"my-middleware-value",
	))
}))
router.Get("/a.html",
	HandlerFunc(func(req *Request, next NextHandler) *response.Response {
		v, _ := req.Get("my-middleware-key")
		return response.NewText(v.(string))
	}),
)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/a.html", nil)
router.ServeHTTP(w, req)
fmt.Printf("*response.Response: %q\n", w.Body)
Output:

*response.Response: "my-middleware-value"

Directories

Path Synopsis
middleware
oauth2
Package oauth2 provides oauth2 middleware
Package oauth2 provides oauth2 middleware
session/sessiontest
Package sessiontest provides session test helper
Package sessiontest provides session test helper
Package value provides lazy.Value for web context.
Package value provides lazy.Value for web context.

Jump to

Keyboard shortcuts

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