Documentation ¶
Index ¶
- Constants
- type Context
- func (c *Context) Clear(res http.ResponseWriter)
- func (c *Context) ClientIp() string
- func (c *Context) ContinueRequest()
- func (c *Context) GetLocal(key string) (value interface{}, exists bool)
- func (c *Context) MustGetLocal(key string) interface{}
- func (c *Context) PerformRequest()
- func (c *Context) SetLocal(key string, value interface{})
- type HandlerFunc
- type HandlersChain
- type Param
- type Params
- type Response
- func (r *Response) Clear(writer http.ResponseWriter)
- func (r *Response) CloseNotify() <-chan bool
- func (r *Response) Flush()
- func (r *Response) HEAD(status int)
- func (r *Response) HTML(status int, html string) (err error)
- func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (r *Response) JSON(status int, obj interface{}) (err error)
- func (r *Response) Render(status int, contentType string)
- func (r *Response) Rendered() bool
- func (r *Response) Status() int
- func (r *Response) Text(status int, text string) (err error)
- func (r *Response) WriteHeader(status int)
- type RouteGroup
- func (group *RouteGroup) Any(relativePath string, handlers ...HandlerFunc) RouteHandler
- func (group *RouteGroup) AnyRaw(relativePath string, raw http.Handler) RouteHandler
- func (group *RouteGroup) BasePath() string
- func (group *RouteGroup) DELETE(relativePath string, handlers ...HandlerFunc) RouteHandler
- func (group *RouteGroup) Directory(relativePath, root string) RouteHandler
- func (group *RouteGroup) File(relativePath, filepath string) RouteHandler
- func (group *RouteGroup) GET(relativePath string, handlers ...HandlerFunc) RouteHandler
- func (group *RouteGroup) Group(relativePath string, handlers ...HandlerFunc) *RouteGroup
- func (group *RouteGroup) HEAD(relativePath string, handlers ...HandlerFunc) RouteHandler
- func (group *RouteGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) RouteHandler
- func (group *RouteGroup) HandleRaw(httpMethod, relativePath string, raw http.Handler) RouteHandler
- func (group *RouteGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) RouteHandler
- func (group *RouteGroup) PATCH(relativePath string, handlers ...HandlerFunc) RouteHandler
- func (group *RouteGroup) POST(relativePath string, handlers ...HandlerFunc) RouteHandler
- func (group *RouteGroup) PUT(relativePath string, handlers ...HandlerFunc) RouteHandler
- func (group *RouteGroup) Use(middleware ...HandlerFunc) RouteHandler
- type RouteHandler
- type RouteInfo
- type Server
- func (s *Server) AllowedMethods(path string) []string
- func (s *Server) DescribeOptions(handlers ...HandlerFunc)
- func (s *Server) IsAvailable() bool
- func (s *Server) NoMethod(handlers ...HandlerFunc)
- func (s *Server) NotFound(handlers ...HandlerFunc)
- func (s *Server) Routes() (routes []RouteInfo)
- func (s *Server) Run(addr ...string) error
- func (s *Server) RunTLS(addr string, certFile string, keyFile string) (err error)
- func (s *Server) RunUnix(file string) (err error)
- func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request)
- func (s *Server) SetAvailable(available bool)
- func (s *Server) Unavailable(handlers ...HandlerFunc)
Constants ¶
const ( HeaderContentType = "Content-Type" ContentTypeHTML = "text/html; charset=utf-8" ContentTypeJSON = "application/json; charset=utf-8" ContentTypeText = "text/plain; charset=utf-8" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct { Request *http.Request Response Response Params Params // Params from the path (eg. /thing/:id) Locals map[string]interface{} // Local values set by middleware Debug bool // contains filtered or unexported fields }
Context manages the control flow of middleware
func (*Context) Clear ¶
func (c *Context) Clear(res http.ResponseWriter)
Clear resets the context so it can be used by another request
func (*Context) ClientIp ¶
ClientIp implements a best effort algorithm to return the real client IP, it parses X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.
func (*Context) ContinueRequest ¶
func (c *Context) ContinueRequest()
ContinueRequest asks the server to call the next handler for this request after the current handler function has returned. This should be preferred over PerformRequest().Now() because it keeps the program stack simpler.
func (*Context) MustGetLocal ¶
Returns the value for the given key if it exists, otherwise it panics.
func (*Context) PerformRequest ¶
func (c *Context) PerformRequest()
PerformRequest is used to handle the request immediately. It can be used instead of ContinueRequest when it must perform logic after the normal request handler has run. If neither method is called, then the server does not run any more request handlers.
PerformRequest should be used to initiate the request for the first time; if there are no handlers when PerformRequest is called, Go will panic with an index out of range runtime error.
type HandlerFunc ¶
type HandlerFunc func(c *Context)
type HandlersChain ¶
type HandlersChain []HandlerFunc
func (HandlersChain) Last ¶
func (chain HandlersChain) Last() HandlerFunc
Last returns the last handler in the chain. ie. the last handler is the main one.
type Params ¶
type Params []Param
Params is a Param-slice, as returned by the server. The slice is ordered, the first URL parameter is also the first slice value. It is therefore safe to read values by the index.
type Response ¶
type Response struct { http.ResponseWriter // contains filtered or unexported fields }
func (*Response) Clear ¶
func (r *Response) Clear(writer http.ResponseWriter)
func (*Response) CloseNotify ¶
Implements the http.CloseNotify interface
func (*Response) WriteHeader ¶
Override http.ResponseWriter's WriteHeader method
type RouteGroup ¶
type RouteGroup struct { Handlers HandlersChain // contains filtered or unexported fields }
RouteGroup is used internally to configure a server, a RouteGroup is associated with a prefix and an array of handlers (middleware)
func (*RouteGroup) Any ¶
func (group *RouteGroup) Any(relativePath string, handlers ...HandlerFunc) RouteHandler
Any registers a route that matches all the HTTP methods. GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE
func (*RouteGroup) AnyRaw ¶
func (group *RouteGroup) AnyRaw(relativePath string, raw http.Handler) RouteHandler
AnyRaw registers a route matching all methods, but handled with a raw http.Handler
func (*RouteGroup) BasePath ¶
func (group *RouteGroup) BasePath() string
func (*RouteGroup) DELETE ¶
func (group *RouteGroup) DELETE(relativePath string, handlers ...HandlerFunc) RouteHandler
Delete is a shortcut for server.Handle("DELETE", path, handle)
func (*RouteGroup) Directory ¶
func (group *RouteGroup) Directory(relativePath, root string) RouteHandler
Directory serves files from the given file system directory. eg. router.Directory("/", "/var/www")
func (*RouteGroup) File ¶
func (group *RouteGroup) File(relativePath, filepath string) RouteHandler
File registers a single route in order to server a single file of the local filesystem. eg. router.File("favicon.ico", "./resources/favicon.ico")
func (*RouteGroup) GET ¶
func (group *RouteGroup) GET(relativePath string, handlers ...HandlerFunc) RouteHandler
Get is a shortcut for server.Handle("GET", path, handle)
func (*RouteGroup) Group ¶
func (group *RouteGroup) Group(relativePath string, handlers ...HandlerFunc) *RouteGroup
Group creates a new server group. You should add all the routes that have common middlwares or the same path prefix. For example, all the routes that use a common middlware for authorization could be grouped.
func (*RouteGroup) HEAD ¶
func (group *RouteGroup) HEAD(relativePath string, handlers ...HandlerFunc) RouteHandler
Head is a shortcut for server.Handle("HEAD", path, handle)
func (*RouteGroup) Handle ¶
func (group *RouteGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) RouteHandler
Handle registers a new request handle and middleware with the given path and method. The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes. See the example code in github.
For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.
This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).
func (*RouteGroup) HandleRaw ¶
func (group *RouteGroup) HandleRaw(httpMethod, relativePath string, raw http.Handler) RouteHandler
HandleRaw registers a new request handler using a normal http.Handler
func (*RouteGroup) OPTIONS ¶
func (group *RouteGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) RouteHandler
Options is a shortcut for server.Handle("OPTIONS", path, handle)
func (*RouteGroup) PATCH ¶
func (group *RouteGroup) PATCH(relativePath string, handlers ...HandlerFunc) RouteHandler
Patch is a shortcut for server.Handle("PATCH", path, handle)
func (*RouteGroup) POST ¶
func (group *RouteGroup) POST(relativePath string, handlers ...HandlerFunc) RouteHandler
Post is a shortcut for server.Handle("POST", path, handle)
func (*RouteGroup) PUT ¶
func (group *RouteGroup) PUT(relativePath string, handlers ...HandlerFunc) RouteHandler
Put is a shortcut for server.Handle("PUT", path, handle)
func (*RouteGroup) Use ¶
func (group *RouteGroup) Use(middleware ...HandlerFunc) RouteHandler
Use adds middleware to the group, see example code in github.
type RouteHandler ¶
type RouteHandler interface { Use(...HandlerFunc) RouteHandler Handle(string, string, ...HandlerFunc) RouteHandler HandleRaw(string, string, http.Handler) RouteHandler Any(string, ...HandlerFunc) RouteHandler AnyRaw(string, http.Handler) RouteHandler GET(string, ...HandlerFunc) RouteHandler POST(string, ...HandlerFunc) RouteHandler DELETE(string, ...HandlerFunc) RouteHandler PATCH(string, ...HandlerFunc) RouteHandler PUT(string, ...HandlerFunc) RouteHandler OPTIONS(string, ...HandlerFunc) RouteHandler HEAD(string, ...HandlerFunc) RouteHandler File(string, string) RouteHandler Directory(string, string) RouteHandler }
type Server ¶
type Server struct { RouteGroup DebugEnabled bool // contains filtered or unexported fields }
Server supports configure middleware and routing for a handler Create an instance of Server, by using New()
func New ¶
func New() *Server
New returns a new blank Server instance without any middleware attached
func (*Server) AllowedMethods ¶
AllowedMethods returns a slice of HTTP methods that are allowed for a path
func (*Server) DescribeOptions ¶
func (s *Server) DescribeOptions(handlers ...HandlerFunc)
DescribeOptions registers a global handler chain for OPTIONS requests, with the "Allow" header set automatically to list allowed HTTP methods for the route.
func (*Server) IsAvailable ¶
IsAvailable returns whether the server is available or not. If the server is not available, the unavailable handler will be called instead of using the normal routing rules.
func (*Server) NoMethod ¶
func (s *Server) NoMethod(handlers ...HandlerFunc)
NoMethod registers a handler chain for requests with a method that isn't allowed for a path
func (*Server) NotFound ¶
func (s *Server) NotFound(handlers ...HandlerFunc)
NotFound registers a handler chain for requests with a path that does not exist
func (*Server) Routes ¶
Routes returns a slice of registered routes, including some useful information, such as: the http method, path and the handler name.
func (*Server) Run ¶
Run attaches the server to a http.Server and starts listening and serving HTTP requests. It is a shortcut for http.ListenAndServe(addr, server)
func (*Server) RunTLS ¶
RunTLS attaches the server to a http.Server and starts listening and serving HTTPS (secure) requests. It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, server)
func (*Server) RunUnix ¶
RunUnix attaches the server to a http.Server and starts listening and serving HTTP requests through the specified unix socket (ie. a file).
func (*Server) ServeHTTP ¶
func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request)
Conforms to the http.Handler interface.
func (*Server) SetAvailable ¶
SetAvailable toggles whether the server is available or not. If the server is not available, the unavailable handler will be called instead of using the normal routing rules.
func (*Server) Unavailable ¶
func (s *Server) Unavailable(handlers ...HandlerFunc)
Unavailable registers a handler chain for requests received while the server is marked unavailable