router

package
v11.1.1+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2019 License: BSD-3-Clause Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SubdomainWildcardIndicator where a registered path starts with '*.'.
	// if subdomain == "*." then its wildcard.
	//
	// used internally by router and api builder.
	SubdomainWildcardIndicator = "*."

	// SubdomainWildcardPrefix where a registered path starts with "*./",
	// then this route should accept any subdomain.
	SubdomainWildcardPrefix = SubdomainWildcardIndicator + "/"
	// SubdomainPrefix where './' exists in a registered path then it contains subdomain
	//
	// used on api builder.
	SubdomainPrefix = "./" // i.e subdomain./ -> Subdomain: subdomain. Path: /
)
View Source
const (
	// ParamStart the character in string representation where the underline router starts its dynamic named parameter.
	ParamStart = ":"
	// WildcardParamStart the character in string representation where the underline router starts its dynamic wildcard
	// path parameter.
	WildcardParamStart = "*"
)
View Source
const MethodNone = "NONE"

MethodNone is a Virtual method to store the "offline" routes.

Variables

View Source
var (
	// AllMethods contains the valid http methods:
	// "GET", "POST", "PUT", "DELETE", "CONNECT", "HEAD",
	// "PATCH", "OPTIONS", "TRACE".
	AllMethods = []string{
		"GET",
		"POST",
		"PUT",
		"DELETE",
		"CONNECT",
		"HEAD",
		"PATCH",
		"OPTIONS",
		"TRACE",
	}
)

Functions

func Abs

func Abs(path string) string

Abs calls filepath.Abs but ignores the error and returns the original value if any error occurred.

func DirectoryExists

func DirectoryExists(dir string) bool

DirectoryExists returns true if a directory(or file) exists, otherwise false

func NewWrapper

func NewWrapper(wrapperFunc func(w http.ResponseWriter, r *http.Request, routerNext http.HandlerFunc), wrapped http.HandlerFunc) http.Handler

NewWrapper returns a new http.Handler wrapped by the 'wrapperFunc' the "next" is the final "wrapped" input parameter.

Application is responsible to make it to work on more than one wrappers via composition or func clojure.

func Param

func Param(name string) string

Param receives a parameter name prefixed with the ParamStart symbol.

func StaticEmbeddedHandler

func StaticEmbeddedHandler(vdir string, assetFn func(name string) ([]byte, error), namesFn func() []string, assetsGziped bool) context.Handler

StaticEmbeddedHandler returns a Handler which can serve embedded files that are embedded using the go-bindata tool(assetsGziped = false) or the kataras/bindata tool (assetsGziped = true).

Examples: https://github.com/kataras/iris/tree/master/_examples/file-server

func StaticHandler

func StaticHandler(systemPath string, showList bool, gzip bool) context.Handler

StaticHandler returns a new Handler which is ready to serve all kind of static files.

Developers can wrap this handler using the `router.StripPrefix` for a fixed static path when the result handler is being, finally, registered to a route.

Usage: app := iris.New() ... fileserver := iris.StaticHandler("./static_files", false, false) h := router.StripPrefix("/static", fileserver) /* http://mydomain.com/static/css/style.css */ app.Get("/static", h) ...

func StripPrefix

func StripPrefix(prefix string, h context.Handler) context.Handler

StripPrefix returns a handler that serves HTTP requests by removing the given prefix from the request URL's Path and invoking the handler h. StripPrefix handles a request for a path that doesn't begin with prefix by replying with an HTTP 404 not found error.

Usage: fileserver := Party#StaticHandler("./static_files", false, false) h := router.StripPrefix("/static", fileserver) app.Get("/static/{f:path}", h) app.Head("/static/{f:path}", h)

func TypeByExtension

func TypeByExtension(ext string) (typ string)

TypeByExtension returns the MIME type associated with the file extension ext. The extension ext should begin with a leading dot, as in ".html". When ext has no associated type, typeByExtension returns "".

Extensions are looked up first case-sensitively, then case-insensitively.

The built-in table is small but on unix it is augmented by the local system's mime.types file(s) if available under one or more of these names:

/etc/mime.types
/etc/apache2/mime.types
/etc/apache/mime.types

On Windows, MIME types are extracted from the registry.

Text types have the charset parameter set to "utf-8" by default.

func TypeByFilename

func TypeByFilename(fullFilename string) string

TypeByFilename same as TypeByExtension but receives a filename path instead.

func WildcardParam

func WildcardParam(name string) string

WildcardParam receives a parameter name prefixed with the WildcardParamStart symbol.

Types

type APIBuilder

type APIBuilder struct {
	// contains filtered or unexported fields
}

APIBuilder the visible API for constructing the router and child routers.

func NewAPIBuilder

func NewAPIBuilder() *APIBuilder

NewAPIBuilder creates & returns a new builder which is responsible to build the API and the router handler.

func (*APIBuilder) AllowMethods

func (api *APIBuilder) AllowMethods(methods ...string) Party

AllowMethods will re-register the future routes that will be registered via `Handle`, `Get`, `Post`, ... to the given "methods" on that Party and its children "Parties", duplicates are not registered.

Call of `AllowMethod` will override any previous allow methods.

func (*APIBuilder) Any

func (api *APIBuilder) Any(relativePath string, handlers ...context.Handler) (routes []*Route)

Any registers a route for ALL of the http methods (Get,Post,Put,Head,Patch,Options,Connect,Delete).

func (*APIBuilder) Connect

func (api *APIBuilder) Connect(relativePath string, handlers ...context.Handler) *Route

Connect registers a route for the Connect http method.

Returns a *Route and an error which will be filled if route wasn't registered successfully.

func (*APIBuilder) Delete

func (api *APIBuilder) Delete(relativePath string, handlers ...context.Handler) *Route

Delete registers a route for the Delete http method.

Returns a *Route and an error which will be filled if route wasn't registered successfully.

func (*APIBuilder) Done

func (api *APIBuilder) Done(handlers ...context.Handler)

Done appends to the very end, Handler(s) to the current Party's routes and child routes.

Call order matters, it should be called right before the routes that they care about these handlers.

The difference from .Use is that this/or these Handler(s) are being always running last.

func (*APIBuilder) DoneGlobal

func (api *APIBuilder) DoneGlobal(handlers ...context.Handler)

