router

package
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2020 License: BSD-3-Clause Imports: 25 Imported by: 0

Documentation

Index

Constants

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 (
	// 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 (
	// MethodNone is a Virtual method
	// to store the "offline" routes.
	MethodNone = "NONE"
)

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",
	}
)
View Source
var StaticCacheDuration = 20 * time.Second

StaticCacheDuration expiration duration for INACTIVE file handlers, it's the only one global configuration which can be changed.

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) context.Handler

StaticEmbeddedHandler returns a Handler which can serve embedded into executable files.

Examples: https://github.com/alphayan/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 := iris.StaticHandler("./static_files", false, false) h := router.StripPrefix("/static", fileserver) app.Get("/static", 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) 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 The difference from .Use is that this/or these Handler(s) are being always running last.

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) 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 `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) 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:int} /user/me", genericUserHandler)

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

app.Handle("GET", "/user", userHandler)
app.Handle("GET", "/user/{id:int}", 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 oerrides the parent template layout with a more specific layout for this Party returns this Party, to continue as normal Usage: app := iris.New() my := app.Party("/my").Layout("layouts/mylayout.html")

{
	my.Get("/", func(ctx context.Context) {
		ctx.MustRender("page1.html", nil)
	})
}

func (*APIBuilder) Macros

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

Macros returns the macro map which is responsible to register custom macro functions for all routes.

Learn more at: https://github.com/alphayan/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. See: 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" >= 400. 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) 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" Third parameter is the Asset function Forth parameter is the AssetNames function.

Returns the GET *Route.

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

func (*APIBuilder) StaticEmbeddedHandler

func (api *APIBuilder) StaticEmbeddedHandler(vdir string, assetFn func(name string) ([]byte, error), namesFn func() []string) context.Handler

StaticEmbeddedHandler returns a Handler which can serve embedded into executable files.

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

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 before all routes, including all parties, subdomains and other middleware that were registered before or will be after. It doesn't care about call order, it will prepend the handlers to all existing routes and the future routes that may being registered.

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" >= 400. 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 Party

type Party interface {
	// GetReporter returns the reporter for adding errors
	GetReporter() *errors.Reporter
	// Macros returns the macro map which is responsible
	// to register custom macro functions for all routes.
	//
	// Learn more at:  https://github.com/alphayan/iris/tree/master/_examples/routing/dynamic-path
	Macros() *macro.Map

	// 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)

	// 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:int} /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:int}", 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/alphayan/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

	// 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 oerrides the parent template layout with a more specific layout for this Party
	// returns this Party, to continue as normal
	// Usage:
	// app := iris.New()
	// my := app.Party("/my").Layout("layouts/mylayout.html")
	// 	{
	// 		my.Get("/", func(ctx context.Context) {
	// 			ctx.MustRender("page1.html", nil)
	// 		})
	// 	}
	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 is same as context.Handler but its usage is only about routing,
	// separate the concept here.
	HandleRequest(context.Context)
	// Build  should builds the handler, it's being called on router's BuildRouter.
	Build(provider RoutesProvider) error
}

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 // "userRoute"
	Method    string // "GET"
	Subdomain string // "admin."

	Path string // "/api/user/:id"

	// Handlers are the main route's handlers, executed by order.
	// Cannot be empty.
	Handlers        context.Handlers
	MainHandlerName string

	// 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
	// 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.Map) (*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) IsOnline

func (r Route) IsOnline() bool

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

func (Route) ResolvePath

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

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

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:int} 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, i 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) 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) 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 {
	IndexNames      []string
	AssetHandler    context.Handler
	AssetValidators []AssetValidator
}

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 "/".

It can be called after the `BuildWrapper ` as well but BEFORE the server start.

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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