2017-12-11 17:16:59 +08:00
|
|
|
package ghttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"gitee.com/johng/gf/g/container/gmap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 域名管理器对象
|
|
|
|
type Domain struct {
|
|
|
|
s *Server // 所属Server
|
|
|
|
m map[string]bool // 多域名
|
|
|
|
}
|
|
|
|
|
|
|
|
// 域名对象表,用以存储和检索域名(支持多域名)与域名对象之间的关联关系
|
2017-12-13 16:45:00 +08:00
|
|
|
var domains = gmap.NewStringInterfaceMap()
|
2017-12-11 17:16:59 +08:00
|
|
|
|
|
|
|
// 生成一个域名对象
|
|
|
|
func (s *Server) Domain(domain string) *Domain {
|
|
|
|
if r := domains.Get(domain); r != nil {
|
|
|
|
return r.(*Domain)
|
|
|
|
}
|
|
|
|
d := &Domain{
|
|
|
|
s : s,
|
|
|
|
m : make(map[string]bool),
|
|
|
|
}
|
|
|
|
result := strings.Split(domain, ",")
|
|
|
|
for _, v := range result {
|
|
|
|
d.m[strings.TrimSpace(v)] = true
|
|
|
|
}
|
|
|
|
domains.Set(domain, d)
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2017-12-12 17:25:50 +08:00
|
|
|
// 绑定方法
|
2017-12-26 10:13:49 +08:00
|
|
|
func (d *Domain) BindControllerMethod(pattern string, c Controller, method string) error {
|
|
|
|
return d.s.BindControllerMethod(pattern, c, method)
|
2017-12-11 17:16:59 +08:00
|
|
|
}
|
|
|
|
|
2017-12-12 17:25:50 +08:00
|
|
|
// 绑定控制器
|
|
|
|
func (d *Domain) BindController(uri string, c Controller) error {
|
2017-12-11 17:16:59 +08:00
|
|
|
for domain, _ := range d.m {
|
2017-12-12 17:25:50 +08:00
|
|
|
if err := d.s.BindController(uri + "@" + domain, c); err != nil {
|
2017-12-11 17:16:59 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|