web: github.com/gocraft/web Index | Files

package web

import "github.com/gocraft/web"

Index

Package Files

logger_middleware.go options_handler.go panic_handler.go request.go response_writer.go router_serve.go router_setup.go show_errors_middleware.go static_middleware.go tree.go

Variables

var DefaultNotFoundResponse = "Not Found"

DefaultNotFoundResponse is the default text rendered when no route is found and no NotFound handlers are present.

var DefaultPanicResponse = "Application Error"

DefaultPanicResponse is the default text rendered when a panic occurs and no Error handlers are present.

var Logger = log.New(os.Stdout, "", 0)

Logger can be set to your own logger. Logger only applies to the LoggerMiddleware.

var PanicHandler = PanicReporter(logPanicReporter{
    log: log.New(os.Stderr, "ERROR ", log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile),
})

PanicHandler will be logged to in panic conditions (eg, division by zero in an app handler). Applications can set web.PanicHandler = your own logger, if they wish. In terms of logging the requests / responses, see logger_middleware. That is a completely separate system.

func LoggerMiddleware Uses

func LoggerMiddleware(rw ResponseWriter, req *Request, next NextMiddlewareFunc)

LoggerMiddleware is generic middleware that will log requests to Logger (by default, Stdout).

func ShowErrorsMiddleware Uses

func ShowErrorsMiddleware(rw ResponseWriter, req *Request, next NextMiddlewareFunc)

ShowErrorsMiddleware will catch panics and render an HTML page with the stack trace. This middleware should only be used in development.

func StaticMiddleware Uses

func StaticMiddleware(path string, option ...StaticOption) func(ResponseWriter, *Request, NextMiddlewareFunc)

StaticMiddleware is the same as StaticMiddlewareFromDir, but accepts a path string for backwards compatibility.

func StaticMiddlewareFromDir Uses

func StaticMiddlewareFromDir(dir http.FileSystem, options ...StaticOption) func(ResponseWriter, *Request, NextMiddlewareFunc)

StaticMiddlewareFromDir returns a middleware that serves static files from the specified http.FileSystem. This middleware is great for development because each file is read from disk each time and no special caching or cache headers are sent.

If a path is requested which maps to a folder with an index.html folder on your filesystem, then that index.html file will be served.

type GenericHandler Uses

type GenericHandler func(ResponseWriter, *Request)

GenericHandler are handlers that don't have or need a context. If your handler doesn't need a context, you can use this signature to get a small performance boost.

type GenericMiddleware Uses

type GenericMiddleware func(ResponseWriter, *Request, NextMiddlewareFunc)

GenericMiddleware are middleware that doesn't have or need a context. General purpose middleware, such as static file serving, has this signature. If your middlware doesn't need a context, you can use this signature to get a small performance boost.

type NextMiddlewareFunc Uses

type NextMiddlewareFunc func(ResponseWriter, *Request)

NextMiddlewareFunc are functions passed into your middleware. To advance the middleware, call the function. You should usually pass the existing ResponseWriter and *Request into the next middlware, but you can chose to swap them if you want to modify values or capture things written to the ResponseWriter.

type PanicReporter Uses

type PanicReporter interface {
    // Panic is called with the URL of the request, the result of calling recover, and the stack.
    Panic(url string, err interface{}, stack string)
}

PanicReporter can receive panics that happen when serving a request and report them to a log of some sort.

type Request Uses

type Request struct {
    *http.Request

    // PathParams exists if you have wildcards in your URL that you need to capture.
    // Eg, /users/:id/tickets/:ticket_id and /users/1/tickets/33 would yield the map {id: "3", ticket_id: "33"}
    PathParams map[string]string
    // contains filtered or unexported fields
}

Request wraps net/http's Request and gocraf/web specific fields. In particular, PathParams is used to access captures params in your URL. A Request is sent to handlers on each request.

func (*Request) IsRouted Uses

func (r *Request) IsRouted() bool

IsRouted can be called from middleware to determine if the request has been routed yet.

func (*Request) RoutePath Uses

func (r *Request) RoutePath() string

RoutePath returns the routed path string. Eg, if a route was registered with router.Get("/suggestions/:suggestion_id/comments", f), then RoutePath will return "/suggestions/:suggestion_id/comments".

type ResponseWriter Uses

type ResponseWriter interface {
    http.ResponseWriter
    http.Flusher
    http.Hijacker
    http.CloseNotifier

    // StatusCode returns the written status code, or 0 if none has been written yet.
    StatusCode() int
    // Written returns whether the header has been written yet.
    Written() bool
    // Size returns the size in bytes of the body written so far.
    Size() int
}

ResponseWriter includes net/http's ResponseWriter and adds a StatusCode() method to obtain the written status code. A ResponseWriter is sent to handlers on each request.

type Router Uses

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

Router implements net/http's Handler interface and is what you attach middleware, routes/handlers, and subrouters to.

func New Uses

func New(ctx interface{}) *Router

New returns a new router with context type ctx. ctx should be a struct instance, whose purpose is to communicate type information. On each request, an instance of this context type will be automatically allocated and sent to handlers.

func NewWithPrefix Uses

func NewWithPrefix(ctx interface{}, pathPrefix string) *Router

NewWithPrefix returns a new router (see New) but each route will have an implicit prefix. For instance, with pathPrefix = "/api/v2", all routes under this router will begin with "/api/v2".

func (*Router) Delete Uses

func (r *Router) Delete(path string, fn interface{}) *Router

Delete will add a route to the router that matches on DELETE requests and the specified path.

func (*Router) Error Uses

func (r *Router) Error(fn interface{}) *Router

Error sets the specified function as the error handler (when panics happen) and returns the router.

func (*Router) Get Uses

func (r *Router) Get(path string, fn interface{}) *Router

Get will add a route to the router that matches on GET requests and the specified path.

func (*Router) Head Uses

func (r *Router) Head(path string, fn interface{}) *Router

Head will add a route to the router that matches on HEAD requests and the specified path.

func (*Router) Middleware Uses

func (r *Router) Middleware(fn interface{}) *Router

Middleware adds the specified middleware tot he router and returns the router.

func (*Router) NotFound Uses

func (r *Router) NotFound(fn interface{}) *Router

NotFound sets the specified function as the not-found handler (when no route matches) and returns the router. Note that only the root router can have a NotFound handler.

func (*Router) Options Uses

func (r *Router) Options(path string, fn interface{}) *Router

Options will add a route to the router that matches on OPTIONS requests and the specified path.

func (*Router) OptionsHandler Uses

func (r *Router) OptionsHandler(fn interface{}) *Router

OptionsHandler sets the specified function as the options handler and returns the router. Note that only the root router can have a OptionsHandler handler.

func (*Router) Patch Uses

func (r *Router) Patch(path string, fn interface{}) *Router

Patch will add a route to the router that matches on PATCH requests and the specified path.

func (*Router) Post Uses

func (r *Router) Post(path string, fn interface{}) *Router

Post will add a route to the router that matches on POST requests and the specified path.

func (*Router) Put Uses

func (r *Router) Put(path string, fn interface{}) *Router

Put will add a route to the router that matches on PUT requests and the specified path.

func (*Router) ServeHTTP Uses

func (rootRouter *Router) ServeHTTP(rw http.ResponseWriter, r *http.Request)

This is the entry point for servering all requests.

func (*Router) Subrouter Uses

func (r *Router) Subrouter(ctx interface{}, pathPrefix string) *Router

Subrouter attaches a new subrouter to the specified router and returns it. You can use the same context or pass a new one. If you pass a new one, it must embed a pointer to the previous context in the first slot. You can also pass a pathPrefix that each route will have. If "" is passed, then no path prefix is applied.

type StaticOption Uses

type StaticOption struct {
    Prefix    string
    IndexFile string
}

StaticOption configures how StaticMiddlewareDir handles url paths and index files for directories. If set, Prefix is removed from the start of the url path before attempting to serve a directory or file. If set, IndexFile is the index file to serve when the url path maps to a directory.

Package web imports 13 packages (graph) and is imported by 187 packages. Updated 2017-09-25. Refresh now. Tools for package owners.