DoneGlobal registers handlers that should run at the very end. It appends those handler(s) to all routes, including all parties, subdomains. It doesn't care about call order, it will append the handlers to all existing routes and the future routes that may being registered.

The difference from `.UseGlobal` is that this/or these Handler(s) are being always running last. Use of `ctx.Next()` at the previous handler is necessary. It's always a good practise to call it right before the `Application#Run` function.

func (*APIBuilder) Favicon

func (api *APIBuilder) Favicon(favPath string, requestPath ...string) *Route

Favicon serves static favicon accepts 2 parameters, second is optional favPath (string), declare the system directory path of the __.ico requestPath (string), it's the route's path, by default this is the "/favicon.ico" because some browsers tries to get this by default first, you can declare your own path if you have more than one favicon (desktop, mobile and so on)

this func will add a route for you which will static serve the /yuorpath/yourfile.ico to the /yourfile.ico (nothing special that you can't handle by yourself). Note that you have to call it on every favicon you have to serve automatically (desktop, mobile and so on).

Returns the GET *Route.

func (*APIBuilder) FireErrorCode

func (api *APIBuilder) FireErrorCode(ctx context.Context)

FireErrorCode executes an error http status code handler based on the context's status code.

If a handler is not already registered, then it creates & registers a new trivial handler on the-fly.

func (*APIBuilder) Get

func (api *APIBuilder) Get(relativePath string, handlers ...context.Handler) *Route

Get registers a route for the Get http method.

Returns a *Route and an error which will be filled if route wasn't registered successfully.

func (*APIBuilder) GetRelPath

func (api *APIBuilder) GetRelPath() string

GetRelPath returns the current party's relative path. i.e: if r := app.Party("/users"), then the `r.GetRelPath()` is the "/users". if r := app.Party("www.") or app.Subdomain("www") then the `r.GetRelPath()` is the "www.".

func (*APIBuilder) GetReport

func (api *APIBuilder) GetReport() error

GetReport returns an error may caused by party's methods.

func (*APIBuilder) GetReporter

func (api *APIBuilder) GetReporter() *errors.Reporter

GetReporter returns the reporter for adding errors

func (*APIBuilder) GetRoute

func (api *APIBuilder) GetRoute(routeName string) *Route

GetRoute returns the registered route based on its name, otherwise nil. One note: "routeName" should be case-sensitive.

func (*APIBuilder) GetRouteReadOnly

func (api *APIBuilder) GetRouteReadOnly(routeName string) context.RouteReadOnly

GetRouteReadOnly returns the registered "read-only" route based on its name, otherwise nil. One note: "routeName" should be case-sensitive. Used by the context to get the current route. It returns an interface instead to reduce wrong usage and to keep the decoupled design between the context and the routes. Look `GetRoutesReadOnly` to fetch a list of all registered routes.

Look `GetRoute` for more.

func (*APIBuilder) GetRoutes

func (api *APIBuilder) GetRoutes() []*Route

GetRoutes returns the routes information, some of them can be changed at runtime some others not.

Needs refresh of the router to Method or Path or Handlers changes to take place.

func (*APIBuilder) GetRoutesReadOnly

func (api *APIBuilder) GetRoutesReadOnly() []context.RouteReadOnly

GetRoutesReadOnly returns the registered routes with "read-only" access, you cannot and you should not change any of these routes' properties on request state, you can use the `GetRoutes()` for that instead.

It returns interface-based slice instead of the real ones in order to apply safe fetch between context(request-state) and the builded application.

Look `GetRouteReadOnly` too.

func (*APIBuilder) Handle

func (api *APIBuilder) Handle(method string, relativePath string, handlers ...context.Handler) *Route

Handle registers a route to the server's api. if empty method is passed then handler(s) are being registered to all methods, same as .Any.

Returns a *Route, app will throw any errors later on.

func (*APIBuilder) HandleMany

func (api *APIBuilder) HandleMany(methodOrMulti string, relativePathorMulti string, handlers ...context.Handler) (routes []*Route)

HandleMany works like `Handle` but can receive more than one paths separated by spaces and returns always a slice of *Route instead of a single instance of Route.

It's useful only if the same handler can handle more than one request paths, otherwise use `Party` which can handle many paths with different handlers and middlewares.

Usage:

app.HandleMany("GET", "/user /user/{id:uint64} /user/me", genericUserHandler)

At the other side, with `Handle` we've had to write:

app.Handle("GET", "/user", userHandler)
app.Handle("GET", "/user/{id:uint64}", userByIDHandler)
app.Handle("GET", "/user/me", userMeHandler)

This method is used behind the scenes at the `Controller` function in order to handle more than one paths for the same controller instance.

func (*APIBuilder) Head

func (api *APIBuilder) Head(relativePath string, handlers ...context.Handler) *Route

Head registers a route for the Head http method.

Returns a *Route and an error which will be filled if route wasn't registered successfully.

func (*APIBuilder) Layout

func (api *APIBuilder) Layout(tmplLayoutFile string) Party

Layout overrides the parent template layout with a more specific layout for this Party. It returns the current Party.

The "tmplLayoutFile" should be a relative path to the templates dir. Usage:

app := iris.New() app.RegisterView(iris.$VIEW_ENGINE("./views", ".$extension")) my := app.Party("/my").Layout("layouts/mylayout.html")

my.Get("/", func(ctx iris.Context) {
	ctx.View("page1.html")
})

Examples: https://github.com/kataras/iris/tree/master/_examples/view

func (*APIBuilder) Macros

func (api *APIBuilder) Macros() *macro.Macros

Macros returns the macro collection that is responsible to register custom macros with their own parameter types and their macro functions for all routes.

Learn more at: https://github.com/kataras/iris/tree/master/_examples/routing/dynamic-path

func (*APIBuilder) None

func (api *APIBuilder) None(relativePath string, handlers ...context.Handler) *Route

None registers an "offline" route see context.ExecRoute(routeName) and party.Routes().Online(handleResultRouteInfo, "GET") and Offline(handleResultRouteInfo)

Returns a *Route and an error which will be filled if route wasn't registered successfully.

func (*APIBuilder) OnAnyErrorCode

func (api *APIBuilder) OnAnyErrorCode(handlers ...context.Handler)

OnAnyErrorCode registers a handler which called when error status code written. Same as `OnErrorCode` but registers all http error codes based on the `context.StatusCodeNotSuccessful` which defaults to < 200 || >= 400 for an error code, any previous error code will be overridden, so call it first if you want to use any custom handler for a specific error status code.

Read more at: http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml

func (*APIBuilder) OnErrorCode

func (api *APIBuilder) OnErrorCode(statusCode int, handlers ...context.Handler)

OnErrorCode registers an error http status code based on the "statusCode" < 200 || >= 400 (came from `context.StatusCodeNotSuccessful`). The handler is being wrapepd by a generic handler which will try to reset the body if recorder was enabled and/or disable the gzip if gzip response recorder was active.

func (*APIBuilder) Options

func (api *APIBuilder) Options(relativePath string, handlers ...context.Handler) *Route

Options registers a route for the Options http method.

Returns a *Route and an error which will be filled if route wasn't registered successfully.

func (*APIBuilder) Party

func (api *APIBuilder) Party(relativePath string, handlers ...context.Handler) Party

Party groups routes which may have the same prefix and share same handlers, returns that new rich subrouter.

You can even declare a subdomain with relativePath as "mysub." or see `Subdomain`.

func (*APIBuilder) PartyFunc

func (api *APIBuilder) PartyFunc(relativePath string, partyBuilderFunc func(p Party)) Party

PartyFunc same as `Party`, groups routes that share a base path or/and same handlers. However this function accepts a function that receives this created Party instead. Returns the Party in order the caller to be able to use this created Party to continue the top-bottom routes "tree".

Note: `iris#Party` and `core/router#Party` describes the exactly same interface.

Usage:

app.PartyFunc("/users", func(u iris.Party){
	u.Use(authMiddleware, logMiddleware)
	u.Get("/", getAllUsers)
	u.Post("/", createOrUpdateUser)
	u.Delete("/", deleteUser)
})

Look `Party` for more.

func (*APIBuilder) Patch

func (api *APIBuilder) Patch(relativePath string, handlers ...context.Handler) *Route

Patch registers a route for the Patch http method.

Returns a *Route and an error which will be filled if route wasn't registered successfully.

func (*APIBuilder) Post

func (api *APIBuilder) Post(relativePath string, handlers ...context.Handler) *Route

Post registers a route for the Post http method.

Returns a *Route and an error which will be filled if route wasn't registered successfully.

func (*APIBuilder) Put

func (api *APIBuilder) Put(relativePath string, handlers ...context.Handler) *Route

Put registers a route for the Put http method.

Returns a *Route and an error which will be filled if route wasn't registered successfully.

func (*APIBuilder) Reset

func (api *APIBuilder) Reset() Party

Reset removes all the begin and done handlers that may derived from the parent party via `Use` & `Done`, and the execution rules. Note that the `Reset` will not reset the handlers that are registered via `UseGlobal` & `DoneGlobal`.

Returns this Party.

func (*APIBuilder) SetExecutionRules

func (api *APIBuilder) SetExecutionRules(executionRules ExecutionRules) Party

SetExecutionRules alters the execution flow of the route handlers outside of the handlers themselves.

For example, if for some reason the desired result is the (done or all) handlers to be executed no matter what even if no `ctx.Next()` is called in the previous handlers, including the begin(`Use`), the main(`Handle`) and the done(`Done`) handlers themselves, then:

Party#SetExecutionRules(iris.ExecutionRules {
  Begin: iris.ExecutionOptions{Force: true},
  Main:  iris.ExecutionOptions{Force: true},
  Done:  iris.ExecutionOptions{Force: true},
})

Note that if : true then the only remained way to "break" the handler chain is by `ctx.StopExecution()` now that `ctx.Next()` does not matter.

These rules are per-party, so if a `Party` creates a child one then the same rules will be applied to that as well. Reset of these rules (before `Party#Handle`) can be done with `Party#SetExecutionRules(iris.ExecutionRules{})`.

The most common scenario for its use can be found inside Iris MVC Applications; when we want the `Done` handlers of that specific mvc app's `Party` to be executed but we don't want to add `ctx.Next()` on the `OurController#EndRequest`.

Returns this Party.

Example: https://github.com/kataras/iris/tree/master/_examples/mvc/middleware/without-ctx-next

func (*APIBuilder) StaticContent

func (api *APIBuilder) StaticContent(reqPath string, cType string, content []byte) *Route

StaticContent registers a GET and HEAD method routes to the requestPath that are ready to serve raw static bytes, memory cached.

Returns the GET *Route.

func (*APIBuilder) StaticEmbedded

func (api *APIBuilder) StaticEmbedded(requestPath string, vdir string, assetFn func(name string) ([]byte, error), namesFn func() []string) *Route

StaticEmbedded used when files are distributed inside the app executable, using go-bindata mostly First parameter is the request path, the path which the files in the vdir will be served to, for example "/static" Second parameter is the (virtual) directory path, for example "./assets" (no trailing slash), Third parameter is the Asset function Forth parameter is the AssetNames function.

Returns the GET *Route.

Example: https://github.com/kataras/iris/tree/master/_examples/file-server/embedding-files-into-app

func (*APIBuilder) StaticEmbeddedGzip

func (api *APIBuilder) StaticEmbeddedGzip(requestPath string, vdir string, gzipAssetFn func(name string) ([]byte, error), gzipNamesFn func() []string) *Route

StaticEmbeddedGzip registers a route which can serve embedded gziped files that are embedded using the https://github.com/kataras/bindata tool and only. It's 8 times faster than the `StaticEmbeddedHandler` with `go-bindata` but it sends gzip response only, so the client must be aware that is expecting a gzip body (browsers and most modern browsers do that, so you can use it without fair).

First parameter is the request path, the path which the files in the vdir will be served to, for example "/static" Second parameter is the (virtual) directory path, for example "./assets" (no trailing slash), Third parameter is the GzipAsset function Forth parameter is the GzipAssetNames function.

Example: https://github.com/kataras/iris/tree/master/_examples/file-server/embedding-gziped-files-into-app

func (*APIBuilder) StaticHandler

func (api *APIBuilder) StaticHandler(systemPath string, showList bool, gzip bool) context.Handler

StaticHandler returns a new Handler which is ready to serve all kind of static files.

