Documentation
¶
Index ¶
- Variables
- func CleanPath(p string) string
- type ActionHandler
- type HandlerFunc
- type Injector
- type Map
- type MiddlewareFunc
- type MiddlewareHandlerFunc
- type MiddlewareStack
- type Module
- type ModuleFactory
- type ModuleOptioner
- type ModuleStarter
- type ModuleStopper
- type Options
- type Param
- type Params
- type Provider
- type Response
- func ResponseData(code int, data []byte, contentType []string) Response
- func ResponseDownload(name string, reader io.Reader) Response
- func ResponseError(code int, err error) Response
- func ResponseFile(filepath string) Response
- func ResponseHeader(code int, headers map[string]string) Response
- func ResponseJSON(code int, data interface{}) Response
- func ResponseReader(code int, reader io.Reader, contentType []string) Response
- func ResponseRedirect(code int, url string) Response
- func ResponseText(code int, text string) Response
- func ResponseXML(code int, data interface{}) Response
- type Route
- type Router
- func (r *Router) Attach(prefix string, router *Router)
- func (r *Router) AttachRoutes(prefix string, routes Routes)
- func (r *Router) DELETE(path string, handler HandlerFunc, middlewares ...MiddlewareHandlerFunc)
- func (r *Router) GET(path string, handler HandlerFunc, middlewares ...MiddlewareHandlerFunc)
- func (r *Router) Group(path string, middlewares ...MiddlewareHandlerFunc) *Router
- func (r *Router) HEAD(path string, handler HandlerFunc, middlewares ...MiddlewareHandlerFunc)
- func (r *Router) Handle(method, path string, handler HandlerFunc, middlewares ...MiddlewareHandlerFunc)
- func (r *Router) Lookup(method, path string) (*Route, Params, bool)
- func (r *Router) OPTIONS(path string, handler HandlerFunc, middlewares ...MiddlewareHandlerFunc)
- func (r *Router) PATCH(path string, handler HandlerFunc, middlewares ...MiddlewareHandlerFunc)
- func (r *Router) POST(path string, handler HandlerFunc, middlewares ...MiddlewareHandlerFunc)
- func (r *Router) PUT(path string, handler HandlerFunc, middlewares ...MiddlewareHandlerFunc)
- func (r *Router) Routes() (routes Routes)
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) Use(mw ...MiddlewareHandlerFunc)
- type RouterFactory
- type RouterOptions
- type Routes
Constants ¶
This section is empty.
Variables ¶
var ParamsKey = paramsKey{}
ParamsKey is the request context key under which URL params are stored.
Functions ¶
func CleanPath ¶
CleanPath is the URL version of path.Clean, it returns a canonical URL path for p, eliminating . and .. elements.
The following rules are applied iteratively until no further processing can be done:
- Replace multiple slashes with a single slash.
- Eliminate each . path name element (the current directory).
- Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
- Eliminate .. elements that begin a rooted path: that is, replace "/.." by "/" at the beginning of a path.
If the result of this process is an empty string, "/" is returned
Types ¶
type ActionHandler ¶
type ActionHandler interface { Method() string Path() string Middlewares() []MiddlewareHandlerFunc Handle(r *http.Request) Response }
ActionHandler interface is used to define http action handlers defined by module router
type HandlerFunc ¶
HandlerFunc is a function that is registered to a route to handle http requests
type Injector ¶
type Injector interface {
Provide(constructor interface{}) (interface{}, error)
}
Injector defines Dependency Injector interface
type MiddlewareFunc ¶
type MiddlewareFunc func(w http.ResponseWriter, r *http.Request) Response
MiddlewareFunc defines middleware handler function
type MiddlewareHandlerFunc ¶
type MiddlewareHandlerFunc func(MiddlewareFunc) MiddlewareFunc
MiddlewareHandlerFunc defines middleware interface
func DoSomething(next MiddlewareFunc) MiddlewareFunc { return func(w http.ResponseWriter, r *http.Request) Response { // do something before calling the next handler resp := next(w, r) // do something after call the handler return resp } }
type MiddlewareStack ¶
type MiddlewareStack struct {
// contains filtered or unexported fields
}
MiddlewareStack holds middlewares applied to router
func (*MiddlewareStack) Append ¶
func (mws *MiddlewareStack) Append(mw ...MiddlewareHandlerFunc)
Append new Middlewares to stack
func (*MiddlewareStack) Clone ¶
func (mws *MiddlewareStack) Clone(mw ...MiddlewareHandlerFunc) *MiddlewareStack
Clone current stack to new one abd apply new middlewares
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module struct
func Bootstrap ¶
func Bootstrap(moduleFactory ModuleFactory) (*Module, error)
Bootstrap creates Flow Module instance for given factory object
type ModuleFactory ¶
type ModuleFactory interface { // ProvideImports returns list of instance providers for module dependecies // This method is used to register all module dependecies // eg. logging, db connection,.... // all dependecies that are provided in this method // will be available to all modules imported by the factory ProvideImports() []Provider // ProvideExports returns list of instance providers for // functionalities that module will export. // Exported functionalities will be available to other modules that // import module created by the Factory ProvideExports() []Provider // ProvideModules returns list of instance providers // for modules that current module depends on ProvideModules() []Provider // ProvideRouters returns list of instance providers for module routers. // Module routers are used for http routing ProvideRouters() []Provider }
ModuleFactory interface for creating flow.Module
type ModuleOptioner ¶
type ModuleOptioner interface {
Options() Options
}
ModuleOptioner interface is used for providing Application Options This interface is used only for root module or AppModule
type ModuleStarter ¶
type ModuleStarter interface {
Start() error
}
ModuleStarter interface used when http application is served Start method is invoked if module implements the interface
type ModuleStopper ¶
type ModuleStopper interface {
Stop()
}
ModuleStopper interface used when http application is stopped Stop method is invoked during shutdown process if module implements the interface
type Options ¶
type Options struct { RouterOptions Env string Name string Addr string Version string ModuleSuffix string ControllerSuffix string ControllerIndex string // contains filtered or unexported fields }
Options holds application configuration Options
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.
func ParamsFromContext ¶
ParamsFromContext pulls the URL parameters from a request context, or returns nil if none are present.
type Provider ¶
func NewProvider ¶
func NewProvider(constructor interface{}) Provider
type Response ¶
Response defines interface for HTTP action responses
func ResponseData ¶
ResponseData creates []byte render Response
func ResponseDownload ¶
ResponseDownload creates file attachment ActionResult with following headers:
Content-Type Content-Length Content-Disposition
Content-Type is set using mime#TypeByExtension with the filename's extension. Content-Type will default to application/octet-stream if using a filename with an unknown extension.
func ResponseError ¶
ResponseError creates new Error response for given http code and error
func ResponseFile ¶
ResponseFile serves content from given file
func ResponseJSON ¶
ResponseJSON creates JSON rendered Response
func ResponseReader ¶
ResponseReader creates io.Reader render Response for given http code, reader and content type
func ResponseRedirect ¶
ResponseRedirect creates Redirect ewsponse for given http code and destination URL
func ResponseText ¶
ResponseText creates Text rendered Response
func ResponseXML ¶
ResponseXML creates XML rendered Response
type Route ¶
type Route struct { Method string Path string Mws *MiddlewareStack Handler HandlerFunc // contains filtered or unexported fields }
Route structure
func (*Route) HandleRequest ¶
HandleRequest handles http request. It executes all route middlewares and action handler
type Router ¶
type Router struct { // Enables automatic redirection if the current route can't be matched but a // handler for the path with (without) the trailing slash exists. // For example if /foo/ is requested but a route only exists for /foo, the // client is redirected to /foo with http status code 301 for GET requests // and 308 for all other request methods. RedirectTrailingSlash bool // If enabled, the router tries to fix the current request path, if no // handle is registered for it. // First superfluous path elements like ../ or // are removed. // Afterwards the router does a case-insensitive lookup of the cleaned path. // If a handle can be found for this route, the router makes a redirection // to the corrected path with status code 301 for GET requests and 308 for // all other request methods. // For example /FOO and /..//Foo could be redirected to /foo. // RedirectTrailingSlash is independent of this option. RedirectFixedPath bool // If enabled, the router checks if another method is allowed for the // current route, if the current request can not be routed. // If this is the case, the request is answered with 'Method Not Allowed' // and HTTP status code 405. // If no other Method is allowed, the request is delegated to the NotFound // handler. HandleMethodNotAllowed bool // If enabled, the router automatically replies to OPTIONS requests. // Custom OPTIONS handlers take priority over automatic replies. HandleOptions bool // Body404 string to be displayed when route is not found Body404 string // Body405 string to be displayed when route is not allowed Body405 string // contains filtered or unexported fields }
Router is a http.Handler which can be used to dispatch requests to different handler functions via configurable routes
func NewRouter ¶
func NewRouter() *Router
NewRouter creates new Router instance with default options
func NewRouterWithOptions ¶
func NewRouterWithOptions(opts RouterOptions) *Router
NewRouterWithOptions creates new Router instance for given options
func (*Router) AttachRoutes ¶
AttachRoutes to current routes
func (*Router) DELETE ¶
func (r *Router) DELETE(path string, handler HandlerFunc, middlewares ...MiddlewareHandlerFunc)
DELETE is a shortcut for router.Handle(http.MethodDelete, path, handler)
func (*Router) GET ¶
func (r *Router) GET(path string, handler HandlerFunc, middlewares ...MiddlewareHandlerFunc)
GET is a shortcut for router.Handle(http.MethodGet, path, handler)
func (*Router) Group ¶
func (r *Router) Group(path string, middlewares ...MiddlewareHandlerFunc) *Router
Group creates a new router group.
You should add all the routes that have common middlewares or the same path prefix. For example, all the routes that use a common middleware for authorization could be grouped.
func (*Router) HEAD ¶
func (r *Router) HEAD(path string, handler HandlerFunc, middlewares ...MiddlewareHandlerFunc)
HEAD is a shortcut for router.Handle(http.MethodHead, path, handler)
func (*Router) Handle ¶
func (r *Router) Handle(method, path string, handler HandlerFunc, middlewares ...MiddlewareHandlerFunc)
Handle registers a new request handle with the given path and method.
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 (*Router) Lookup ¶
Lookup allows the manual lookup of a method + path combo. This is e.g. useful to build a framework around this router. If the path was found, it returns the handle function and the path parameter values. Otherwise the third return value indicates whether a redirection to the same path with an extra / without the trailing slash should be performed.
func (*Router) OPTIONS ¶
func (r *Router) OPTIONS(path string, handler HandlerFunc, middlewares ...MiddlewareHandlerFunc)
OPTIONS is a shortcut for router.Handle(http.MethodOptions, path, handler)
func (*Router) PATCH ¶
func (r *Router) PATCH(path string, handler HandlerFunc, middlewares ...MiddlewareHandlerFunc)
PATCH is a shortcut for router.Handle(http.MethodPatch, path, handler)
func (*Router) POST ¶
func (r *Router) POST(path string, handler HandlerFunc, middlewares ...MiddlewareHandlerFunc)
POST is a shortcut for router.Handle(http.MethodPost, path, handler)
func (*Router) PUT ¶
func (r *Router) PUT(path string, handler HandlerFunc, middlewares ...MiddlewareHandlerFunc)
PUT is a shortcut for router.Handle(http.MethodPut, path, handler)
func (*Router) Use ¶
func (r *Router) Use(mw ...MiddlewareHandlerFunc)
Use appends one or more middlewares to middleware stack.
type RouterFactory ¶
type RouterFactory interface { Path() string Middlewares() []MiddlewareHandlerFunc ProvideHandlers() []Provider RegisterSubRouters() bool }
RouterFactory interface responsible for creating module routers