fireball: Index | Files

package fireball

import ""


Package Files

app.go context.go decorators.go error.go json.go parser.go response.go route.go router.go test_helpers.go utilities.go


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":     "" /* 155 byte string literal not displayed */,
        "Access-Control-Allow-Methods":     "GET, POST, PUT, PATCH, DELETE, COPY, HEAD, OPTIONS, LINK, UNLINK, CONNECT, TRACE, PURGE",

func DefaultErrorHandler Uses

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 Uses

func RecordJSONResponse(t *testing.T, resp Response, v interface{}) *httptest.ResponseRecorder

type App Uses

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

func NewApp Uses

func NewApp(routes []*Route) *App

NewApp returns a new App object with all of the default fields

func (*App) ServeHTTP Uses

func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)

type BasicRouter Uses

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:


Matched Path Variables can be retrieved in Handlers by the Context:

func Handler(c *Context) (Response, error) {
    id := c.PathVariables["id"]

func NewBasicRouter Uses

func NewBasicRouter(routes []*Route) *BasicRouter

NewBasicRouter returns a new BasicRouter with the specified Routes

func (*BasicRouter) Match Uses

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 Uses

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

func (*Context) HTML Uses

func (c *Context) HTML(status int, templateName string, data interface{}) (*HTTPResponse, error)

Context.HTML calls HTML with the Context's template parser

type Decorator Uses

type Decorator func(Handler) Handler

A Decorator wraps logic around a Handler

func BasicAuthDecorator Uses

func BasicAuthDecorator(username, password string) Decorator

BasicAuthDecorator will add basic authentication using the specified username and password

func HeaderResponseDecorator Uses

func HeaderResponseDecorator(headers map[string]string) Decorator

HeaderResponseDecorator will add the specified headers to each response

func LogDecorator Uses

func LogDecorator() Decorator

LogDecorator will print the method and url of each request

type GlobParser Uses

type GlobParser struct {
    Root string
    Glob string
    // contains filtered or unexported fields

GlobParser generates a template by recusively searching the specified root directory and parses templates that match the specified glob pattern

func NewGlobParser Uses

func NewGlobParser(root, glob string) *GlobParser

NewGlobParser returns a GlobParser with the specified root and glob pattern

func (*GlobParser) Parse Uses

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:


Template Names:

type HTTPError Uses

type HTTPError struct {
    Err error

HTTPError implements the Response and Error interfaces

func NewError Uses

func NewError(status int, err error, headers map[string]string) *HTTPError

NewError returns a new HTTPError

func NewJSONError Uses

func NewJSONError(status int, err error) (*HTTPError, error)

NewJSONError returns a new HTTPError in JSON format

func (*HTTPError) Error Uses

func (e *HTTPError) Error() string

Error calls the internal Err.Error function

type HTTPResponse Uses

type HTTPResponse struct {
    Status  int
    Body    []byte
    Headers map[string]string

HTTPResponse objects write the specified status, headers, and body to a http.ResponseWriter

func HTML Uses

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 Uses

func NewJSONResponse(status int, data interface{}) (*HTTPResponse, error)

NewJSONResponse returns a new HTTPResponse in JSON format

func NewResponse Uses

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 Uses

func (h *HTTPResponse) Write(w http.ResponseWriter, r *http.Request)

Write will write the specified status, headers, and body to the http.ResponseWriter

type Handler Uses

type Handler func(c *Context) (Response, error)

Handler performs the business logic on a request

type Handlers Uses

type Handlers map[string]Handler

Handlers maps a http method to a Handler

type Response Uses

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

func Redirect Uses

func Redirect(status int, url string) Response

Redirect wraps http.Redirect in a ResponseFunc

type ResponseFunc Uses

type ResponseFunc func(http.ResponseWriter, *http.Request)

ResponseFunc is a function which implements the Response interface

func (ResponseFunc) Write Uses

func (rf ResponseFunc) Write(w http.ResponseWriter, r *http.Request)

type Route Uses

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 Uses

func Decorate(routes []*Route, decorators ...Decorator) []*Route

Decorate is a helper function that decorates each Handler in each Route with the given Decorators

func EnableCORS Uses

func EnableCORS(routes []*Route) []*Route

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 Uses

type RouteMatch struct {
    Handler       Handler
    PathVariables map[string]string

RouteMatch objects are returned by the router when a request is successfully matched

type Router Uses

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 Uses

type RouterFunc func(*http.Request) (*RouteMatch, error)

RouterFunc is a function which implements the Router interface

func (RouterFunc) Match Uses

func (rf RouterFunc) Match(r *http.Request) (*RouteMatch, error)

type TemplateParser Uses

type TemplateParser interface {
    Parse() (*template.Template, error)

TemplateParser is an interface object that is used to parse HTML templates

type TemplateParserFunc Uses

type TemplateParserFunc func() (*template.Template, error)

TemplateParserFunc is a function which implements the TemplateParser interface

func (TemplateParserFunc) Parse Uses

func (tpf TemplateParserFunc) Parse() (*template.Template, error)

Package fireball imports 12 packages (graph) and is imported by 4 packages. Updated 2018-10-08. Refresh now. Tools for package owners.