/
web.go
112 lines (101 loc) · 4.72 KB
/
web.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Package web provides a fast and flexible middleware stack and mux.
This package attempts to solve three problems that net/http does not. First, it
allows you to specify flexible patterns, including routes with named parameters
and regular expressions. Second, it allows you to write reconfigurable
middleware stacks. And finally, it allows you to attach additional context to
requests, in a manner that can be manipulated by both compliant middleware and
handlers.
*/
package web
import (
"net/http"
)
/*
C is a request-local context object which is threaded through all compliant
middleware layers and given to the final request handler.
*/
type C struct {
// URLParams is a map of variables extracted from the URL (typically
// from the path portion) during routing. See the documentation for the
// URL Pattern you are using (or the documentation for PatternType for
// the case of standard pattern types) for more information about how
// variables are extracted and named.
URLParams map[string]string
// Env is a free-form environment for storing request-local data. Keys
// may be arbitrary types that support equality, however package-private
// types with type-safe accessors provide a convenient way for packages
// to mediate access to their request-local data.
Env map[interface{}]interface{}
}
// Handler is similar to net/http's http.Handler, but also accepts a Goji
// context object.
type Handler interface {
ServeHTTPC(C, http.ResponseWriter, *http.Request)
}
// HandlerFunc is similar to net/http's http.HandlerFunc, but supports a context
// object. Implements both http.Handler and Handler.
type HandlerFunc func(C, http.ResponseWriter, *http.Request)
// ServeHTTP implements http.Handler, allowing HandlerFunc's to be used with
// net/http and other compliant routers. When used in this way, the underlying
// function will be passed an empty context.
func (h HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h(C{}, w, r)
}
// ServeHTTPC implements Handler.
func (h HandlerFunc) ServeHTTPC(c C, w http.ResponseWriter, r *http.Request) {
h(c, w, r)
}
/*
PatternType is the type denoting Patterns and types that Goji internally
converts to Pattern (via the ParsePattern function). In order to provide an
expressive API, this type is an alias for interface{} (that is named for the
purposes of documentation), however only the following concrete types are
accepted:
- types that implement Pattern
- string, which is interpreted as a Sinatra-like URL pattern. In
particular, the following syntax is recognized:
- a path segment starting with with a colon will match any
string placed at that position. e.g., "/:name" will match
"/carl", binding "name" to "carl".
- a pattern ending with "/*" will match any route with that
prefix. For instance, the pattern "/u/:name/*" will match
"/u/carl/" and "/u/carl/projects/123", but not "/u/carl"
(because there is no trailing slash). In addition to any names
bound in the pattern, the special key "*" is bound to the
unmatched tail of the match, but including the leading "/". So
for the two matching examples above, "*" would be bound to "/"
and "/projects/123" respectively.
Unlike http.ServeMux's patterns, string patterns support neither the
"rooted subtree" behavior nor Host-specific routes. Users who require
either of these features are encouraged to compose package http's mux
with the mux provided by this package.
- regexp.Regexp, which is assumed to be a Perl-style regular expression
that is anchored on the left (i.e., the beginning of the string). If
your regular expression is not anchored on the left, a
hopefully-identical left-anchored regular expression will be created
and used instead.
Capturing groups will be converted into bound URL parameters in
URLParams. If the capturing group is named, that name will be used;
otherwise the special identifiers "$1", "$2", etc. will be used.
*/
type PatternType interface{}
/*
HandlerType is the type of Handlers and types that Goji internally converts to
Handler. In order to provide an expressive API, this type is an alias for
interface{} (that is named for the purposes of documentation), however only the
following concrete types are accepted:
- types that implement http.Handler
- types that implement Handler
- func(http.ResponseWriter, *http.Request)
- func(web.C, http.ResponseWriter, *http.Request)
*/
type HandlerType interface{}
/*
MiddlewareType is the type of Goji middleware. In order to provide an expressive
API, this type is an alias for interface{} (that is named for the purposes of
documentation), however only the following concrete types are accepted:
- func(http.Handler) http.Handler
- func(*web.C, http.Handler) http.Handler
*/
type MiddlewareType interface{}