Documentation ¶
Index ¶
- Constants
- Variables
- type Context
- type ContextFunc
- type Ctx
- func (c *Ctx) AcceptedLanguages(lowercase bool) []string
- func (c *Ctx) Attachment(r io.Reader, filename string) (err error)
- func (c *Ctx) BaseContext() *Ctx
- func (c *Ctx) ClientIP() (clientIP string)
- func (c *Ctx) Get(key string) (value interface{}, exists bool)
- func (c *Ctx) HandlerName() string
- func (c *Ctx) Inline(r io.Reader, filename string) (err error)
- func (c *Ctx) Next()
- func (c *Ctx) Param(name string) string
- func (c *Ctx) ParseForm() error
- func (c *Ctx) ParseMultipartForm(maxMemory int64) error
- func (c *Ctx) Request() *http.Request
- func (c *Ctx) RequestComplete()
- func (c *Ctx) Reset(w http.ResponseWriter, r *http.Request)
- func (c *Ctx) Response() *Response
- func (c *Ctx) Set(key string, value interface{})
- func (c *Ctx) Stream(step func(w io.Writer) bool)
- func (c *Ctx) WebSocket() *websocket.Conn
- type CustomHandlerFunc
- type Handler
- type HandlerFunc
- type HandlersChain
- type IRouteGroup
- type IRoutes
- type LARS
- func (g *LARS) Any(path string, h ...Handler)
- func (g *LARS) Connect(path string, h ...Handler)
- func (g *LARS) Delete(path string, h ...Handler)
- func (g *LARS) Get(path string, h ...Handler)
- func (l *LARS) GetRouteMap() []*RouteMap
- func (g *LARS) Group(prefix string, middleware ...Handler) IRouteGroup
- func (g *LARS) Head(path string, h ...Handler)
- func (g *LARS) Match(methods []string, path string, h ...Handler)
- func (g *LARS) Options(path string, h ...Handler)
- func (g *LARS) Patch(path string, h ...Handler)
- func (g *LARS) Post(path string, h ...Handler)
- func (g *LARS) Put(path string, h ...Handler)
- func (l *LARS) Register404(notFound ...Handler)
- func (l *LARS) RegisterContext(fn ContextFunc)
- func (l *LARS) RegisterCustomHandler(customType interface{}, fn CustomHandlerFunc)
- func (l *LARS) Serve() http.Handler
- func (l *LARS) SetHandle405MethodNotAllowed(set bool)
- func (l *LARS) SetRedirectTrailingSlash(set bool)
- func (g *LARS) Trace(path string, h ...Handler)
- func (g *LARS) Use(m ...Handler)
- func (g *LARS) WebSocket(path string, h Handler)
- type Param
- type Params
- type Response
- func (r *Response) CloseNotify() <-chan bool
- func (r *Response) Committed() bool
- func (r *Response) Flush()
- func (r *Response) Header() http.Header
- func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (r *Response) SetWriter(w http.ResponseWriter)
- func (r *Response) Size() int64
- func (r *Response) Status() int
- func (r *Response) Write(b []byte) (n int, err error)
- func (r *Response) WriteHeader(code int)
- func (r *Response) WriteString(s string) (n int, err error)
- func (r *Response) Writer() http.ResponseWriter
- type RouteMap
- type Router
Constants ¶
const ( // CONNECT HTTP method CONNECT = "CONNECT" // DELETE HTTP method DELETE = "DELETE" // GET HTTP method GET = "GET" // HEAD HTTP method HEAD = "HEAD" // OPTIONS HTTP method OPTIONS = "OPTIONS" // PATCH HTTP method PATCH = "PATCH" // POST HTTP method POST = "POST" // PUT HTTP method PUT = "PUT" // TRACE HTTP method TRACE = "TRACE" ApplicationJSON = "application/json" ApplicationJSONCharsetUTF8 = ApplicationJSON + "; " + CharsetUTF8 ApplicationJavaScript = "application/javascript" ApplicationJavaScriptCharsetUTF8 = ApplicationJavaScript + "; " + CharsetUTF8 ApplicationXML = "application/xml" ApplicationXMLCharsetUTF8 = ApplicationXML + "; " + CharsetUTF8 ApplicationForm = "application/x-www-form-urlencoded" ApplicationProtobuf = "application/protobuf" ApplicationMsgpack = "application/msgpack" TextHTML = "text/html" TextHTMLCharsetUTF8 = TextHTML + "; " + CharsetUTF8 TextPlain = "text/plain" TextPlainCharsetUTF8 = TextPlain + "; " + CharsetUTF8 MultipartForm = "multipart/form-data" OctetStream = "application/octet-stream" CharsetUTF8 = "charset=utf-8" AcceptedLanguage = "Accept-Language" AcceptEncoding = "Accept-Encoding" Authorization = "Authorization" ContentDisposition = "Content-Disposition" ContentEncoding = "Content-Encoding" ContentLength = "Content-Length" ContentType = "Content-Type" Location = "Location" Upgrade = "Upgrade" Vary = "Vary" WWWAuthenticate = "WWW-Authenticate" XForwardedFor = "X-Forwarded-For" XRealIP = "X-Real-Ip" Gzip = "gzip" WildcardParam = "*wildcard" )
HTTP Constant Terms and Variables
Variables ¶
var NativeChainHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { c := GetContext(w) b := c.BaseContext() if b.index+1 < len(b.handlers) { c.Next() } })
NativeChainHandler is used in native handler chains example using nosurf crsf middleware nosurf.NewPure(lars.NativeChainHandlerFunc)
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context interface { context.Context Request() *http.Request Response() *Response WebSocket() *websocket.Conn Param(name string) string ParseForm() error ParseMultipartForm(maxMemory int64) error Set(key string, value interface{}) Get(key string) (value interface{}, exists bool) Next() Reset(w http.ResponseWriter, r *http.Request) RequestComplete() ClientIP() (clientIP string) AcceptedLanguages(lowercase bool) []string HandlerName() string Stream(step func(w io.Writer) bool) Attachment(r io.Reader, filename string) (err error) Inline(r io.Reader, filename string) (err error) BaseContext() *Ctx }
Context is the context interface type
func GetContext ¶
func GetContext(w http.ResponseWriter) Context
GetContext is a helper method for retrieving the Context object from the ResponseWriter when using native go hanlders. NOTE: this will panic if fed an http.ResponseWriter not provided by lars's chaining.
type ContextFunc ¶
ContextFunc is the function to run when creating a new context
type Ctx ¶
Ctx encapsulates the http request, response context
func NewContext ¶
NewContext returns a new default lars Context object.
func (*Ctx) AcceptedLanguages ¶
AcceptedLanguages returns an array of accepted languages denoted by the Accept-Language header sent by the browser NOTE: some stupid browsers send in locales lowercase when all the rest send it properly
func (*Ctx) Attachment ¶
Attachment is a helper method for returning an attachement file to be downloaded, if you with to open inline see function
func (*Ctx) BaseContext ¶
BaseContext returns the underlying context object LARS uses internally. used when overriding the context object
func (*Ctx) 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 (*Ctx) Get ¶
Get returns the value for the given key, ie: (value, true). If the value does not exists it returns (nil, false)
func (*Ctx) HandlerName ¶
HandlerName returns the current Contexts final handler's name
func (*Ctx) Inline ¶
Inline is a helper method for returning a file inline to be rendered/opened by the browser
func (*Ctx) Next ¶
func (c *Ctx) Next()
Next should be used only inside middleware. It executes the pending handlers in the chain inside the calling handler. See example in github.
func (*Ctx) Param ¶
Param returns the value of the first Param which key matches the given name. If no matching Param is found, an empty string is returned.
func (*Ctx) ParseForm ¶
ParseForm calls the underlying http.Request ParseForm but also adds the URL params to the request Form as if they were defined as query params i.e. ?id=13&ok=true but does not add the params to the http.Request.URL.RawQuery for SEO purposes
func (*Ctx) ParseMultipartForm ¶
ParseMultipartForm calls the underlying http.Request ParseMultipartForm but also adds the URL params to the request Form as if they were defined as query params i.e. ?id=13&ok=true but does not add the params to the http.Request.URL.RawQuery for SEO purposes
func (*Ctx) RequestComplete ¶
func (c *Ctx) RequestComplete()
RequestComplete fires after request completes and just before the *Ctx object gets put back into the pool. Used to close DB connections and such on a custom context
func (*Ctx) Reset ¶
func (c *Ctx) Reset(w http.ResponseWriter, r *http.Request)
Reset resets the Context to it's default request state
func (*Ctx) Set ¶
Set is used to store a new key/value pair exclusivelly for thisContext. It also lazy initializes c.Keys if it was not used previously.
type CustomHandlerFunc ¶
CustomHandlerFunc wraped by HandlerFunc and called where you can type cast both Context and Handler and call Handler
type Handler ¶
type Handler interface{}
Handler is the type used in registering handlers. NOTE: these handlers may get wrapped by the HandlerFunc type internally.
type HandlerFunc ¶
type HandlerFunc func(Context)
HandlerFunc is the internal handler type used for middleware and handlers
type HandlersChain ¶
type HandlersChain []HandlerFunc
HandlersChain is an array of HanderFunc handlers to run
type IRouteGroup ¶
type IRouteGroup interface { IRoutes Group(prefix string, middleware ...Handler) IRouteGroup }
IRouteGroup interface for router group
type IRoutes ¶
type IRoutes interface { Use(...Handler) Any(string, ...Handler) Get(string, ...Handler) Post(string, ...Handler) Delete(string, ...Handler) Patch(string, ...Handler) Put(string, ...Handler) Options(string, ...Handler) Head(string, ...Handler) Connect(string, ...Handler) Trace(string, ...Handler) WebSocket(string, Handler) }
IRoutes interface for routes
type LARS ¶
type LARS struct {
// contains filtered or unexported fields
}
LARS is the main routing instance
func (*LARS) GetRouteMap ¶
GetRouteMap returns an array of all registered routes
func (*LARS) Group ¶
func (g *LARS) Group(prefix string, middleware ...Handler) IRouteGroup
Group creates a new sub router with prefix. It inherits all properties from the parent. Passing middleware overrides parent middleware but still keeps the root level middleware intact.
func (*LARS) Register404 ¶
Register404 alows for overriding of the not found handler function. NOTE: this is run after not finding a route even after redirecting with the trailing slash
func (*LARS) RegisterContext ¶
func (l *LARS) RegisterContext(fn ContextFunc)
RegisterContext registers a custom Context function for creation and resetting of a global object passed per http request
func (*LARS) RegisterCustomHandler ¶
func (l *LARS) RegisterCustomHandler(customType interface{}, fn CustomHandlerFunc)
RegisterCustomHandler registers a custom handler that gets wrapped by HandlerFunc
func (*LARS) SetHandle405MethodNotAllowed ¶
SetHandle405MethodNotAllowed tells lars whether to handle the http 405 Method Not Allowed status code
func (*LARS) SetRedirectTrailingSlash ¶
SetRedirectTrailingSlash tells lars whether to try and fix a URL by trying to find it lowercase -> with or without slash -> 404
type Params ¶
type Params []Param
Params is a Param-slice, as returned by the router. 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 }
Response struct contains methods and to capture extra data about the http request and more efficiently reset underlying writer object... it does comply with the http.ResponseWriter interface
func (*Response) CloseNotify ¶
CloseNotify wraps response writer's CloseNotify function.
func (*Response) Committed ¶
Committed returns whether the *Response header has already been written to and if has been committed to this return.
func (*Response) Header ¶
Header returns the header map that will be sent by WriteHeader. Changing the header after a call to WriteHeader (or Write) has no effect unless the modified headers were declared as trailers by setting the "Trailer" header before the call to WriteHeader (see example). To suppress implicit *Response headers, set their value to nil.
func (*Response) SetWriter ¶
func (r *Response) SetWriter(w http.ResponseWriter)
SetWriter sets the provided writer as the new *Response http.ResponseWriter
func (*Response) Write ¶
Write writes the data to the connection as part of an HTTP reply. If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK) before writing the data. If the Header does not contain a Content-Type line, Write adds a Content-Type set to the result of passing the initial 512 bytes of written data to DetectContentType.
func (*Response) WriteHeader ¶
WriteHeader sends an HTTP *Response header with status code. If WriteHeader is not called explicitly, the first call to Write will trigger an implicit WriteHeader(http.StatusOK). Thus explicit calls to WriteHeader are mainly used to send error codes.
func (*Response) WriteString ¶
WriteString write string to ResponseWriter
func (*Response) Writer ¶
func (r *Response) Writer() http.ResponseWriter
Writer return the *Response's http.ResponseWriter object. Usually only used when creating middleware.