-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add context.FindClosest(n) to find closest paths - useful for 404 pag…
…es to suggest valid pages Former-commit-id: 90ff7c9da5369df5bd99fbbecf9955a8c555fea5
- Loading branch information
Showing
11 changed files
with
137 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.Run(iris.Addr(":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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters