mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
rewrite middleware: add PrimarySubdomain and simplify its usage example
This commit is contained in:
@@ -7,45 +7,23 @@ import (
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
/*
|
||||
rewriteOptions := rewrite.Options{
|
||||
RedirectMatch: []string{
|
||||
"301 /seo/(.*) /$1",
|
||||
"301 /docs/v12(.*) /docs",
|
||||
"301 /old(.*) /",
|
||||
}}
|
||||
OR Load from file:
|
||||
*/
|
||||
rewriteOptions := rewrite.LoadOptions("redirects.yml")
|
||||
rewriteEngine, err := rewrite.New(rewriteOptions)
|
||||
if err != nil { // reports any line parse errors.
|
||||
app.Logger().Fatal(err)
|
||||
}
|
||||
|
||||
app.Get("/", index)
|
||||
app.Get("/about", about)
|
||||
app.Get("/docs", docs)
|
||||
|
||||
/*
|
||||
// To use it per-party, even if not route match:
|
||||
app.UseRouter(rewriteEngine.Handler)
|
||||
// To use it per-party when route match:
|
||||
app.Use(rewriteEngine.Handler)
|
||||
//
|
||||
// To use it on a single route just pass it to the Get/Post method.
|
||||
// To make the entire application respect the rewrite rules
|
||||
// you have to wrap the Iris Router and pass the Wrapper method instead,
|
||||
// (recommended way to use this middleware, right before Listen/Run):
|
||||
*/
|
||||
app.WrapRouter(rewriteEngine.Wrapper)
|
||||
app.Subdomain("test").Get("/", testIndex)
|
||||
|
||||
// http://localhost:8080/seo
|
||||
redirects := rewrite.Load("redirects.yml")
|
||||
app.WrapRouter(redirects)
|
||||
|
||||
// http://mydomain.com:8080/seo/about -> http://www.mydomain.com:8080/about
|
||||
// http://test.mydomain.com:8080
|
||||
// http://localhost:8080/seo -> http://localhost:8080
|
||||
// http://localhost:8080/about
|
||||
// http://localhost:8080/docs/v12/hello
|
||||
// http://localhost:8080/docs/v12some
|
||||
// http://localhost:8080/oldsome
|
||||
// http://localhost:8080/oldindex/random
|
||||
// http://localhost:8080/docs/v12/hello -> http://localhost:8080/docs
|
||||
// http://localhost:8080/docs/v12some -> http://localhost:8080/docs
|
||||
// http://localhost:8080/oldsome -> http://localhost:8080
|
||||
// http://localhost:8080/oldindex/random -> http://localhost:8080
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
@@ -60,3 +38,30 @@ func about(ctx iris.Context) {
|
||||
func docs(ctx iris.Context) {
|
||||
ctx.WriteString("Docs")
|
||||
}
|
||||
|
||||
func testIndex(ctx iris.Context) {
|
||||
ctx.WriteString("Test Subdomain Index")
|
||||
}
|
||||
|
||||
/* More...
|
||||
rewriteOptions := rewrite.Options{
|
||||
RedirectMatch: []string{
|
||||
"301 /seo/(.*) /$1",
|
||||
"301 /docs/v12(.*) /docs",
|
||||
"301 /old(.*) /",
|
||||
},
|
||||
PrimarySubdomain: "www",
|
||||
}
|
||||
rewriteEngine, err := rewrite.New(rewriteOptions)
|
||||
|
||||
// To use it per-party use its `Handler` method. Even if not route match:
|
||||
app.UseRouter(rewriteEngine.Handler)
|
||||
// To use it per-party when route match:
|
||||
app.Use(rewriteEngine.Handler)
|
||||
//
|
||||
// To use it on a single route just pass it to the Get/Post method.
|
||||
//
|
||||
// To make the entire application respect the redirect rules
|
||||
// you have to wrap the Iris Router and pass the `Rewrite` method instead
|
||||
// as we did at this example.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user