Documentation ¶
Index ¶
- Variables
- func LoggerMiddleware(rw ResponseWriter, req *Request, next NextMiddlewareFunc)
- func ShowErrorsMiddleware(rw ResponseWriter, req *Request, next NextMiddlewareFunc)
- func StaticMiddleware(path string, option ...StaticOption) func(ResponseWriter, *Request, NextMiddlewareFunc)
- func StaticMiddlewareFromDir(dir http.FileSystem, options ...StaticOption) func(ResponseWriter, *Request, NextMiddlewareFunc)
- type GenericHandler
- type GenericMiddleware
- type NextMiddlewareFunc
- type PanicReporter
- type Request
- type ResponseWriter
- type Router
- func (r *Router) Delete(path string, fn interface{}) *Router
- func (r *Router) Error(fn interface{}) *Router
- func (r *Router) Get(path string, fn interface{}) *Router
- func (r *Router) Head(path string, fn interface{}) *Router
- func (r *Router) Middleware(fn interface{}) *Router
- func (r *Router) NotFound(fn interface{}) *Router
- func (r *Router) Options(path string, fn interface{}) *Router
- func (r *Router) OptionsHandler(fn interface{}) *Router
- func (r *Router) Patch(path string, fn interface{}) *Router
- func (r *Router) Post(path string, fn interface{}) *Router
- func (r *Router) Put(path string, fn interface{}) *Router
- func (rootRouter *Router) ServeHTTP(rw http.ResponseWriter, r *http.Request)
- func (r *Router) Subrouter(ctx interface{}, pathPrefix string) *Router
- type StaticOption
Constants ¶
This section is empty.
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.
Functions ¶
func LoggerMiddleware ¶
func LoggerMiddleware(rw ResponseWriter, req *Request, next NextMiddlewareFunc)
LoggerMiddleware is generic middleware that will log requests to Logger (by default, Stdout).
func ShowErrorsMiddleware ¶
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 ¶
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 ¶
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.
Types ¶
type GenericHandler ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
type ResponseWriter ¶
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 ¶
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 ¶
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 ¶
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 ¶
Delete will add a route to the router that matches on DELETE requests and the specified path.
func (*Router) Error ¶
Error sets the specified function as the error handler (when panics happen) and returns the router.
func (*Router) Get ¶
Get will add a route to the router that matches on GET requests and the specified path.
func (*Router) Head ¶
Head will add a route to the router that matches on HEAD requests and the specified path.
func (*Router) Middleware ¶
Middleware adds the specified middleware tot he router and returns the router.
func (*Router) NotFound ¶
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 ¶
Options will add a route to the router that matches on OPTIONS requests and the specified path.
func (*Router) OptionsHandler ¶
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 ¶
Patch will add a route to the router that matches on PATCH requests and the specified path.
func (*Router) Post ¶
Post will add a route to the router that matches on POST requests and the specified path.
func (*Router) Put ¶
Put will add a route to the router that matches on PUT requests and the specified path.
func (*Router) ServeHTTP ¶
func (rootRouter *Router) ServeHTTP(rw http.ResponseWriter, r *http.Request)
This is the entry point for servering all requests.
func (*Router) Subrouter ¶
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 ¶
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.