mirror of
https://github.com/kataras/iris.git
synced 2025-12-27 14:57:05 +00:00
add 'Configuration.PathIntelligence' and 'OnErrorCode' and 'OnAnyErrorCode' on APIContainer
Former-commit-id: bc3d0232106622063205f326bfa4ed3aa84179de
This commit is contained in:
24
_examples/routing/intelligence/main.go
Normal file
24
_examples/routing/intelligence/main.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import "github.com/kataras/iris/v12"
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Get("/home", handler)
|
||||
app.Get("/contact", handler)
|
||||
app.Get("/contract", handler)
|
||||
|
||||
// http://localhost:8080/home
|
||||
// http://localhost:8080/hom
|
||||
//
|
||||
// http://localhost:8080/contact
|
||||
// http://localhost:8080/cont
|
||||
//
|
||||
// http://localhost:8080/contract
|
||||
// http://localhost:8080/contr
|
||||
app.Listen(":8080", iris.WithPathIntelligence)
|
||||
}
|
||||
|
||||
func handler(ctx iris.Context) {
|
||||
ctx.Writef("Path: %s", ctx.Path())
|
||||
}
|
||||
37
_examples/routing/intelligence/manual/main.go
Normal file
37
_examples/routing/intelligence/manual/main.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import "github.com/kataras/iris/v12"
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.OnErrorCode(iris.StatusNotFound, notFound)
|
||||
|
||||
// [register some routes...]
|
||||
app.Get("/home", handler)
|
||||
app.Get("/news", handler)
|
||||
app.Get("/news/politics", handler)
|
||||
app.Get("/user/profile", handler)
|
||||
app.Get("/user", handler)
|
||||
app.Get("/newspaper", handler)
|
||||
app.Get("/user/{id}", handler)
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func notFound(ctx iris.Context) {
|
||||
suggestPaths := ctx.FindClosest(3)
|
||||
if len(suggestPaths) == 0 {
|
||||
ctx.WriteString("404 not found")
|
||||
return
|
||||
}
|
||||
|
||||
ctx.HTML("Did you mean?<ul>")
|
||||
for _, s := range suggestPaths {
|
||||
ctx.HTML(`<li><a href="%s">%s</a></li>`, s, s)
|
||||
}
|
||||
ctx.HTML("</ul>")
|
||||
}
|
||||
|
||||
func handler(ctx iris.Context) {
|
||||
ctx.Writef("Path: %s", ctx.Path())
|
||||
}
|
||||
Reference in New Issue
Block a user