import "github.com/gocraft/web"
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
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.
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(rw ResponseWriter, req *Request, next NextMiddlewareFunc)
LoggerMiddleware is generic middleware that will log requests to Logger (by default, Stdout).
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(path string, option ...StaticOption) func(ResponseWriter, *Request, NextMiddlewareFunc)
StaticMiddleware is the same as StaticMiddlewareFromDir, but accepts a path string for backwards compatibility.
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 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 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 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 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 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.
IsRouted can be called from middleware to determine if the request has been routed yet.
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 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 struct {
// contains filtered or unexported fields
}
Router implements net/http's Handler interface and is what you attach middleware, routes/handlers, and subrouters to.
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.
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".
Delete will add a route to the router that matches on DELETE requests and the specified path.
Error sets the specified function as the error handler (when panics happen) and returns the router.
Get will add a route to the router that matches on GET requests and the specified path.
Head will add a route to the router that matches on HEAD requests and the specified path.
Middleware adds the specified middleware tot he router and returns the 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.
Options will add a route to the router that matches on OPTIONS requests and the specified path.
OptionsHandler sets the specified function as the options handler and returns the router. Note that only the root router can have a OptionsHandler handler.
Patch will add a route to the router that matches on PATCH requests and the specified path.
Post will add a route to the router that matches on POST requests and the specified path.
Put will add a route to the router that matches on PUT requests and the specified path.
This is the entry point for servering all requests.
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.
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 223 packages. Updated 2020-05-02. Refresh now. Tools for package owners.