github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_server_domain.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  // 域名服务注册管理.
     7  
     8  package ghttp
     9  
    10  import (
    11  	"strings"
    12  )
    13  
    14  // 域名管理器对象
    15  type Domain struct {
    16  	s *Server         // 所属Server
    17  	m map[string]bool // 多域名
    18  }
    19  
    20  // 生成一个域名对象, 参数 domains 支持给定多个域名。
    21  func (s *Server) Domain(domains string) *Domain {
    22  	d := &Domain{
    23  		s: s,
    24  		m: make(map[string]bool),
    25  	}
    26  	for _, v := range strings.Split(domains, ",") {
    27  		d.m[strings.TrimSpace(v)] = true
    28  	}
    29  	return d
    30  }
    31  
    32  // 注意该方法是直接绑定方法的内存地址,执行的时候直接执行该方法,不会存在初始化新的控制器逻辑
    33  func (d *Domain) BindHandler(pattern string, handler HandlerFunc) {
    34  	for domain, _ := range d.m {
    35  		d.s.BindHandler(pattern+"@"+domain, handler)
    36  	}
    37  }
    38  
    39  // 执行对象方法
    40  func (d *Domain) BindObject(pattern string, obj interface{}, methods ...string) {
    41  	for domain, _ := range d.m {
    42  		d.s.BindObject(pattern+"@"+domain, obj, methods...)
    43  	}
    44  }
    45  
    46  // 执行对象方法注册,methods参数不区分大小写
    47  func (d *Domain) BindObjectMethod(pattern string, obj interface{}, method string) {
    48  	for domain, _ := range d.m {
    49  		d.s.BindObjectMethod(pattern+"@"+domain, obj, method)
    50  	}
    51  }
    52  
    53  // RESTful执行对象注册
    54  func (d *Domain) BindObjectRest(pattern string, obj interface{}) {
    55  	for domain, _ := range d.m {
    56  		d.s.BindObjectRest(pattern+"@"+domain, obj)
    57  	}
    58  }
    59  
    60  // 控制器注册
    61  func (d *Domain) BindController(pattern string, c Controller, methods ...string) {
    62  	for domain, _ := range d.m {
    63  		d.s.BindController(pattern+"@"+domain, c, methods...)
    64  	}
    65  }
    66  
    67  // 控制器方法注册,methods参数区分大小写
    68  func (d *Domain) BindControllerMethod(pattern string, c Controller, method string) {
    69  	for domain, _ := range d.m {
    70  		d.s.BindControllerMethod(pattern+"@"+domain, c, method)
    71  	}
    72  }
    73  
    74  // RESTful控制器注册
    75  func (d *Domain) BindControllerRest(pattern string, c Controller) {
    76  	for domain, _ := range d.m {
    77  		d.s.BindControllerRest(pattern+"@"+domain, c)
    78  	}
    79  }
    80  
    81  // 绑定指定的hook回调函数, hook参数的值由ghttp server设定,参数不区分大小写
    82  // 目前hook支持:Init/Shut
    83  func (d *Domain) BindHookHandler(pattern string, hook string, handler HandlerFunc) {
    84  	for domain, _ := range d.m {
    85  		d.s.BindHookHandler(pattern+"@"+domain, hook, handler)
    86  	}
    87  }
    88  
    89  // 通过map批量绑定回调函数
    90  func (d *Domain) BindHookHandlerByMap(pattern string, hookmap map[string]HandlerFunc) {
    91  	for domain, _ := range d.m {
    92  		d.s.BindHookHandlerByMap(pattern+"@"+domain, hookmap)
    93  	}
    94  }
    95  
    96  // 绑定指定的状态码回调函数
    97  func (d *Domain) BindStatusHandler(status int, handler HandlerFunc) {
    98  	for domain, _ := range d.m {
    99  		d.s.setStatusHandler(d.s.statusHandlerKey(status, domain), handler)
   100  	}
   101  }
   102  
   103  // 通过map批量绑定状态码回调函数
   104  func (d *Domain) BindStatusHandlerByMap(handlerMap map[int]HandlerFunc) {
   105  	for k, v := range handlerMap {
   106  		d.BindStatusHandler(k, v)
   107  	}
   108  }