mirror of
https://github.com/kataras/iris.git
synced 2026-01-09 13:05:56 +00:00
NEW: Application#SubdomainRedirect. Example: https://github.com/kataras/iris/blob/master/_examples/subdomains/redirect/main.go
Former-commit-id: d8dd7c426dc9f14c870f103fef703595a2915612
This commit is contained in:
@@ -10,6 +10,7 @@ It doesn't always contain the "best ways" but it does cover each important featu
|
||||
|
||||
- [Hello world!](hello-world/main.go)
|
||||
- [Glimpse](overview/main.go)
|
||||
- [WWW](www/main.go)
|
||||
- [Tutorial: Online Visitors](tutorial/online-visitors/main.go)
|
||||
- [Tutorial: A Todo MVC Application using Iris and Vue.js](https://hackernoon.com/a-todo-mvc-application-using-iris-and-vue-js-5019ff870064)
|
||||
- [Tutorial: URL Shortener using BoltDB](https://medium.com/@kataras/a-url-shortener-service-using-go-iris-and-bolt-4182f0b00ae7)
|
||||
@@ -267,7 +268,8 @@ Follow the examples below,
|
||||
- [Single](subdomains/single/main.go)
|
||||
- [Multi](subdomains/multi/main.go)
|
||||
- [Wildcard](subdomains/wildcard/main.go)
|
||||
- [WWW](subdomains/www/main.go)
|
||||
- [WWW](subdomains/www/main.go)
|
||||
- [Redirect fast](subdomains/redirect/main.go)
|
||||
|
||||
### Convert `http.Handler/HandlerFunc`
|
||||
|
||||
|
||||
4
_examples/subdomains/redirect/hosts
Normal file
4
_examples/subdomains/redirect/hosts
Normal file
@@ -0,0 +1,4 @@
|
||||
127.0.0.1 mydomain.com
|
||||
127.0.0.1 www.mydomain.com
|
||||
|
||||
# Windows: Drive:/Windows/system32/drivers/etc/hosts, on Linux: /etc/hosts
|
||||
68
_examples/subdomains/redirect/main.go
Normal file
68
_examples/subdomains/redirect/main.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// Package main shows how to register a simple 'www' subdomain,
|
||||
// using the `app.WWW` method, which will register a router wrapper which will
|
||||
// redirect all 'mydomain.com' requests to 'www.mydomain.com'.
|
||||
// Check the 'hosts' file to see how to test the 'mydomain.com' on your local machine.
|
||||
package main
|
||||
|
||||
import "github.com/kataras/iris"
|
||||
|
||||
const addr = "mydomain.com:80"
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
|
||||
// http(s)://mydomain.com, will be redirect to http(s)://www.mydomain.com.
|
||||
// The `www` variable is the `app.Subdomain("www")`.
|
||||
//
|
||||
// app.WWW() wraps the router so it can redirect all incoming requests
|
||||
// that comes from 'http(s)://mydomain.com/%path%' (www is missing)
|
||||
// to `http(s)://www.mydomain.com/%path%`.
|
||||
//
|
||||
// Try:
|
||||
// http://mydomain.com -> http://www.mydomain.com
|
||||
// http://mydomain.com/users -> http://www.mydomain.com/users
|
||||
// http://mydomain.com/users/login -> http://www.mydomain.com/users/login
|
||||
app.Run(iris.Addr(addr))
|
||||
}
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("This will never be executed.")
|
||||
})
|
||||
|
||||
www := app.Subdomain("www") // <- same as app.Party("www.")
|
||||
www.Get("/", index)
|
||||
|
||||
// www is an `iris.Party`, use it like you already know, like grouping routes.
|
||||
www.PartyFunc("/users", func(p iris.Party) { // <- same as www.Party("/users").Get(...)
|
||||
p.Get("/", usersIndex)
|
||||
p.Get("/login", getLogin)
|
||||
})
|
||||
|
||||
// redirects mydomain.com/%anypath% to www.mydomain.com/%anypath%.
|
||||
// First argument is the 'from' and second is the 'to/target'.
|
||||
app.SubdomainRedirect(app, www)
|
||||
|
||||
// If you need to redirect any subdomain to 'www' then:
|
||||
// app.SubdomainRedirect(app.WildcardSubdomain(), www)
|
||||
// If you need to redirect from a subdomain to the root domain then:
|
||||
// app.SubdomainRedirect(app.Subdomain("mysubdomain"), app)
|
||||
//
|
||||
// Note that app.Party("mysubdomain.") and app.Subdomain("mysubdomain")
|
||||
// is the same exactly thing, the difference is that the second can omit the last dot('.').
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
func index(ctx iris.Context) {
|
||||
ctx.Writef("This is the www.mydomain.com endpoint.")
|
||||
}
|
||||
|
||||
func usersIndex(ctx iris.Context) {
|
||||
ctx.Writef("This is the www.mydomain.com/users endpoint.")
|
||||
}
|
||||
|
||||
func getLogin(ctx iris.Context) {
|
||||
ctx.Writef("This is the www.mydomain.com/users/login endpoint.")
|
||||
}
|
||||
33
_examples/subdomains/redirect/main_test.go
Normal file
33
_examples/subdomains/redirect/main_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/httptest"
|
||||
)
|
||||
|
||||
func TestSubdomainRedirectWWW(t *testing.T) {
|
||||
app := newApp()
|
||||
root := strings.TrimSuffix(addr, ":80")
|
||||
|
||||
e := httptest.New(t, app)
|
||||
|
||||
tests := []struct {
|
||||
path string
|
||||
response string
|
||||
}{
|
||||
{"/", fmt.Sprintf("This is the www.%s endpoint.", root)},
|
||||
{"/users", fmt.Sprintf("This is the www.%s/users endpoint.", root)},
|
||||
{"/users/login", fmt.Sprintf("This is the www.%s/users/login endpoint.", root)},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
req := e.GET(test.path)
|
||||
// req.WithURL("http://www." + root)
|
||||
|
||||
req.Expect().Status(httptest.StatusOK).Body().Equal(test.response)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,25 +11,41 @@ func newApp() *iris.Application {
|
||||
app.Get("/about", info)
|
||||
app.Get("/contact", info)
|
||||
|
||||
usersAPI := app.Party("/api/users")
|
||||
{
|
||||
usersAPI.Get("/", info)
|
||||
usersAPI.Get("/{id:int}", info)
|
||||
app.PartyFunc("/api/users", func(r iris.Party) {
|
||||
r.Get("/", info)
|
||||
r.Get("/{id:int}", info)
|
||||
|
||||
usersAPI.Post("/", info)
|
||||
r.Post("/", info)
|
||||
|
||||
usersAPI.Put("/{id:int}", info)
|
||||
}
|
||||
r.Put("/{id:int}", info)
|
||||
}) /* <- same as:
|
||||
usersAPI := app.Party("/api/users")
|
||||
{ // those brackets are just syntactic-sugar things.
|
||||
// This method is rarely used but you can make use of it when you want
|
||||
// scoped variables to that code block only.
|
||||
usersAPI.Get/Post...
|
||||
}
|
||||
usersAPI.Get/Post...
|
||||
*/
|
||||
|
||||
www := app.Party("www.")
|
||||
{
|
||||
// get all routes that are registered so far, including all "Parties" but subdomains:
|
||||
// http://www.mydomain.com/hi
|
||||
www.Get("/hi", func(ctx iris.Context) {
|
||||
ctx.Writef("hi from www.mydomain.com")
|
||||
})
|
||||
|
||||
// Just to show how you can get all routes and copy them to another
|
||||
// party or subdomain:
|
||||
// Get all routes that are registered so far, including all "Parties" but subdomains:
|
||||
currentRoutes := app.GetRoutes()
|
||||
// register them to the www subdomain/vhost as well:
|
||||
// Register them to the www subdomain/vhost as well:
|
||||
for _, r := range currentRoutes {
|
||||
www.Handle(r.Method, r.Path, r.Handlers...)
|
||||
}
|
||||
}
|
||||
// See also the "subdomains/redirect" to register redirect router wrappers between subdomains,
|
||||
// i.e mydomain.com to www.mydomain.com (like facebook does for SEO reasons(;)).
|
||||
|
||||
return app
|
||||
}
|
||||
@@ -43,6 +59,7 @@ func main() {
|
||||
// http://mydomain.com/api/users/42
|
||||
|
||||
// http://www.mydomain.com
|
||||
// http://www.mydomain.com/hi
|
||||
// http://www.mydomain.com/about
|
||||
// http://www.mydomain.com/contact
|
||||
// http://www.mydomain.com/api/users
|
||||
|
||||
Reference in New Issue
Block a user