add a simple showcase of subdomain registration on the wiki routing subdomains section

Gerasimos (Makis) Maropoulos 2019-07-03 01:35:29 +03:00
parent 327278518c
commit 3e11e9e37b
No known key found for this signature in database
GPG Key ID: F169457BBDA4ACF4

@ -1,4 +1,3 @@
Iris has the simplest known form for subdomains registration to a single application. Of course you can always use nginx or caddy for managment in production.
Subdomains are separated into two categories: **static** and **dynamic/wildcard**.
@ -27,6 +26,30 @@ can handle any subdomain requests. Server will accept any subdomain
WildcardSubdomain(middleware ...Handler) Party
```
Example Code:
```go
// [app := iris.New...]
admin := app.Subdomain("admin")
// admin.mydomain.com
admin.Get("/", func(ctx iris.Context) {
ctx.Writef("INDEX FROM admin.mydomain.com")
})
// admin.mydomain.com/hey
admin.Get("/hey", func(ctx iris.Context) {
ctx.Writef("HEY FROM admin.mydomain.com/hey")
})
```
For local development you'll have to edit your hosts, for example in windows operating system open the `C:\Windows\System32\Drivers\etc\hosts` file and append:
```txt
127.0.0.1 mydomain.com
127.0.0.1 admin.mydomain.com
```
To prove that subdomains works like any other regular `Party` you can also register a subdomain using the alternative method below:
```go
@ -34,10 +57,10 @@ adminSubdomain:= app.Party("admin.")
// or
adminAnalayticsSubdomain := app.Party("admin.analytics.")
// or for a dynamic one:
app.Party("*.")
anySubdomain := app.Party("*.")
```
There is an `iris.Application` method which allows to register a global redirection rule for subdomains as well.
There is also an `iris.Application` method which allows to register a global redirection rule for subdomains as well.
The `SubdomainRedirect` sets (or adds if used more than one time) a router wrapper which
redirects(StatusMovedPermanently) a (sub)domain to another subdomain or to the root domain as fast as possible,
@ -52,9 +75,9 @@ It receives two arguments, they are the from and to/target locations,
SubdomainRedirect(from, to Party) Party
```
**Usage**:
**Usage**
```go
// [app := iris.New...]
www := app.Subdomain("www")
app.SubdomainRedirect(app, www)
```