Documentation ¶
Index ¶
- Constants
- Variables
- type Context
- func (c *Context) Fail(status int, msg interface{})
- func (c *Context) Finalize() int
- func (c *Context) Header(key, value string)
- func (c *Context) Logger() *log.Logger
- func (c *Context) MustStatus(status int)
- func (c *Context) Params() httprouter.Params
- func (c *Context) Request() *http.Request
- func (c *Context) SetBody(v interface{})
- func (c *Context) Status(status int)
- func (c *Context) StopChain()
- type CustomProvideFunction
- type Engine
- func (e *Engine) CustomProvide(value interface{}, fn CustomProvideFunction)
- func (e *Engine) CustomProvideWithKey(key string, value interface{}, fn CustomProvideFunction)
- func (e *Engine) GetHandler() http.Handler
- func (e *Engine) GetRouter() *Router
- func (e *Engine) GetWSRouter() *WSRouter
- func (e *Engine) ListenAndServe(addr string) error
- func (e *Engine) ListenAndServeTLS(addr, certFile, keyFile string) error
- func (e *Engine) Provide(value interface{})
- func (e *Engine) ProvideUnsafe(key string, value interface{})
- func (e *Engine) ProvideWithKey(key string, value interface{})
- func (e *Engine) ServeFile(path, file string)
- func (e *Engine) ServeFiles(path string, root http.FileSystem)
- func (e *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (e *Engine) SetErrorHandler(fn ErrorHandler)
- func (e *Engine) SetRouteCallback(fn RouteCallback)
- type ErrorHandler
- type HandlerStat
- type InjectionError
- type ParseError
- type RequestHandler
- type RouteCallback
- type RouteStat
- type Router
- func (r *Router) CONNECT(path string, handlers ...RequestHandler)
- func (r *Router) DELETE(path string, handlers ...RequestHandler)
- func (r *Router) GET(path string, handlers ...RequestHandler)
- func (r *Router) Group(_path string, handlers ...RequestHandler) *Router
- func (r *Router) HEAD(path string, handlers ...RequestHandler)
- func (r *Router) Method(method, path string, handlers ...RequestHandler)
- func (r *Router) OPTIONS(path string, handlers ...RequestHandler)
- func (r *Router) PATCH(path string, handlers ...RequestHandler)
- func (r *Router) POST(path string, handlers ...RequestHandler)
- func (r *Router) PUT(path string, handlers ...RequestHandler)
- func (r *Router) TRACE(path string, handlers ...RequestHandler)
- type UploadedFile
- type ValidationError
- type WSRouter
- type WebsocketHandler
Constants ¶
const ( // PlaceParameter is used in ValidationError to indicate the error is in // URL Parameters PlaceParameter = "URL Path Parameter" // PlaceQuery is used in ValidationError to indicate the error is in // Query parameters PlaceQuery = "Query Parameter" // PlaceBody is used in ValidationError to indicate the error is in // Body of the request PlaceBody = "Body" // PlaceForm is used in ValidationError to indicate the error is in // submitted form PlaceForm = "Form Value" )
const ( // FieldParameter defines the struct field name for looking up URL Parameters FieldParameter = "Param" // FieldBody defines the struct field name for looking up the body of request FieldBody = "Body" // FieldForm defines the struct field name for looking up form of request FieldForm = "Form" // FieldQuery defines the struct field name for looking up QUery Parameters FieldQuery = "Query" )
const ( // TagInject The field name that is used to lookup injections in the handlers TagInject = "inject" // TagQuery is the field tag to define a query parameter's key TagQuery = "q" )
Variables ¶
var ErrNoSuchDependency = errors.New("No such dependency exists")
ErrNoSuchDependency is thrown whenever the requested interface could not be found in the injector
var NoOpRouteCallback = func(stat RouteStat) {}
NoOpRouteCallback is doing nothing for a RouteCallback which should increases the performance which can be desirable when too many requests arrive
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context is an object that is alive during an HTTP Request. It holds useful information about a request and allows the gongular to hold the information, then serialize it to the client whenever all handlers are finished.
func (*Context) MustStatus ¶
MustStatus overrides the status
func (*Context) Params ¶
func (c *Context) Params() httprouter.Params
Params returns the URL parameters of the request
func (*Context) Request ¶
Request returns the request object so that it can be used in middlewares or handlers.
func (*Context) SetBody ¶
func (c *Context) SetBody(v interface{})
SetBody sets the given interface which will be written
type CustomProvideFunction ¶
CustomProvideFunction is called whenever a value is needed to be provided with custom logic
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is the main gongular router wrapper.
func NewEngine ¶
func NewEngine() *Engine
NewEngine creates a new engine with the proper fields initialized
func (*Engine) CustomProvide ¶
func (e *Engine) CustomProvide(value interface{}, fn CustomProvideFunction)
CustomProvide provides with "default" key by calling the supplied CustomProvideFunction each time
func (*Engine) CustomProvideWithKey ¶
func (e *Engine) CustomProvideWithKey(key string, value interface{}, fn CustomProvideFunction)
CustomProvide provides with "default" key by calling the supplied CustomProvideFunction each time
func (*Engine) GetHandler ¶
GetHandler returns the underlying router as a http.Handler so that others can embed it if needed, which is useful for tests in our case.
func (*Engine) GetWSRouter ¶
GetWSRouter return the underlying Websocket router
func (*Engine) ListenAndServe ¶
ListenAndServe serves the given engine with a specific address. Mainly used for quick testing.
func (*Engine) ListenAndServeTLS ¶
ListenAndServeTLS serves the given engine with a specific address on HTTPs.
func (*Engine) Provide ¶
func (e *Engine) Provide(value interface{})
Provide provides with "default" key
func (*Engine) ProvideUnsafe ¶
ProvideUnsafe provides a key with an exact value
func (*Engine) ProvideWithKey ¶
ProvideWithKey provides an interface with a key
func (*Engine) ServeFiles ¶
func (e *Engine) ServeFiles(path string, root http.FileSystem)
ServeFiles serves the static files
func (*Engine) ServeHTTP ¶
func (e *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP serves from http
func (*Engine) SetErrorHandler ¶
func (e *Engine) SetErrorHandler(fn ErrorHandler)
SetErrorHandler sets the error handler
func (*Engine) SetRouteCallback ¶
func (e *Engine) SetRouteCallback(fn RouteCallback)
SetRouteCallback sets the callback function that is called when the route ends, which contains stats about the executed functions in that request
type ErrorHandler ¶
ErrorHandler is generic interface for error handling
type HandlerStat ¶
HandlerStat keeps duration, error (if exists) and whether the chain was stopped for
a single handler
type InjectionError ¶
InjectionError occurs whenever the listed dependency cannot be injected
func (InjectionError) Error ¶
func (i InjectionError) Error() string
type ParseError ¶
ParseError occurs whenever the field cannot be parsed, i.e. type mismatch
func (ParseError) Error ¶
func (p ParseError) Error() string
type RequestHandler ¶
RequestHandler is a generic handler for gongular2
type RouteCallback ¶
type RouteCallback func(stat RouteStat)
RouteCallback is the interface to what to do with a given route
var DefaultRouteCallback RouteCallback = func(stat RouteStat) { s := fmt.Sprintln(stat.Request.Method, stat.Request.RemoteAddr, stat.MatchedPath, stat.Request.RequestURI, stat.TotalDuration, stat.ResponseSize, stat.ResponseCode) for idx, h := range stat.Handlers { s += fmt.Sprintln("\t", idx, " ", h.FuncName, " ", h.Duration) } fmt.Println(s) }
DefaultRouteCallback prints many information about the the request including the individual handler(s) information as well
type RouteStat ¶
type RouteStat struct { Request *http.Request Handlers []HandlerStat MatchedPath string TotalDuration time.Duration ResponseSize int ResponseCode int Logs *bytes.Buffer }
RouteStat holds information for the whole route, which path it matched, the written response size and the final status code for the request, and finally the logs generated by all handlers and it includes the individual HandlerStat this route consists of.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router holds the required states and does the mapping of requests
func (*Router) CONNECT ¶
func (r *Router) CONNECT(path string, handlers ...RequestHandler)
CONNECT registers the given handlers at the path for a CONNECT request
func (*Router) DELETE ¶
func (r *Router) DELETE(path string, handlers ...RequestHandler)
DELETE registers the given handlers at the path for a DELETE request
func (*Router) GET ¶
func (r *Router) GET(path string, handlers ...RequestHandler)
GET registers the given handlers at the path for a GET request
func (*Router) Group ¶
func (r *Router) Group(_path string, handlers ...RequestHandler) *Router
Group groups a given path with additional interfaces. It is useful to avoid repetitions while defining many paths
func (*Router) HEAD ¶
func (r *Router) HEAD(path string, handlers ...RequestHandler)
HEAD registers the given handlers at the path for a HEAD request
func (*Router) Method ¶
func (r *Router) Method(method, path string, handlers ...RequestHandler)
Method registers the given handlers at the path for the given method request
func (*Router) OPTIONS ¶
func (r *Router) OPTIONS(path string, handlers ...RequestHandler)
OPTIONS registers the given handlers at the path for a OPTIONS request
func (*Router) PATCH ¶
func (r *Router) PATCH(path string, handlers ...RequestHandler)
PATCH registers the given handlers at the path for a PATCH request
func (*Router) POST ¶
func (r *Router) POST(path string, handlers ...RequestHandler)
POST registers the given handlers at the path for a POST request
func (*Router) PUT ¶
func (r *Router) PUT(path string, handlers ...RequestHandler)
PUT registers the given handlers at the path for a PUT request
func (*Router) TRACE ¶
func (r *Router) TRACE(path string, handlers ...RequestHandler)
TRACE registers the given handlers at the path for a TRACE request
type UploadedFile ¶
type UploadedFile struct { File multipart.File Header *multipart.FileHeader }
UploadedFile packs the file and the header of the file to one struct
type ValidationError ¶
ValidationError occurs whenever one or more fields fail the validation by govalidator
func (ValidationError) Error ¶
func (v ValidationError) Error() string
type WSRouter ¶
type WSRouter struct {
// contains filtered or unexported fields
}
WSRouter wraps the Engine with the ability to map WebsocketHandler to routes. Currently it does not support sub-routing but it supports injections, param and query parameter bindings.
func (*WSRouter) Handle ¶
func (r *WSRouter) Handle(path string, handler WebsocketHandler)
Handle registers the given Websocket handler if
type WebsocketHandler ¶
type WebsocketHandler interface { // Before is a filter applied just before upgrading the request to websocket. It can be useful for filtering the // request and returning an error would not open a websocket but close it with an error. The http.Header is for // answering with a http.Header which allows setting a cookie. Can be omitted if not desired. Before(c *Context) (http.Header, error) // Handle is regular handling of the web socket, user is fully responsible for the request Handle(conn *websocket.Conn) }
WebsocketHandler handles the Websocket interactions. It has two functions Before, Handle which must be implemented by the target object