子域名
子域名可分为: 静态的子域名和动态的子域名。
- 静态子域名:如
bbs.coolwp.org
- 动态子域名:如
bbs.user1.blog.sohu.com
Iris 拥有最简单的子域名形式,简单的如 Party 。
静态子域名
package main
import (
"github.com/kataras/iris"
)
func main() {
api := iris.New()
// first the subdomains.
admin := api.Party("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")
})
// admin.mydomain.com/hey2
admin.Get("/hey2", func(ctx *iris.Context) {
ctx.Writef("HEY SECOND FROM admin.mydomain.com/hey")
})
}
// mydomain.com/
api.Get("/", func(ctx *iris.Context) {
ctx.Writef("INDEX FROM no-subdomain hey")
})
// mydomain.com/hey
api.Get("/hey", func(ctx *iris.Context) {
ctx.Writef("HEY FROM no-subdomain hey")
})
api.Listen("mydomain.com:80")
}
动态子域名/泛域名
// Package "main" is an example on how to catch dynamic/wildcard subdomains.
// On the first example (subdomains_1) we saw how to create routes for static subdomains,
// subdomains you know that you will have.
// Here we see an example on how to catch unknown subdomains, dynamic subdomains,
// like username.mydomain.com:8080.
package main
import "github.com/kataras/iris"
// first register a dynamic-wildcard subdomain to your server machine(dns/...) (check ./hosts if you use windows).
// run this file and try to redirect: http://username1.mydomain.com:8080/, http://username2.mydomain.com:8080/, http://username1.mydomain.com/something, http://username1.mydomain.com/something/sadsadsa
func main() {
/*
Keep note that you can use both of domains now (after 3.0.0-rc.1)
admin.mydomain.com, and for other the Party(*.) but this is not this example's purpose
admin := iris.Party("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")
})
// admin.mydomain.com/hey2
admin.Get("/hey2", func(ctx *iris.Context) {
ctx.Writef("HEY SECOND FROM admin.mydomain.com/hey")
})
}
*/
dynamicSubdomains := iris.Party("*.")
{
dynamicSubdomains.Get("/", dynamicSubdomainHandler)
dynamicSubdomains.Get("/something", dynamicSubdomainHandler)
dynamicSubdomains.Get("/something/:param1", dynamicSubdomainHandlerWithParam)
}
iris.Get("/", func(ctx *iris.Context) {
ctx.Writef("Hello from mydomain.com path: %s", ctx.Path())
})
iris.Get("/hello", func(ctx *iris.Context) {
ctx.Writef("Hello from mydomain.com path: %s", ctx.Path())
})
iris.Listen("mydomain.com:8080")
}
func dynamicSubdomainHandler(ctx *iris.Context) {
username := ctx.Subdomain()
ctx.Writef("Hello from dynamic subdomain path: %s, here you can handle the route for dynamic subdomains, handle the user: %s", ctx.Path(), username)
// if http://username4.mydomain.com:8080/ prints:
// Hello from dynamic subdomain path: /, here you can handle the route for dynamic subdomains, handle the user: username4
}
func dynamicSubdomainHandlerWithParam(ctx *iris.Context) {
username := ctx.Subdomain()
ctx.Writef("Hello from dynamic subdomain path: %s, here you can handle the route for dynamic subdomains, handle the user: %s", ctx.Path(), username)
ctx.Writef("THE PARAM1 is: %s", ctx.Param("param1"))
}
你还可为子域名设置无限个处理程序/中间件。
您注意到注释中的“subdomains_1”哪个,因此,这是因为本书几乎所有的代码都是可运行示例的快照。