mirror of
https://github.com/kataras/iris.git
synced 2026-01-08 20:41:57 +00:00
add 'Configuration.PathIntelligence' and 'OnErrorCode' and 'OnAnyErrorCode' on APIContainer
Former-commit-id: bc3d0232106622063205f326bfa4ed3aa84179de
This commit is contained in:
@@ -229,3 +229,19 @@ func (api *APIContainer) Any(relativePath string, handlersFn ...interface{}) (ro
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// OnErrorCode registers a handlers chain for this `Party` for a specific HTTP status code.
|
||||
// Read more at: http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
|
||||
// Look `OnAnyErrorCode` too.
|
||||
func (api *APIContainer) OnErrorCode(statusCode int, handlersFn ...interface{}) []*Route {
|
||||
handlers := api.convertHandlerFuncs("/{tail:path}", handlersFn...)
|
||||
return api.Self.OnErrorCode(statusCode, handlers...)
|
||||
}
|
||||
|
||||
// OnAnyErrorCode registers a handlers chain for all error codes
|
||||
// (4xxx and 5xxx, change the `ClientErrorCodes` and `ServerErrorCodes` variables to modify those)
|
||||
// Look `OnErrorCode` too.
|
||||
func (api *APIContainer) OnAnyErrorCode(handlersFn ...interface{}) []*Route {
|
||||
handlers := api.convertHandlerFuncs("/{tail:path}", handlersFn...)
|
||||
return api.Self.OnAnyErrorCode(handlers...)
|
||||
}
|
||||
|
||||
@@ -417,6 +417,16 @@ func (h *routerHandler) HandleRequest(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
if config.GetEnablePathIntelligence() && method == http.MethodGet {
|
||||
closestPaths := ctx.FindClosest(1)
|
||||
if len(closestPaths) > 0 {
|
||||
u := ctx.Request().URL
|
||||
u.Path = closestPaths[0]
|
||||
ctx.Redirect(u.String(), http.StatusMovedPermanently)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ctx.StatusCode(http.StatusNotFound)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user