Documentation ¶
Index ¶
- Variables
- func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error)
- func RecordJSONResponse(t *testing.T, resp Response, v interface{}) *httptest.ResponseRecorder
- type App
- type BasicRouter
- type Context
- type Decorator
- type GlobParser
- type HTTPError
- type HTTPResponse
- type Handler
- type Handlers
- type Response
- type ResponseFunc
- type Route
- type RouteMatch
- type Router
- type RouterFunc
- type TemplateParser
- type TemplateParserFunc
Constants ¶
This section is empty.
Variables ¶
var ( HTMLHeaders = map[string]string{"Content-Type": "text/html"} JSONHeaders = map[string]string{"Content-Type": "application/json"} TextHeaders = map[string]string{"Content-Type": "text/plain"} CORSHeaders = map[string]string{ "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Credentials": "true", "Access-Control-Allow-Headers": "Authorization, Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers", "Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE, COPY, HEAD, OPTIONS, LINK, UNLINK, CONNECT, TRACE, PURGE", } )
Functions ¶
func DefaultErrorHandler ¶
func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error)
DefaultErrorHandler is the default ErrorHandler used by an App If the error implements the Response interface, it will call its Write function Otherwise, a 500 with the error message is returned
func RecordJSONResponse ¶
func RecordJSONResponse(t *testing.T, resp Response, v interface{}) *httptest.ResponseRecorder
Types ¶
type App ¶
type App struct { // The After function is called after each request has completed After func(http.ResponseWriter, *http.Request) // The Before function is called before each request is routed Before func(http.ResponseWriter, *http.Request) // The ErrorHandler is called whenever a Handler returns a non-nil error ErrorHandler func(http.ResponseWriter, *http.Request, error) // The NotFoundHandler is called whenever the Router returns a nil RouteMatch NotFoundHandler func(http.ResponseWriter, *http.Request) // The template parser is passed into the Context Parser TemplateParser // The router is used to match a request to a Handler whenever a request is made Router Router }
App is the main structure of fireball applications. It can be invoked as an http.Handler
type BasicRouter ¶
type BasicRouter struct { Routes []*Route // contains filtered or unexported fields }
BasicRouter attempts to match requests based on its Routes. This router supports variables in the URL by using ":variable" notation in URL sections. For example, the following are all valid Paths:
"/home" "/movies/:id" "/users/:userID/purchases/:purchaseID"
Matched Path Variables can be retrieved in Handlers by the Context:
func Handler(c *Context) (Response, error) { id := c.PathVariables["id"] ... }
func NewBasicRouter ¶
func NewBasicRouter(routes []*Route) *BasicRouter
NewBasicRouter returns a new BasicRouter with the specified Routes
func (*BasicRouter) Match ¶
func (r *BasicRouter) Match(req *http.Request) (*RouteMatch, error)
Match attempts to match the *http.Request to a Route. Successful matches are cached for improved performance.
type Context ¶
type Context struct { // PathVariables are the URL-related variables returned by the Router PathVariables map[string]string // Meta can be used to pass information along Decorators Meta map[string]interface{} // Parser is used to render html templates Parser TemplateParser // Request is the originating *http.Request Request *http.Request }
Context is passed into Handlers It contains fields and helper functions related to the request
type Decorator ¶
A Decorator wraps logic around a Handler
func BasicAuthDecorator ¶
BasicAuthDecorator will add basic authentication using the specified username and password
func HeaderResponseDecorator ¶
HeaderResponseDecorator will add the specified headers to each response
func LogDecorator ¶
func LogDecorator() Decorator
LogDecorator will print the method and url of each request
type GlobParser ¶
GlobParser generates a template by recusively searching the specified root directory and parses templates that match the specified glob pattern
func NewGlobParser ¶
func NewGlobParser(root, glob string) *GlobParser
NewGlobParser returns a GlobParser with the specified root and glob pattern
func (*GlobParser) Parse ¶
func (p *GlobParser) Parse() (*template.Template, error)
Parse recursively searches the root directory and parses templates that match the specified glob pattern. Template names are generated by path/from/root + filename.
For example, if GlobParser.Root == "views", the following template names would be generated:
Files: views/ index.html partials/ login.html Template Names: "index.html" "partials/login.html"
type HTTPError ¶
type HTTPError struct { *HTTPResponse Err error }
HTTPError implements the Response and Error interfaces
func NewJSONError ¶
NewJSONError returns a new HTTPError in JSON format
type HTTPResponse ¶
HTTPResponse objects write the specified status, headers, and body to a http.ResponseWriter
func HTML ¶
func HTML(parser TemplateParser, status int, templateName string, data interface{}) (*HTTPResponse, error)
HTML is a helper function that returns a response generated from the given templateName and data
func NewJSONResponse ¶
func NewJSONResponse(status int, data interface{}) (*HTTPResponse, error)
NewJSONResponse returns a new HTTPResponse in JSON format
func NewResponse ¶
func NewResponse(status int, body []byte, headers map[string]string) *HTTPResponse
NewResponse returns a new HTTPResponse with the specified status, body, and headers
func (*HTTPResponse) Write ¶
func (h *HTTPResponse) Write(w http.ResponseWriter, r *http.Request)
Write will write the specified status, headers, and body to the http.ResponseWriter
type Response ¶
type Response interface {
Write(http.ResponseWriter, *http.Request)
}
Response is an object that writes to an http.ResponseWriter A Response object implements the http.Handler interface
type ResponseFunc ¶
type ResponseFunc func(http.ResponseWriter, *http.Request)
ResponseFunc is a function which implements the Response interface
func (ResponseFunc) Write ¶
func (rf ResponseFunc) Write(w http.ResponseWriter, r *http.Request)
type Route ¶
type Route struct { // Path is used to determine if a request's URL matches this Route Path string // Handlers map common HTTP methods to different Handlers Handlers map[string]Handler }
Routes are used to map a request to a RouteMatch
func Decorate ¶
Decorate is a helper function that decorates each Handler in each Route with the given Decorators
func EnableCORS ¶
EnableCORS decorates each route by adding CORS headers to each response An OPTIONS Handler is added to each route if one doesn't already exist
type RouteMatch ¶
RouteMatch objects are returned by the router when a request is successfully matched
type Router ¶
type Router interface {
Match(*http.Request) (*RouteMatch, error)
}
Router is an interface that matches an *http.Request to a RouteMatch. If no matches are found, a nil RouteMatch should be returned.
type RouterFunc ¶
type RouterFunc func(*http.Request) (*RouteMatch, error)
RouterFunc is a function which implements the Router interface
func (RouterFunc) Match ¶
func (rf RouterFunc) Match(r *http.Request) (*RouteMatch, error)
type TemplateParser ¶
TemplateParser is an interface object that is used to parse HTML templates