Note: The only difference from package-level `StaticHandler` is that this `StaticHandler“ receives a request path which is appended to the party's relative path and stripped here.

Usage: app := iris.New() ... mySubdomainFsServer := app.Party("mysubdomain.") h := mySubdomainFsServer.StaticHandler("./static_files", false, false) /* http://mysubdomain.mydomain.com/static/css/style.css */ mySubdomainFsServer.Get("/static", h) ...

func (*APIBuilder) StaticServe

func (api *APIBuilder) StaticServe(systemPath string, requestPath ...string) *Route

StaticServe serves a directory as web resource. Same as `StaticWeb`. DEPRECATED; use `StaticWeb` or `StaticHandler` (for more options) instead.

func (*APIBuilder) StaticWeb

func (api *APIBuilder) StaticWeb(requestPath string, systemPath string) *Route

StaticWeb returns a handler that serves HTTP requests with the contents of the file system rooted at directory.

first parameter: the route path second parameter: the system directory

for more options look router.StaticHandler.

api.StaticWeb("/static", "./static")

As a special case, the returned file server redirects any request ending in "/index.html" to the same path, without the final "index.html".

StaticWeb calls the `StripPrefix(fullpath, NewStaticHandlerBuilder(systemPath).Listing(false).Build())`.

Returns the GET *Route.

func (*APIBuilder) Subdomain

func (api *APIBuilder) Subdomain(subdomain string, middleware ...context.Handler) Party

Subdomain returns a new party which is responsible to register routes to this specific "subdomain".

If called from a child party then the subdomain will be prepended to the path instead of appended. So if app.Subdomain("admin").Subdomain("panel") then the result is: "panel.admin.".

func (*APIBuilder) Trace

func (api *APIBuilder) Trace(relativePath string, handlers ...context.Handler) *Route

Trace registers a route for the Trace http method.

Returns a *Route and an error which will be filled if route wasn't registered successfully.

func (*APIBuilder) Use

func (api *APIBuilder) Use(handlers ...context.Handler)

Use appends Handler(s) to the current Party's routes and child routes. If the current Party is the root, then it registers the middleware to all child Parties' routes too.

Call order matters, it should be called right before the routes that they care about these handlers.

If it's called after the routes then these handlers will never be executed. Use `UseGlobal` if you want to register begin handlers(middleware) that should be always run before all application's routes.

func (*APIBuilder) UseGlobal

func (api *APIBuilder) UseGlobal(handlers ...context.Handler)

UseGlobal registers handlers that should run at the very beginning. It prepends those handler(s) to all routes, including all parties, subdomains. It doesn't care about call order, it will prepend the handlers to all existing routes and the future routes that may being registered.

The difference from `.DoneGLobal` is that this/or these Handler(s) are being always running first. Use of `ctx.Next()` of those handler(s) is necessary to call the main handler or the next middleware. It's always a good practise to call it right before the `Application#Run` function.

func (*APIBuilder) WildcardSubdomain

func (api *APIBuilder) WildcardSubdomain(middleware ...context.Handler) Party

WildcardSubdomain returns a new party which is responsible to register routes to a dynamic, wildcard(ed) subdomain. A dynamic subdomain is a subdomain which can reply to any subdomain requests. Server will accept any subdomain (if not static subdomain found) and it will search and execute the handlers of this party.

type AssetValidator

type AssetValidator func(filename string) bool

AssetValidator returns true if "filename" is asset, i.e: strings.Contains(filename, ".").

type ErrorCodeHandler

type ErrorCodeHandler struct {
	StatusCode int
	Handlers   context.Handlers
	// contains filtered or unexported fields
}

ErrorCodeHandler is the entry of the list of all http error code handlers.

func (*ErrorCodeHandler) Fire

func (ch *ErrorCodeHandler) Fire(ctx context.Context)

Fire executes the specific an error http error status. it's being wrapped to make sure that the handler will render correctly.

type ErrorCodeHandlers

type ErrorCodeHandlers struct {
	// contains filtered or unexported fields
}

ErrorCodeHandlers contains the http error code handlers. User of this struct can register, get a status code handler based on a status code or fire based on a receiver context.

func (*ErrorCodeHandlers) Fire

func (s *ErrorCodeHandlers) Fire(ctx context.Context)

Fire executes an error http status code handler based on the context's status code.

If a handler is not already registered, then it creates & registers a new trivial handler on the-fly.

func (*ErrorCodeHandlers) Get

func (s *ErrorCodeHandlers) Get(statusCode int) *ErrorCodeHandler

Get returns an http error handler based on the "statusCode". If not found it returns nil.

func (*ErrorCodeHandlers) Register

func (s *ErrorCodeHandlers) Register(statusCode int, handlers ...context.Handler) *ErrorCodeHandler

Register registers an error http status code based on the "statusCode" < 200 || >= 400 (`context.StatusCodeNotSuccessful`). The handler is being wrapepd by a generic handler which will try to reset the body if recorder was enabled and/or disable the gzip if gzip response recorder was active.

type ExecutionOptions

type ExecutionOptions struct {
	// Force if true then the handler9s) will execute even if the previous (or/and current, depends on the type of the rule)
	// handler does not calling the `ctx.Next()`,
	// note that the only way remained to stop a next handler is with the `ctx.StopExecution()` if this option is true.
	//
	// If true and `ctx.Next()` exists in the handlers that it shouldn't be, the framework will understand it but use it wisely.
	//
	// Defaults to false.
	Force bool
}

ExecutionOptions is a set of default behaviors that can be changed in order to customize the execution flow of the routes' handlers with ease.

See `ExecutionRules` and `Party#SetExecutionRules` for more.

type ExecutionRules

type ExecutionRules struct {
	// Begin applies from `Party#Use`/`APIBUilder#UseGlobal` to the first...!last `Party#Handle`'s IF main handlers > 1.
	Begin ExecutionOptions
	// Done applies to the latest `Party#Handle`'s (even if one) and all done handlers.
	Done ExecutionOptions
	// Main applies to the `Party#Handle`'s all handlers, plays nice with the `Done` rule
	// when more than one handler was registered in `Party#Handle` without `ctx.Next()` (for Force: true).
	Main ExecutionOptions
}

ExecutionRules gives control to the execution of the route handlers outside of the handlers themselves. Usage:

