add subdomains section

Gerasimos (Makis) Maropoulos 2019-07-03 01:22:55 +03:00
parent 2be60fea82
commit 7917635eae
No known key found for this signature in database
GPG Key ID: F169457BBDA4ACF4
3 changed files with 65 additions and 1 deletions

@ -14,6 +14,7 @@ This wiki is the main source of documentation for **developers** working with (o
* [[Reverse Lookups|Routing-reverse-lookups]] * [[Reverse Lookups|Routing-reverse-lookups]]
* [[Middleware|Routing-middleware]] * [[Middleware|Routing-middleware]]
* [[Handle HTTP errors|Routing-error-handlers]] * [[Handle HTTP errors|Routing-error-handlers]]
* [[Subdomains|Routing-subdomains]]
* [[Wrap the Router|Routing-wrap-the-router]] * [[Wrap the Router|Routing-wrap-the-router]]
* [[Dependency Injection|Routing-dependency-injection]] * [[Dependency Injection|Routing-dependency-injection]]
* [[Cookies]] * [[Cookies]]

62
Routing-subdomains.md Normal file

@ -0,0 +1,62 @@
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**.
* Static : when you know the subdomain, i.e : `analytics.mydomain.com`
* Wildcard : when you don't know the subdomain but you know its form, i.e : `user_created.mydomain.com`, `otheruser.mydomain.com` like the `username.github.io`
We use the `Subdomain` and `WildcardSubdomain` methods of an `iris.Party` or `iris.Application` to register subdomains.
The `Subdomain` method returns a new `Party` which is responsible to register routes to
his specific "subdomain".
The only difference from a regular `Party` is that if called from a child party then the subdomain will be prepended to the path instead of appended.
So if `app.Subdomain("admin").Subdomain("panel")` then the result is: `"panel.admin."`.
```go
Subdomain(subdomain string, middleware ...Handler) Party
```
The `WildcardSubdomain` method returns a new `Party` which is responsible to register routes to
a dynamic, wildcard(ed) subdomain. A dynamic subdomain is a subdomain which
can handle any subdomain requests. Server will accept any subdomain
(if not static subdomain found) and it will search and execute the handlers of this `Party`.
```go
WildcardSubdomain(middleware ...Handler) Party
```
To prove that subdomains works like any other regular `Party` you can also register a subdomain using the alternative method below:
```go
adminSubdomain:= app.Party("admin.")
// or
adminAnalayticsSubdomain := app.Party("admin.analytics.")
// or for a dynamic one:
app.Party("*.")
```
There is 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,
before the execution of the route's handler(s).
It receives two arguments, they are the from and to/target locations,
'from' can be a wildcard subdomain as well (app.WildcardSubdomain())
'to' is not allowed to be a wildcard for obvious reasons,
'from' can be the root domain(app) when the 'to' is not the root domain and visa-versa.
```go
SubdomainRedirect(from, to Party) Party
```
**Usage**:
```go
// [app := iris.New...]
www := app.Subdomain("www")
app.SubdomainRedirect(app, www)
```
The above will redirect all http(s)://mydomain.com/%anypath% to http(s)://www.mydomain.com/%anypath%.

@ -8,7 +8,8 @@
* [[Path Parameter Types|Routing-path-parameter-types]] * [[Path Parameter Types|Routing-path-parameter-types]]
* [[Reverse Lookups|Routing-reverse-lookups]] * [[Reverse Lookups|Routing-reverse-lookups]]
* [[Middleware|Routing-middleware]] * [[Middleware|Routing-middleware]]
* [[Custom error handlers|Routing-error-handlers]] * [[Handle HTTP errors|Routing-error-handlers]]
* [[Subdomains|Routing-subdomains]]
* [[Wrap the Router|Routing-wrap-the-router]] * [[Wrap the Router|Routing-wrap-the-router]]
* [[Dependency Injection|Routing-dependency-injection]] * [[Dependency Injection|Routing-dependency-injection]]
* [[Cookies]] * [[Cookies]]