Subdomains
Iris has the simplest known form for subdomains registration to a single application. Of course you can always use nginx or caddy for management 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 that it's before a particular subdomain or root domain, 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 this 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.".
1
Subdomain(subdomain string, middleware ...Handler) Party
Copied!
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.
1
WildcardSubdomain(middleware ...Handler) Party
Copied!
Example Code:
1
// [app := iris.New...]
2
admin := app.Subdomain("admin")
3
4
// admin.mydomain.com
5
admin.Get("/", func(ctx iris.Context) {
6
ctx.Writef("INDEX FROM admin.mydomain.com")
7
})
8
9
// admin.mydomain.com/hey
10
admin.Get("/hey", func(ctx iris.Context) {
11
ctx.Writef("HEY FROM admin.mydomain.com/hey")
12
})
13
14
// [other routes here...]
15
16
app.Listen("mydomain.com:80")
Copied!
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:
1
127.0.0.1 mydomain.com
2
127.0.0.1 admin.mydomain.com
Copied!
To prove that subdomains works like any other regular Party you can also register a subdomain using the alternative method below:
1
adminSubdomain:= app.Party("admin.")
2
// or
3
adminAnalayticsSubdomain := app.Party("admin.analytics.")
4
// or for a dynamic one:
5
anySubdomain := app.Party("*.")
Copied!
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, 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.
1
SubdomainRedirect(from, to Party) Party
Copied!
Usage
1
www := app.Subdomain("www")
2
app.SubdomainRedirect(app, www)
Copied!
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.
1
// Host returns the host part of the current url.
2
Host() string
3
// Subdomain returns the subdomain of this request, if any.
4
// Note that this is a fast method which does not cover all cases.
5
Subdomain() (subdomain string)
6
// IsWWW returns true if the current subdomain (if any) is www.
7
IsWWW() bool
8
// FullRqeuestURI returns the full URI,
9
// including the scheme, the host and the relative requested path/resource.
10
FullRequestURI() string
Copied!
Usage
1
func info(ctx iris.Context) {
2
method := ctx.Method()
3
subdomain := ctx.Subdomain()
4
path := ctx.Path()
5
6
ctx.Writef("\nInfo\n\n")
7
ctx.Writef("Method: %s\nSubdomain: %s\nPath: %s", method, subdomain, path)
8
}
Copied!
Last modified 1yr ago
Export as PDF
Copy link