add the Context's helpers when working with subdomains

Gerasimos (Makis) Maropoulos 2019-07-03 01:41:28 +03:00
parent 806ad5e7aa
commit aa12f0a009
No known key found for this signature in database
GPG Key ID: F169457BBDA4ACF4

@ -87,3 +87,31 @@ app.SubdomainRedirect(app, www)
```
The above will redirect all http(s)://mydomain.com/%anypath% to http(s)://www.mydomain.com/%anypath%.
The `Context` offers four main methods when working with subdomains that may be helpful for you.
```go
// Host returns the host part of the current url.
Host() string
// Subdomain returns the subdomain of this request, if any.
// Note that this is a fast method which does not cover all cases.
Subdomain() (subdomain string)
// IsWWW returns true if the current subdomain (if any) is www.
IsWWW() bool
// FullRqeuestURI returns the full URI,
// including the scheme, the host and the relative requested path/resource.
FullRequestURI() string
```
**Usage**
```go
func info(ctx iris.Context) {
method := ctx.Method()
subdomain := ctx.Subdomain()
path := ctx.Path()
ctx.Writef("\nInfo\n\n")
ctx.Writef("Method: %s\nSubdomain: %s\nPath: %s", method, subdomain, path)
}
```