Party#SetExecutionRules(ExecutionRules {
  Done: ExecutionOptions{Force: true},
})

See `Party#SetExecutionRules` for more.

type Party

type Party interface {
	// GetRelPath returns the current party's relative path.
	// i.e:
	// if r := app.Party("/users"), then the `r.GetRelPath()` is the "/users".
	// if r := app.Party("www.") or app.Subdomain("www") then the `r.GetRelPath()` is the "www.".
	GetRelPath() string
	// GetReporter returns the reporter for adding errors
	GetReporter() *errors.Reporter
	// Macros returns the macro collection that is responsible
	// to register custom macros with their own parameter types and their macro functions for all routes.
	//
	// Learn more at:  https://github.com/kataras/iris/tree/master/_examples/routing/dynamic-path
	Macros() *macro.Macros

	// Party groups routes which may have the same prefix and share same handlers,
	// returns that new rich subrouter.
	//
	// You can even declare a subdomain with relativePath as "mysub." or see `Subdomain`.
	Party(relativePath string, middleware ...context.Handler) Party
	// PartyFunc same as `Party`, groups routes that share a base path or/and same handlers.
	// However this function accepts a function that receives this created Party instead.
	// Returns the Party in order the caller to be able to use this created Party to continue the
	// top-bottom routes "tree".
	//
	// Note: `iris#Party` and `core/router#Party` describes the exactly same interface.
	//
	// Usage:
	// app.PartyFunc("/users", func(u iris.Party){
	//	u.Use(authMiddleware, logMiddleware)
	//	u.Get("/", getAllUsers)
	//	u.Post("/", createOrUpdateUser)
	//	u.Delete("/", deleteUser)
	// })
	//
	// Look `Party` for more.
	PartyFunc(relativePath string, partyBuilderFunc func(p Party)) Party
	// Subdomain returns a new party which is responsible to register routes to
	// this specific "subdomain".
	//
	// If called from a child party then the subdomain will be prepended to the path instead of appended.
	// So if app.Subdomain("admin").Subdomain("panel") then the result is: "panel.admin.".
	Subdomain(subdomain string, middleware ...context.Handler) Party

	// Use appends Handler(s) to the current Party's routes and child routes.
	// If the current Party is the root, then it registers the middleware to all child Parties' routes too.
	Use(middleware ...context.Handler)

	// Done appends to the very end, Handler(s) to the current Party's routes and child routes.
	// The difference from .Use is that this/or these Handler(s) are being always running last.
	Done(handlers ...context.Handler)
	// Reset removes all the begin and done handlers that may derived from the parent party via `Use` & `Done`,
	// and the execution rules.
	// Note that the `Reset` will not reset the handlers that are registered via `UseGlobal` & `DoneGlobal`.
	//
	// Returns this Party.
	Reset() Party

	// AllowMethods will re-register the future routes that will be registered
	// via `Handle`, `Get`, `Post`, ... to the given "methods" on that Party and its children "Parties",
	// duplicates are not registered.
	//
	// Call of `AllowMethod` will override any previous allow methods.
	AllowMethods(methods ...string) Party

	// SetExecutionRules alters the execution flow of the route handlers outside of the handlers themselves.
	//
	// For example, if for some reason the desired result is the (done or all) handlers to be executed no matter what
	// even if no `ctx.Next()` is called in the previous handlers, including the begin(`Use`),
	// the main(`Handle`) and the done(`Done`) handlers themselves, then:
	// Party#SetExecutionRules(iris.ExecutionRules {
	//   Begin: iris.ExecutionOptions{Force: true},
	//   Main:  iris.ExecutionOptions{Force: true},
	//   Done:  iris.ExecutionOptions{Force: true},
	// })
	//
	// Note that if : true then the only remained way to "break" the handler chain is by `ctx.StopExecution()` now that `ctx.Next()` does not matter.
	//
	// These rules are per-party, so if a `Party` creates a child one then the same rules will be applied to that as well.
	// Reset of these rules (before `Party#Handle`) can be done with `Party#SetExecutionRules(iris.ExecutionRules{})`.
	//
	// The most common scenario for its use can be found inside Iris MVC Applications;
	// when we want the `Done` handlers of that specific mvc app's `Party`
	// to be executed but we don't want to add `ctx.Next()` on the `OurController#EndRequest`.
	//
	// Returns this Party.
	//
	// Example: https://github.com/kataras/iris/tree/master/_examples/mvc/middleware/without-ctx-next
	SetExecutionRules(executionRules ExecutionRules) Party
	// Handle registers a route to the server's router.
	// if empty method is passed then handler(s) are being registered to all methods, same as .Any.
	//
	// Returns the read-only route information.
	Handle(method string, registeredPath string, handlers ...context.Handler) *Route
	// HandleMany works like `Handle` but can receive more than one
	// paths separated by spaces and returns always a slice of *Route instead of a single instance of Route.
	//
	// It's useful only if the same handler can handle more than one request paths,
	// otherwise use `Party` which can handle many paths with different handlers and middlewares.
	//
	// Usage:
	// 	app.HandleMany(iris.MethodGet, "/user /user/{id:uint64} /user/me", userHandler)
	// At the other side, with `Handle` we've had to write:
	// 	app.Handle(iris.MethodGet, "/user", userHandler)
	// 	app.Handle(iris.MethodGet, "/user/{id:uint64}", userHandler)
	// 	app.Handle(iris.MethodGet, "/user/me", userHandler)
	//
	// This method is used behind the scenes at the `Controller` function
	// in order to handle more than one paths for the same controller instance.
	HandleMany(method string, relativePath string, handlers ...context.Handler) []*Route

	// None registers an "offline" route
	// see context.ExecRoute(routeName) and
	// party.Routes().Online(handleResultregistry.*Route, "GET") and
	// Offline(handleResultregistry.*Route)
	//
	// Returns the read-only route information.
	None(path string, handlers ...context.Handler) *Route

	// Get registers a route for the Get http method.
	//
	// Returns the read-only route information.
	Get(path string, handlers ...context.Handler) *Route
	// Post registers a route for the Post http method.
	//
	// Returns the read-only route information.
	Post(path string, handlers ...context.Handler) *Route
	// Put registers a route for the Put http method.
	//
	// Returns the read-only route information.
	Put(path string, handlers ...context.Handler) *Route
	// Delete registers a route for the Delete http method.
	//
	// Returns the read-only route information.
	Delete(path string, handlers ...context.Handler) *Route
	// Connect registers a route for the Connect http method.
	//
	// Returns the read-only route information.
	Connect(path string, handlers ...context.Handler) *Route
	// Head registers a route for the Head http method.
	//
	// Returns the read-only route information.
	Head(path string, handlers ...context.Handler) *Route
	// Options registers a route for the Options http method.
	//
	// Returns the read-only route information.
	Options(path string, handlers ...context.Handler) *Route
	// Patch registers a route for the Patch http method.
	//
	// Returns the read-only route information.
	Patch(path string, handlers ...context.Handler) *Route
	// Trace registers a route for the Trace http method.
	//
	// Returns the read-only route information.
	Trace(path string, handlers ...context.Handler) *Route
	// Any registers a route for ALL of the http methods
	// (Get,Post,Put,Head,Patch,Options,Connect,Delete).
	Any(registeredPath string, handlers ...context.Handler) []*Route

	// StaticHandler returns a new Handler which is ready
	// to serve all kind of static files.
	//
	// Note:
	// The only difference from package-level `StaticHandler`
	// is that this `StaticHandler` receives a request path which
	// is appended to the party's relative path and stripped here.
	//
	// Usage:
	// app := iris.New()
	// ...
	// mySubdomainFsServer := app.Party("mysubdomain.")
	// h := mySubdomainFsServer.StaticHandler("./static_files", false, false)
	// /* http://mysubdomain.mydomain.com/static/css/style.css */
	// mySubdomainFsServer.Get("/static", h)
	// ...
	//
	StaticHandler(systemPath string, showList bool, gzip bool) context.Handler

	// StaticServe serves a directory as web resource
	// it's the simpliest form of the Static* functions
	// Almost same usage as StaticWeb
	// accepts only one required parameter which is the systemPath,
	// the same path will be used to register the GET and HEAD method routes.
	// If second parameter is empty, otherwise the requestPath is the second parameter
	// it uses gzip compression (compression on each request, no file cache).
	//
	// Returns the GET *Route.
	StaticServe(systemPath string, requestPath ...string) *Route
	// StaticContent registers a GET and HEAD method routes to the requestPath
	// that are ready to serve raw static bytes, memory cached.
	//
	// Returns the GET *Route.
	StaticContent(requestPath string, cType string, content []byte) *Route

	// StaticEmbedded  used when files are distributed inside the app executable, using go-bindata mostly
	// First parameter is the request path, the path which the files in the vdir will be served to, for example "/static"
	// Second parameter is the (virtual) directory path, for example "./assets"
	// Third parameter is the Asset function
	// Forth parameter is the AssetNames function.
	//
	// Returns the GET *Route.
	//
	// Example: https://github.com/kataras/iris/tree/master/_examples/file-server/embedding-files-into-app
	StaticEmbedded(requestPath string, vdir string, assetFn func(name string) ([]byte, error), namesFn func() []string) *Route
	// StaticEmbeddedGzip registers a route which can serve embedded gziped files
	// that are embedded using the https://github.com/kataras/bindata tool and only.
	// It's 8 times faster than the `StaticEmbeddedHandler` with `go-bindata` but
	// it sends gzip response only, so the client must be aware that is expecting a gzip body
	// (browsers and most modern browsers do that, so you can use it without fair).
	//
	//
	// Example: https://github.com/kataras/iris/tree/master/_examples/file-server/embedding-gziped-files-into-app
	StaticEmbeddedGzip(requestPath string, vdir string, gzipAssetFn func(name string) ([]byte, error), gzipNamesFn func() []string) *Route
	// Favicon serves static favicon
	// accepts 2 parameters, second is optional
	// favPath (string), declare the system directory path of the __.ico
	// requestPath (string), it's the route's path, by default this is the "/favicon.ico" because some browsers tries to get this by default first,
	// you can declare your own path if you have more than one favicon (desktop, mobile and so on)
	//
	// this func will add a route for you which will static serve the /yuorpath/yourfile.ico to the /yourfile.ico
	// (nothing special that you can't handle by yourself).
	// Note that you have to call it on every favicon you have to serve automatically (desktop, mobile and so on).
	//
	// Returns the GET *Route.
	Favicon(favPath string, requestPath ...string) *Route
	// StaticWeb returns a handler that serves HTTP requests
	// with the contents of the file system rooted at directory.
	//
	// first parameter: the route path
	// second parameter: the system directory
	//
	// for more options look router.StaticHandler.
	//
	//     router.StaticWeb("/static", "./static")
	//
	// As a special case, the returned file server redirects any request
	// ending in "/index.html" to the same path, without the final
	// "index.html".
	//
	// StaticWeb calls the `StripPrefix(fullpath, NewStaticHandlerBuilder(systemPath).Listing(false).Build())`.
	//
	// Returns the GET *Route.
	StaticWeb(requestPath string, systemPath string) *Route

	// Layout overrides the parent template layout with a more specific layout for this Party.
	// It returns the current Party.
	//
	// The "tmplLayoutFile" should be a relative path to the templates dir.
	// Usage:
	//
	// app := iris.New()
	// app.RegisterView(iris.$VIEW_ENGINE("./views", ".$extension"))
	// my := app.Party("/my").Layout("layouts/mylayout.html")
	// 	my.Get("/", func(ctx iris.Context) {
	// 		ctx.View("page1.html")
	// 	})
	//
	// Examples: https://github.com/kataras/iris/tree/master/_examples/view
	Layout(tmplLayoutFile string) Party
}

