Iris 具有已知最简单的注册子域名到单个应用程序的方式。当然你也可以在生成环境中使用 nginx 或者 caddy

    子域名被分为两类:静态动态/通配

    • 静态:你所知的子域名,例如:analytics.mydomain.com
    • 通配:翻译不通(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.comotheruser.mydomain.com,就像 username.github.io 这样。

    我们使用 iris.Party 或者 iris.ApplicationSubdomainWildcardSubdomain 方法注册子域名。

    Subdomain 方法返回的是一个新的 Party 对象,它负责为特定的子域名注册路由。

    与常规 Party 不同的是,如果子 party 调用它,域名将会被添加到路径的前面,而不是追加到路径后面。因此如果 app.Sundomain("admin").Subdomain("panel"),结果是:panel.admin

    1. Subdomain(subdomain string, middleware ...Handler) Party

    WildcardSubdomain 方法返回一个新的 Party,它负责注册路由到一个动态的,通配的子域名中。一个动态的子域名能处理多个子域名请求。服务器将会接受多个子域名(如果没有找到对应的静态子域名),它也会搜索和和执行这个 Party 的处理器。

    1. WildcardSubdomain(middleware ...Handler) Party

    示例代码:

    1. // [app := iris.New...]
    2. admin := app.Subdomain("admin")
    3. // admin.mydomain.com
    4. admin.Get("/", func(ctx iris.Context) {
    5. ctx.Writef("INDEX FROM admin.mydomain.com")
    6. })
    7. // admin.mydomain.com/hey
    8. admin.Get("/hey", func(ctx iris.Context) {
    9. ctx.Writef("HEY FROM admin.mydomain.com/hey")
    10. })
    11. // [other routes here...]
    12. app.Run(iris.Addr("mydomain.com:80"))

    对于本地开发系统,你要修改你的 hosts 文件,例如在windows操作系统中,打开 C:\Windows\System32\Drivers\etc\hosts 文件,然后追加:

    1. 127.0.0.1 mydomain.com
    2. 127.0.0.1 admin.mydomain.com

    为了证明子域名像其它正则 Party 一样工作,你也可以用下面这种另类的方法注册一个子域名:

    1. adminSubdomain:= app.Party("admin.")
    2. // or
    3. adminAnalayticsSubdomain := app.Party("admin.analytics.")
    4. // or for a dynamic one:
    5. anySubdomain := app.Party("*.")

    还有一个 iris.Application 方法,允许为子域名创建全局重定向规则。

    SubdomainRedirect 设置(当使用超过1次时添加)一个路由包装器,它可以使一个(子)域名在执行路由处理器之前尽可能快地重定向(永久重定向)到另一个子域名或根域名。

    它接收2个参数,它们是 fromto/target 的位置, from 也可以是一个通配的子域名(app.WildcardSubdomain()),to 不允许是通配的。当 to 不是根域名时 from 可以是跟域名,反正亦然。

    1. SubdomainRedirect(from, to Party) Party

    使用:

    1. www := app.Subdomain("www")
    2. app.SubdomainRedirect(app, www)

    上面的所有 htt(s)://mydomain.com/%anypath% 将会重定向到 https(s)://www.mydomain.com/%anypath%

    当你使用子域名时,Context提供了四个对你很有帮助的主要方法。

    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

    使用:

    1. func info(ctx iris.Context) {
    2. method := ctx.Method()
    3. subdomain := ctx.Subdomain()
    4. path := ctx.Path()
    5. ctx.Writef("\nInfo\n\n")
    6. ctx.Writef("Method: %s\nSubdomain: %s\nPath: %s", method, subdomain, path)
    7. }