Documentation ¶
Overview ¶
Package web provides simple web application framework for golang
Index ¶
- Constants
- Variables
- type Handler
- type HandlerFunc
- type NextHandler
- type Option
- type PathPattern
- type Request
- type Router
- func (r *Router) Delete(pattern string, handlers ...Handler)
- func (r *Router) Get(pattern string, handlers ...Handler)
- func (r *Router) Post(pattern string, handlers ...Handler)
- func (r *Router) Put(pattern string, handlers ...Handler)
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) Use(handlers ...Handler)
Examples ¶
Constants ¶
const (
RouterLoggerKey = "web.router"
)
LoggerKeys
Variables ¶
var DefaultOption = &Option{ HMACKey: xhmac.NewBase64([]byte("speedland"), sha256.New), }
var NotFound = response.NewTextWithStatus("not found", response.HTTPStatusNotFound)
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 ¶
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.
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 ¶
FromContext returns a *Request associated with the context.
func NewRequest ¶
NewRequest returns a new *Request
func (*Request) Get ¶
Get implements keyvalue.Getter to enable keyvalue.GetProxy for context values.
func (*Request) WithContext ¶
WithContext returns a shallow copy of r with its context changed to ctx. The provided ctx must be non-nil.
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 (*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 ¶
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. |