From aa12f0a009d73d59e648919cc3829e2116db7959 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Wed, 3 Jul 2019 01:41:28 +0300 Subject: [PATCH] add the Context's helpers when working with subdomains --- Routing-subdomains.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Routing-subdomains.md b/Routing-subdomains.md index b7ba46a..c7efc6e 100644 --- a/Routing-subdomains.md +++ b/Routing-subdomains.md @@ -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) +} +```