Documentation ¶
Overview ¶
Package router provides routing implementation for aah framework. Routes config file format is similar to HOCON syntax aka typesafe config it gets parsed by `aahframework.org/config`.
aah router internally uses customized version of radix tree implementation from `github.com/julienschmidt/httprouter` developer by `@julienschmidt`.
Index ¶
- Constants
- Variables
- func IsDefaultAction(action string) bool
- type CORS
- func (c *CORS) AddAllowHeaders(hdrs []string) *CORS
- func (c *CORS) AddAllowMethods(methods []string) *CORS
- func (c *CORS) AddExposeHeaders(hdrs []string) *CORS
- func (c *CORS) AddOrigins(origins []string) *CORS
- func (c *CORS) IsHeadersAllowed(hdrs string) bool
- func (c *CORS) IsMethodAllowed(method string) bool
- func (c *CORS) IsOriginAllowed(origin string) bool
- func (c *CORS) SetAllowCredentials(b bool) *CORS
- func (c *CORS) SetMaxAge(age string) *CORS
- func (c CORS) String() string
- type Domain
- func (d *Domain) AddRoute(route *Route) error
- func (d *Domain) Allowed(requestMethod, path string) (allowed string)
- func (d *Domain) Lookup(req *http.Request) (*Route, ahttp.URLParams, bool)
- func (d *Domain) LookupByName(name string) *Route
- func (d *Domain) RouteURL(routeName string, args ...interface{}) string
- func (d *Domain) RouteURLNamedArgs(routeName string, args map[string]interface{}) string
- type Route
- type Router
- func (r *Router) CreateRouteURL(host, routeName string, margs map[string]interface{}, args ...interface{}) string
- func (r *Router) DomainAddresses() []string
- func (r *Router) Load() (err error)
- func (r *Router) Lookup(host string) *Domain
- func (r *Router) RegisteredActions() map[string]map[string]uint8
- func (r *Router) RegisteredWSActions() map[string]map[string]uint8
- func (r *Router) RootDomain() *Domain
Constants ¶
const (
// SlashString const for comparison use
SlashString = "/"
)
Variables ¶
var ( ErrCORSOriginIsInvalid = errors.New("cors: invalid origin") ErrCORSMethodNotAllowed = errors.New("cors: method not allowed") ErrCORSHeaderNotAllowed = errors.New("cors: header not allowed") ErrCORSContentTypeNotAllowed = errors.New("cors: content-type not allowed") )
CORS errors
var ( // HTTPMethodActionMap is default Controller Action name for corresponding // HTTP Method. If it's not provided in the route configuration. HTTPMethodActionMap = map[string]string{ ahttp.MethodGet: "Index", ahttp.MethodPost: "Create", ahttp.MethodPut: "Update", ahttp.MethodPatch: "Update", ahttp.MethodDelete: "Delete", ahttp.MethodOptions: "Options", ahttp.MethodHead: "Head", ahttp.MethodTrace: "Trace", } // ErrNoDomainRoutesConfigFound returned when routes config file not found or doesn't // have `domains { ... }` config information. ErrNoDomainRoutesConfigFound = errors.New("router: no domain routes config found") // ErrRouteConstraintFailed returned when request route constraints failed. ErrRouteConstraintFailed = errors.New("router: route constraints failed") )
Functions ¶
func IsDefaultAction ¶
IsDefaultAction method is to identify given action name is defined by aah framework in absence of user configured route action name.
Types ¶
type CORS ¶
type CORS struct { AllowCredentials bool MaxAge string AllowOrigins []string AllowMethods []string AllowHeaders []string ExposeHeaders []string // contains filtered or unexported fields }
CORS struct holds Cross-Origin Resource Sharing (CORS) configuration values and verification methods for the route.
Spec: https://www.w3.org/TR/cors/ Friendly Read: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
func (*CORS) AddAllowHeaders ¶
AddAllowHeaders method adds the given HTTP header into allow headers list.
func (*CORS) AddAllowMethods ¶
AddAllowMethods method adds the given HTTP verb into allow methods list.
func (*CORS) AddExposeHeaders ¶
AddExposeHeaders method adds the given HTTP header into expose headers list.
func (*CORS) AddOrigins ¶
AddOrigins method adds the given origin into allow origin list.
func (*CORS) IsHeadersAllowed ¶
IsHeadersAllowed method returns true if preflight headers are allowed otherwise false.
func (*CORS) IsMethodAllowed ¶
IsMethodAllowed method returns true if preflight method is allowed otherwise false.
func (*CORS) IsOriginAllowed ¶
IsOriginAllowed method check given origin is allowed or not.
func (*CORS) SetAllowCredentials ¶
SetAllowCredentials method sets the given boolean into allow credentials.
type Domain ¶
type Domain struct { IsSubDomain bool MethodNotAllowed bool RedirectTrailingSlash bool AutoOptions bool AntiCSRFEnabled bool CORSEnabled bool Key string Name string Host string Port string DefaultAuth string CORS *CORS CatchAllRoute *Route // contains filtered or unexported fields }
Domain is used to hold domain related routes and it's route configuration
func (*Domain) Allowed ¶
Allowed method returns the value for header `Allow` otherwise empty string.
func (*Domain) Lookup ¶
Lookup method looks up route if found it returns route, path parameters, redirect trailing slash indicator for given `ahttp.Request` by domain and request URI otherwise returns nil and false.
func (*Domain) LookupByName ¶
LookupByName method returns the route for given route name otherwise nil.
func (*Domain) RouteURL ¶
RouteURL method composes route reverse URL for given route and arguments based on index order. If error occurs then method logs it and returns empty string.
func (*Domain) RouteURLNamedArgs ¶
RouteURLNamedArgs composes reverse URL by route name and key-value pair arguments. Additional key-value pairs composed as URL query string. If error occurs then method logs it and returns empty string.
type Route ¶
type Route struct { IsAntiCSRFCheck bool IsStatic bool ListDir bool MaxBodySize int64 Name string Path string Method string Target string Action string ParentName string Auth string Dir string File string CORS *CORS Constraints map[string]string // contains filtered or unexported fields }
Route holds the single route details.
func (*Route) HasAccess ¶
HasAccess method does authorization check based on configured values at route level. TODO: the appropriate place for this method would be `security` package.
type Router ¶
type Router struct { Domains []*Domain // contains filtered or unexported fields }
Router is used to register all application routes and finds the appropriate route information for incoming request path.
func NewWithApp ¶
NewWithApp method creates router instance with aah application instance.
func (*Router) CreateRouteURL ¶
func (r *Router) CreateRouteURL(host, routeName string, margs map[string]interface{}, args ...interface{}) string
CreateRouteURL ...
func (*Router) DomainAddresses ¶
DomainAddresses method returns domain addresses (host:port) from routes configuration.
func (*Router) Load ¶
Load method loads a configuration from given file e.g. `routes.conf` and applies env profile override values if available.
func (*Router) RegisteredActions ¶
RegisteredActions method returns all the controller name and it's actions configured in the "routes.conf".
func (*Router) RegisteredWSActions ¶
RegisteredWSActions method returns all the WebSocket name and it's actions configured in the "routes.conf".
func (*Router) RootDomain ¶
RootDomain method returns the root domain registered in the routes.conf. For e.g.: sample.com, admin.sample.com, *.sample.com. Root Domain is `sample.com`.