Party is just a group joiner of routes which have the same prefix and share same middleware(s) also. Party could also be named as 'Join' or 'Node' or 'Group' , Party chosen because it is fun.

Look the "APIBuilder" for its implementation.

type RequestHandler

type RequestHandler interface {
	// HandleRequest should handle the request based on the Context.
	HandleRequest(context.Context)
	// Build should builds the handler, it's being called on router's BuildRouter.
	Build(provider RoutesProvider) error
	// RouteExists reports whether a particular route exists.
	RouteExists(ctx context.Context, method, path string) bool
}

RequestHandler the middle man between acquiring a context and releasing it. By-default is the router algorithm.

func NewDefaultHandler

func NewDefaultHandler() RequestHandler

NewDefaultHandler returns the handler which is responsible to map the request with a route (aka mux implementation).

type Route

type Route struct {
	Name   string `json:"name"`   // "userRoute"
	Method string `json:"method"` // "GET"

	Subdomain string `json:"subdomain"` // "admin."

	// Handlers are the main route's handlers, executed by order.
	// Cannot be empty.
	Handlers        context.Handlers `json:"-"`
	MainHandlerName string           `json:"mainHandlerName"`

	Path string `json:"path"` // "/api/user/:id"
	// FormattedPath all dynamic named parameters (if any) replaced with %v,
	// used by Application to validate param values of a Route based on its name.
	FormattedPath string `json:"formattedPath"`
	// contains filtered or unexported fields
}

Route contains the information about a registered Route. If any of the following fields are changed then the caller should Refresh the router.

func NewRoute

func NewRoute(method, subdomain, unparsedPath, mainHandlerName string,
	handlers context.Handlers, macros macro.Macros) (*Route, error)

NewRoute returns a new route based on its method, subdomain, the path (unparsed or original), handlers and the macro container which all routes should share. It parses the path based on the "macros", handlers are being changed to validate the macros at serve time, if needed.

func (*Route) BuildHandlers

func (r *Route) BuildHandlers()

BuildHandlers is executed automatically by the router handler at the `Application#Build` state. Do not call it manually, unless you were defined your own request mux handler.

func (*Route) ChangeMethod

func (r *Route) ChangeMethod(newMethod string) bool

ChangeMethod will try to change the HTTP Method of this route instance. A call of `RefreshRouter` is required after this type of change in order to change to be really applied.

func (Route) IsOnline

func (r Route) IsOnline() bool

IsOnline returns true if the route is marked as "online" (state).

func (Route) RegisteredHandlersLen

func (r Route) RegisteredHandlersLen() int

RegisteredHandlersLen returns the end-developer's registered handlers, all except the macro evaluator handler if was required by the build process.

func (Route) ResolvePath

func (r Route) ResolvePath(args ...string) string

ResolvePath returns the formatted path's %v replaced with the args.

func (*Route) RestoreStatus

func (r *Route) RestoreStatus() bool

RestoreStatus will try to restore the status of this route instance, i.e if `SetStatusOffline` called on a "GET" route, then this function will make this route available with "GET" HTTP Method. Note if that you want to set status online for an offline registered route then you should call the `ChangeMethod` instead. It will return true if the status restored, otherwise false. A call of `RefreshRouter` is required after this type of change in order to change to be really applied.

func (*Route) SetStatusOffline

func (r *Route) SetStatusOffline() bool

SetStatusOffline will try make this route unavailable. A call of `RefreshRouter` is required after this type of change in order to change to be really applied.

func (Route) StaticPath

func (r Route) StaticPath() string

StaticPath returns the static part of the original, registered route path. if /user/{id} it will return /user if /user/{id}/friend/{friendid:uint64} it will return /user too if /assets/{filepath:path} it will return /assets.

func (Route) String

func (r Route) String() string

String returns the form of METHOD, SUBDOMAIN, TMPL PATH.

func (Route) Tmpl

func (r Route) Tmpl() macro.Template

Tmpl returns the path template, it contains the parsed template for the route's path. May contain zero named parameters.

Developer can get his registered path via Tmpl().Src, Route.Path is the path converted to match the underline router's specs.

func (Route) Trace

func (r Route) Trace() string

Trace returns some debug infos as a string sentence. Should be called after Build.

type RoutePathReverser

type RoutePathReverser struct {
	// contains filtered or unexported fields
}

RoutePathReverser contains methods that helps to reverse a (dynamic) path from a specific route, route name is required because a route may being registered on more than one http method.

func NewRoutePathReverser

func NewRoutePathReverser(apiRoutesProvider RoutesProvider, options ...RoutePathReverserOption) *RoutePathReverser

NewRoutePathReverser returns a new path reverser based on a routes provider, needed to get a route based on its name. Options is required for the URL function. See WithScheme and WithHost or WithServer.

func (*RoutePathReverser) Path

func (ps *RoutePathReverser) Path(routeName string, paramValues ...interface{}) string

Path returns a route path based on a route name and any dynamic named parameter's values-only.

func (*RoutePathReverser) URL

func (ps *RoutePathReverser) URL(routeName string, paramValues ...interface{}) (url string)

URL same as Path but returns the full uri, i.e https://mysubdomain.mydomain.com/hello/iris

type RoutePathReverserOption

type RoutePathReverserOption func(*RoutePathReverser)

RoutePathReverserOption option signature for the RoutePathReverser.

func WithHost

func WithHost(host string) RoutePathReverserOption

WithHost enables the RoutePathReverser's URL feature. Both "WithHost" and "WithScheme" can be different from the real server's listening address, i.e nginx in front.

func WithScheme

func WithScheme(scheme string) RoutePathReverserOption

WithScheme is an option for the RoutepathReverser, it sets the optional field "vscheme", v for virtual. if vscheme is empty then it will try to resolve it from the RoutePathReverser's vhost field.

See WithHost or WithServer to enable the URL feature.

func WithServer

func WithServer(srv *http.Server) RoutePathReverserOption

WithServer enables the RoutePathReverser's URL feature. It receives an *http.Server and tries to resolve a scheme and a host to be used in the URL function.

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router is the "director". Caller should provide a request handler (router implementation or root handler). Router is responsible to build the received request handler and run it to serve requests, based on the received context.Pool.

User can refresh the router with `RefreshRouter` whenever a route's field is changed by him.

func NewRouter

func NewRouter() *Router

NewRouter returns a new empty Router.

func (*Router) BuildRouter

func (router *Router) BuildRouter(cPool *context.Pool, requestHandler RequestHandler, routesProvider RoutesProvider, force bool) error

BuildRouter builds the router based on the context factory (explicit pool in this case), the request handler which manages how the main handler will multiplexes the routes provided by the third parameter, routerProvider (it's the api builder in this case) and its wrapper.

Use of RefreshRouter to re-build the router if needed.

func (*Router) Downgrade

func (router *Router) Downgrade(newMainHandler http.HandlerFunc)

Downgrade "downgrades", alters the router supervisor service(Router.mainHandler)

algorithm to a custom one,

be aware to change the global variables of 'ParamStart' and 'ParamWildcardStart'. can be used to implement a custom proxy or a custom router which should work with raw ResponseWriter, *Request instead of the Context(which again, can be retrieved by the Framework's context pool).

Note: Downgrade will by-pass the Wrapper, the caller is responsible for everything. Downgrade is thread-safe.

func (*Router) Downgraded

func (router *Router) Downgraded() bool

Downgraded returns true if this router is downgraded.

func (*Router) RefreshRouter

func (router *Router) RefreshRouter() error

RefreshRouter re-builds the router. Should be called when a route's state changed (i.e Method changed at serve-time).

func (*Router) RouteExists

func (router *Router) RouteExists(ctx context.Context, method, path string) bool

RouteExists reports whether a particular route exists It will search from the current subdomain of context's host, if not inside the root domain.

func (*Router) ServeHTTP

func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Router) ServeHTTPC

func (router *Router) ServeHTTPC(ctx context.Context)

ServeHTTPC serves the raw context, useful if we have already a context, it by-pass the wrapper.

func (*Router) WrapRouter

func (router *Router) WrapRouter(wrapperFunc WrapperFunc)

WrapRouter adds a wrapper on the top of the main router. Usually it's useful for third-party middleware when need to wrap the entire application with a middleware like CORS.

Developers can add more than one wrappers, those wrappers' execution comes from last to first. That means that the second wrapper will wrap the first, and so on.

Before build.

type RoutesProvider

type RoutesProvider interface {
	GetRoutes() []*Route
	GetRoute(routeName string) *Route
}

RoutesProvider should be implemented by iteral which contains the registered routes.

type SPABuilder

type SPABuilder struct {
	// Root  defaults to "/", it's the root path that explicitly set-ed,
	// this can be changed if more than SPAs are used on the same
	// iris router instance.
	Root string

	IndexNames      []string
	AssetHandler    context.Handler
	AssetValidators []AssetValidator
	// contains filtered or unexported fields
}

SPABuilder helps building a single page application server which serves both routes and files from the root path.

func NewSPABuilder

func NewSPABuilder(assetHandler context.Handler) *SPABuilder

NewSPABuilder returns a new Single Page Application builder It does what StaticWeb or StaticEmbedded expected to do when serving files and routes at the same time from the root "/" path.

Accepts a static asset handler, which can be an app.StaticHandler, app.StaticEmbeddedHandler...

func (*SPABuilder) AddIndexName

func (s *SPABuilder) AddIndexName(filename string) *SPABuilder

AddIndexName will add an index name. If path == $filename then it redirects to Root, which defaults to "/".

It can be called BEFORE the server start.

func (*SPABuilder) ChangeRoot

func (s *SPABuilder) ChangeRoot(path string) *SPABuilder

ChangeRoot modifies the `Root` request path that is explicitly set-ed if the `AssetHandler` gave a Not Found (404) previously, if request's path is the passed "path" then it explicitly sets that and it retries executing the `AssetHandler`.

Empty Root means that let 404 fire from server-side anyways.

Change it ONLY if you use more than one typical SPAs on the same Iris Application instance.

func (*SPABuilder) Handler

func (s *SPABuilder) Handler(ctx context.Context)

Handler serves the asset handler but in addition, it makes some checks before that, based on the `AssetValidators` and `IndexNames`.

type StaticHandlerBuilder

type StaticHandlerBuilder interface {
	Gzip(enable bool) StaticHandlerBuilder
	Listing(listDirectoriesOnOff bool) StaticHandlerBuilder
	Build() context.Handler
}

StaticHandlerBuilder is the web file system's Handler builder use that or the iris.StaticHandler/StaticWeb methods.

func NewStaticHandlerBuilder

func NewStaticHandlerBuilder(dir string) StaticHandlerBuilder

NewStaticHandlerBuilder returns a new Handler which serves static files supports gzip, no listing and much more Note that, this static builder returns a Handler it doesn't cares about the rest of your iris configuration.

Use the iris.StaticHandler/StaticWeb in order to serve static files on more automatic way this builder is used by people who have more complicated application structure and want a fluent api to work on.

type WrapperFunc

type WrapperFunc func(w http.ResponseWriter, r *http.Request, firstNextIsTheRouter http.HandlerFunc)

WrapperFunc is used as an expected input parameter signature for the WrapRouter. It's a "low-level" signature which is compatible with the net/http. It's being used to run or no run the router based on a custom logic.

func NewSubdomainRedirectWrapper

func NewSubdomainRedirectWrapper(rootDomainGetter func() string, from, to string) WrapperFunc

NewSubdomainRedirectWrapper returns a router wrapper which if it's registered to the router via `router#WrapRouter` it redirects(StatusMovedPermanently) a subdomain or the root domain to another subdomain or to the root domain.

It receives three arguments, the first one is a function which returns the root domain, (in the application it's the app.ConfigurationReadOnly().GetVHost()). The second and third are the from and to locations, 'from' can be a wildcard subdomain as well (*. or *) 'to' is not allowed to be a wildcard for obvious reasons, 'from' can be the root domain when the 'to' is not the root domain and visa-versa. To declare a root domain as 'from' or 'to' you MUST pass an empty string or a slash('/') or a dot('.'). Important note: the 'from' and 'to' should end with "." like we use the `APIBuilder#Party`, if they are subdomains.

Usage(package-level): sd := NewSubdomainRedirectWrapper(func() string { return "mydomain.com" }, ".", "www.") router.WrapRouter(sd)

Usage(high-level using `iris#Application.SubdomainRedirect`) www := app.Subdomain("www") app.SubdomainRedirect(app, www) Because app's rel path is "/" it translates it to the root domain and www's party's rel path is the "www.", so it's the target subdomain.

All the above code snippets will register a router wrapper which will redirect all http(s)://mydomain.com/%anypath% to http(s)://www.mydomain.com/%anypath%.

One or more subdomain redirect wrappers can be used to the same router instance.

NewSubdomainRedirectWrapper may return nil if not allowed input arguments values were received but in that case, the `WrapRouter` will, simply, ignore that wrapper.

Example: https://github.com/kataras/iris/tree/master/_examples/subdomains/redirect

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL