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 RouteOptions
- type Router
- func (r *Router) Delete(path string, fn interface{}, options ...RouteOptions) *Router
- func (r *Router) Error(fn interface{}) *Router
- func (r *Router) Get(path string, fn interface{}, options ...RouteOptions) *Router
- func (r *Router) Head(path string, fn interface{}, options ...RouteOptions) *Router
- func (r *Router) Middleware(fn interface{}) *Router
- func (r *Router) NotFound(fn interface{}) *Router
- func (r *Router) Options(path string, fn interface{}, options ...RouteOptions) *Router
- func (r *Router) OptionsHandler(fn interface{}) *Router
- func (r *Router) Patch(path string, fn interface{}, options ...RouteOptions) *Router
- func (r Router) PathPrefix() string
- func (r *Router) Post(path string, fn interface{}, options ...RouteOptions) *Router
- func (r *Router) Put(path string, fn interface{}, options ...RouteOptions) *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.
func (*Request) IsRouted ¶
IsRouted can be called from middleware to determine if the request has been routed yet.
func (*Request) RouteOptions ¶
func (r *Request) RouteOptions() RouteOptions
RouteOptions returns the RouteOptions that were given during route setup
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 RouteOptions ¶
type RouteOptions struct { // RequestSchemaURL is a url to the request schema that you may want to validate against in a Middleware // This may be a URL to a local file using the file:// scheme RequestSchemaURL *url.URL // ResponseSchemaURL is a url to the response schema that you may want to validate against in a Middleware // This may be a URL to a local file using the file:// scheme ResponseSchemaURL *url.URL // Data is a dictionary of extra data that may be useful to check against Data map[string]interface{} }
RouteOptions contain optional information that may be useful at serving time. For example the RequestSchemaURL could be used to verify the Request Body is validated against the schema
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 ¶
func (r *Router) Delete(path string, fn interface{}, options ...RouteOptions) *Router
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 ¶
func (r *Router) Get(path string, fn interface{}, options ...RouteOptions) *Router
Get will add a route to the router that matches on GET requests and the specified path.
func (*Router) Head ¶
func (r *Router) Head(path string, fn interface{}, options ...RouteOptions) *Router
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 ¶
func (r *Router) Options(path string, fn interface{}, options ...RouteOptions) *Router
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 ¶
func (r *Router) Patch(path string, fn interface{}, options ...RouteOptions) *Router
Patch will add a route to the router that matches on PATCH requests and the specified path.
func (Router) PathPrefix ¶
GetPathPrefix return the path prefix for debug purposes
func (*Router) Post ¶
func (r *Router) Post(path string, fn interface{}, options ...RouteOptions) *Router
Post will add a route to the router that matches on POST requests and the specified path.
func (*Router) Put ¶
func (r *Router) Put(path string, fn interface{}, options ...RouteOptions) *Router
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.