support multi-level subdomains redirect, see previous commit for the SubdomainRedirect fast solution

Former-commit-id: bc5749e46d1ae65f9d17063f3d8f2ea72510a9d8
This commit is contained in:
Gerasimos (Makis) Maropoulos 2018-01-20 14:54:04 +02:00
parent e176ff7b0c
commit 25918427bc
5 changed files with 12 additions and 5 deletions

View File

@ -10,7 +10,6 @@ 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)

View File

@ -1,4 +1,4 @@
127.0.0.1 mydomain.com
127.0.0.1 www.mydomain.com
127.0.0.1 mydomain.com
127.0.0.1 www.mydomain.com
# Windows: Drive:/Windows/system32/drivers/etc/hosts, on Linux: /etc/hosts

View File

@ -44,6 +44,11 @@ func newApp() *iris.Application {
// First argument is the 'from' and second is the 'to/target'.
app.SubdomainRedirect(app, www)
// SubdomainRedirect works for multi-level subdomains as well:
// subsub := www.Subdomain("subsub") // subsub.www.mydomain.com
// subsub.Get("/", func(ctx iris.Context) { ctx.Writef("subdomain is: " + ctx.Subdomain()) })
// app.SubdomainRedirect(subsub, 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:

View File

@ -129,7 +129,10 @@ func hasSubdomain(s string) bool {
// if not start with "/" then it should be something else,
// we don't assume anything else but subdomain.
slashIdx := strings.IndexByte(s, '/')
return slashIdx > 0 || s[0] == SubdomainPrefix[0] || (len(s) >= 2 && s[0:2] == SubdomainWildcardIndicator)
return slashIdx > 0 || // for route paths
s[0] == SubdomainPrefix[0] || // for route paths
(len(s) >= 2 && s[0:2] == SubdomainWildcardIndicator) || // for party rel path or route paths
(len(s) >= 2 && slashIdx != 0 && s[len(s)-1] == '.') // for party rel, i.e www., or subsub.www.
}
// splitSubdomainAndPath checks if the path has subdomain and if it's

View File

@ -705,7 +705,7 @@ var ErrServerClosed = http.ErrServerClosed
// `Listener`, `Server`, `Addr`, `TLS`, `AutoTLS` and `Raw`.
func (app *Application) Run(serve Runner, withOrWithout ...Configurator) error {
// first Build because it doesn't need anything from configuration,
// this give the user the chance to modify the router inside a configurator as well.
// this gives the user the chance to modify the router inside a configurator as well.
if err := app.Build(); err != nil {
return errors.PrintAndReturnErrors(err, app.logger.Errorf)
}