mirror of
https://github.com/kataras/iris.git
synced 2026-01-10 05:25:58 +00:00
Give read access to the current route, a feature that many of you asked for
Former-commit-id: 39295ac1331ee08d3047c84f5c8ea152bce96781
This commit is contained in:
@@ -34,6 +34,14 @@ type Application interface {
|
||||
// It is ready to use after Build state.
|
||||
ServeHTTP(w http.ResponseWriter, r *http.Request)
|
||||
|
||||
// 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 core/router/APIBuilder#GetRoute for more.
|
||||
GetRouteReadOnly(routeName string) RouteReadOnly
|
||||
|
||||
// FireErrorCode executes an error http status code handler
|
||||
// based on the context's status code.
|
||||
//
|
||||
|
||||
@@ -148,7 +148,6 @@ func (r RequestParams) Len() int {
|
||||
// context.Context is very extensible and developers can override
|
||||
// its methods if that is actually needed.
|
||||
type Context interface {
|
||||
|
||||
// BeginRequest is executing once for each request
|
||||
// it should prepare the (new or acquired from pool) context's fields for the new request.
|
||||
//
|
||||
@@ -176,6 +175,20 @@ type Context interface {
|
||||
// Request returns the original *http.Request, as expected.
|
||||
Request() *http.Request
|
||||
|
||||
// SetCurrentRouteName sets the route's name internally,
|
||||
// in order to be able to find the correct current "read-only" Route when
|
||||
// end-developer calls the `GetCurrentRoute()` function.
|
||||
// It's being initialized by the Router, if you change that name
|
||||
// manually nothing really happens except that you'll get other
|
||||
// route via `GetCurrentRoute()`.
|
||||
// Instead, to execute a different path
|
||||
// from this context you should use the `Exec` function
|
||||
// or change the handlers via `SetHandlers/AddHandler` functions.
|
||||
SetCurrentRouteName(currentRouteName string)
|
||||
// GetCurrentRoute returns the current registered "read-only" route that
|
||||
// was being registered to this request's path.
|
||||
GetCurrentRoute() RouteReadOnly
|
||||
|
||||
// Do calls the SetHandlers(handlers)
|
||||
// and executes the first handler,
|
||||
// handlers should not be empty.
|
||||
@@ -799,6 +812,9 @@ type context struct {
|
||||
writer ResponseWriter
|
||||
// the original http.Request
|
||||
request *http.Request
|
||||
// the current route's name registered to this request path.
|
||||
currentRouteName string
|
||||
|
||||
// the local key-value storage
|
||||
params RequestParams // url named parameters
|
||||
values memstore.Store // generic storage, middleware communication
|
||||
@@ -880,6 +896,25 @@ func (ctx *context) Request() *http.Request {
|
||||
return ctx.request
|
||||
}
|
||||
|
||||
// SetCurrentRouteName sets the route's name internally,
|
||||
// in order to be able to find the correct current "read-only" Route when
|
||||
// end-developer calls the `GetCurrentRoute()` function.
|
||||
// It's being initialized by the Router, if you change that name
|
||||
// manually nothing really happens except that you'll get other
|
||||
// route via `GetCurrentRoute()`.
|
||||
// Instead, to execute a different path
|
||||
// from this context you should use the `Exec` function
|
||||
// or change the handlers via `SetHandlers/AddHandler` functions.
|
||||
func (ctx *context) SetCurrentRouteName(currentRouteName string) {
|
||||
ctx.currentRouteName = currentRouteName
|
||||
}
|
||||
|
||||
// GetCurrentRoute returns the current registered "read-only" route that
|
||||
// was being registered to this request's path.
|
||||
func (ctx *context) GetCurrentRoute() RouteReadOnly {
|
||||
return ctx.app.GetRouteReadOnly(ctx.currentRouteName)
|
||||
}
|
||||
|
||||
// Do calls the SetHandlers(handlers)
|
||||
// and executes the first handler,
|
||||
// handlers should not be empty.
|
||||
|
||||
18
context/route.go
Normal file
18
context/route.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package context
|
||||
|
||||
// RouteReadOnly allows decoupled access to the current route
|
||||
// inside the context.
|
||||
type RouteReadOnly interface {
|
||||
// Name returns the route's name.
|
||||
Name() string
|
||||
// String returns the form of METHOD, SUBDOMAIN, TMPL PATH.
|
||||
String() string
|
||||
// Path returns the route's original registered path.
|
||||
Path() string
|
||||
|
||||
// IsOnline returns true if the route is marked as "online" (state).
|
||||
IsOnline() bool
|
||||
|
||||
// ResolvePath returns the formatted path's %v replaced with the args.
|
||||
ResolvePath(args ...string) string
|
||||
}
|
||||
Reference in New Issue
Block a user