1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-08 12:31:58 +00:00

Add a better example for the recaptcha middleware as requested at: https://github.com/kataras/iris/issues/799

Former-commit-id: 85c3286a9d6be5cf47631e7608f70f3790934e64
This commit is contained in:
kataras
2017-11-04 02:59:21 +02:00
parent 7cd78df9e3
commit ef41e07d4e
11 changed files with 153 additions and 59 deletions

View File

@@ -146,6 +146,13 @@ func (api *APIBuilder) Handle(method string, relativePath string, handlers ...co
}
fullpath := api.relativePath + relativePath // for now, keep the last "/" if any, "/xyz/"
if len(handlers) == 0 {
api.reporter.Add("missing handlers for route %s: %s", method, fullpath)
return nil
}
// before join the middleware + handlers + done handlers.
possibleMainHandlerName := context.HandlerName(handlers[0])
// global begin handlers -> middleware that are registered before route registration
// -> handlers that are passed to this Handle function.
@@ -158,7 +165,7 @@ func (api *APIBuilder) Handle(method string, relativePath string, handlers ...co
// here we separate the subdomain and relative path
subdomain, path := splitSubdomainAndPath(fullpath)
r, err := NewRoute(method, subdomain, path, routeHandlers, api.macros)
r, err := NewRoute(method, subdomain, path, possibleMainHandlerName, routeHandlers, api.macros)
if err != nil { // template path parser errors:
api.reporter.Add("%v -> %s:%s:%s", err, method, subdomain, path)
return nil

View File

@@ -1,6 +1,7 @@
package router
import (
"github.com/kataras/golog"
"html"
"net/http"
"sort"
@@ -136,7 +137,9 @@ func (h *routerHandler) Build(provider RoutesProvider) error {
if err := h.addRoute(r); err != nil {
// node errors:
rp.Add("%v -> %s", err, r.String())
continue
}
golog.Debugf(r.Trace())
}
return rp.Return()

View File

@@ -22,7 +22,8 @@ type Route struct {
beginHandlers context.Handlers
// Handlers are the main route's handlers, executed by order.
// Cannot be empty.
Handlers context.Handlers
Handlers context.Handlers
mainHandlerName string
// temp storage, they're appended to the Handlers on build.
// Execution happens after Begin and main Handler(s), can be empty.
doneHandlers context.Handlers
@@ -36,7 +37,7 @@ type Route struct {
// 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 NewRoute(method, subdomain, unparsedPath string,
func NewRoute(method, subdomain, unparsedPath, mainHandlerName string,
handlers context.Handlers, macros *macro.Map) (*Route, error) {
tmpl, err := macro.Parse(unparsedPath, macros)
@@ -54,13 +55,14 @@ func NewRoute(method, subdomain, unparsedPath string,
formattedPath := formatPath(path)
route := &Route{
Name: defaultName,
Method: method,
Subdomain: subdomain,
tmpl: tmpl,
Path: path,
Handlers: handlers,
FormattedPath: formattedPath,
Name: defaultName,
Method: method,
Subdomain: subdomain,
tmpl: tmpl,
Path: path,
Handlers: handlers,
mainHandlerName: mainHandlerName,
FormattedPath: formattedPath,
}
return route, nil
}
@@ -203,6 +205,27 @@ func (r Route) ResolvePath(args ...string) string {
return formattedPath
}
// Trace returns some debug infos as a string sentence.
// Should be called after Build.
func (r Route) Trace() string {
printfmt := fmt.Sprintf("%s:", r.Method)
if r.Subdomain != "" {
printfmt += fmt.Sprintf(" %s", r.Subdomain)
}
printfmt += fmt.Sprintf(" %s ", r.Tmpl().Src)
if l := len(r.Handlers); l > 1 {
printfmt += fmt.Sprintf("-> %s() and %d more", r.mainHandlerName, l-1)
} else {
printfmt += fmt.Sprintf("-> %s()", r.mainHandlerName)
}
// printfmt := fmt.Sprintf("%s: %s >> %s", r.Method, r.Subdomain+r.Tmpl().Src, r.mainHandlerName)
// if l := len(r.Handlers); l > 0 {
// printfmt += fmt.Sprintf(" and %d more", l)
// }
return printfmt // without new line.
}
type routeReadOnlyWrapper struct {
*Route
}
@@ -222,3 +245,7 @@ func (rd routeReadOnlyWrapper) Subdomain() string {
func (rd routeReadOnlyWrapper) Path() string {
return rd.Route.tmpl.Src
}
func (rd routeReadOnlyWrapper) Trace() string {
return rd.Route.Trace()
}