From 3e11e9e37bdef10cd54628b1c146fea42a795f58 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Wed, 3 Jul 2019 01:35:29 +0300 Subject: [PATCH] add a simple showcase of subdomain registration on the wiki routing subdomains section --- Routing-subdomains.md | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/Routing-subdomains.md b/Routing-subdomains.md index 162ef1c..2fc2424 100644 --- a/Routing-subdomains.md +++ b/Routing-subdomains.md @@ -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) ```