2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-31 18:19:58 +08:00
|
|
|
|
2017-12-11 17:16:59 +08:00
|
|
|
package ghttp
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"strings"
|
2017-12-11 17:16:59 +08:00
|
|
|
)
|
|
|
|
|
2020-05-07 23:05:33 +08:00
|
|
|
// Domain is used for route register for domains.
|
2017-12-11 17:16:59 +08:00
|
|
|
type Domain struct {
|
2020-05-07 23:05:33 +08:00
|
|
|
server *Server // Belonged server
|
|
|
|
domains map[string]struct{} // Support multiple domains.
|
2017-12-11 17:16:59 +08:00
|
|
|
}
|
|
|
|
|
2020-05-07 23:05:33 +08:00
|
|
|
// Domain creates and returns a domain object for management for one or more domains.
|
2018-08-01 21:08:46 +08:00
|
|
|
func (s *Server) Domain(domains string) *Domain {
|
2019-06-19 09:06:52 +08:00
|
|
|
d := &Domain{
|
2020-03-17 14:48:52 +08:00
|
|
|
server: s,
|
|
|
|
domains: make(map[string]struct{}),
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
for _, v := range strings.Split(domains, ",") {
|
2020-03-17 14:48:52 +08:00
|
|
|
d.domains[strings.TrimSpace(v)] = struct{}{}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
return d
|
2017-12-11 17:16:59 +08:00
|
|
|
}
|
|
|
|
|
2019-03-06 15:21:00 +08:00
|
|
|
func (d *Domain) BindHandler(pattern string, handler HandlerFunc) {
|
2020-03-17 14:48:52 +08:00
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.BindHandler(pattern+"@"+domain, handler)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2017-12-30 18:35:24 +08:00
|
|
|
}
|
|
|
|
|
2020-03-17 14:48:52 +08:00
|
|
|
func (d *Domain) doBindHandler(
|
|
|
|
pattern string, handler HandlerFunc,
|
|
|
|
middleware []HandlerFunc, source string,
|
|
|
|
) {
|
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.doBindHandler(pattern+"@"+domain, handler, middleware, source)
|
2019-12-04 10:03:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (d *Domain) BindObject(pattern string, obj interface{}, methods ...string) {
|
2020-03-17 14:48:52 +08:00
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.BindObject(pattern+"@"+domain, obj, methods...)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2017-12-30 18:35:24 +08:00
|
|
|
}
|
|
|
|
|
2020-03-17 14:48:52 +08:00
|
|
|
func (d *Domain) doBindObject(
|
|
|
|
pattern string, obj interface{}, methods string,
|
|
|
|
middleware []HandlerFunc, source string,
|
|
|
|
) {
|
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.doBindObject(pattern+"@"+domain, obj, methods, middleware, source)
|
2019-12-04 10:03:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 15:21:00 +08:00
|
|
|
func (d *Domain) BindObjectMethod(pattern string, obj interface{}, method string) {
|
2020-03-17 14:48:52 +08:00
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.BindObjectMethod(pattern+"@"+domain, obj, method)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2018-01-08 11:38:31 +08:00
|
|
|
}
|
|
|
|
|
2020-03-17 14:48:52 +08:00
|
|
|
func (d *Domain) doBindObjectMethod(
|
|
|
|
pattern string, obj interface{}, method string,
|
|
|
|
middleware []HandlerFunc, source string,
|
|
|
|
) {
|
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.doBindObjectMethod(pattern+"@"+domain, obj, method, middleware, source)
|
2019-12-04 10:03:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 15:21:00 +08:00
|
|
|
func (d *Domain) BindObjectRest(pattern string, obj interface{}) {
|
2020-03-17 14:48:52 +08:00
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.BindObjectRest(pattern+"@"+domain, obj)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2017-12-11 17:16:59 +08:00
|
|
|
}
|
|
|
|
|
2020-03-17 14:48:52 +08:00
|
|
|
func (d *Domain) doBindObjectRest(
|
|
|
|
pattern string, obj interface{},
|
|
|
|
middleware []HandlerFunc, source string,
|
|
|
|
) {
|
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.doBindObjectRest(pattern+"@"+domain, obj, middleware, source)
|
2019-12-04 10:03:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (d *Domain) BindController(pattern string, c Controller, methods ...string) {
|
2020-03-17 14:48:52 +08:00
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.BindController(pattern+"@"+domain, c, methods...)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2017-12-30 18:35:24 +08:00
|
|
|
}
|
|
|
|
|
2020-03-17 14:48:52 +08:00
|
|
|
func (d *Domain) doBindController(
|
|
|
|
pattern string, c Controller, methods string,
|
|
|
|
middleware []HandlerFunc, source string,
|
|
|
|
) {
|
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.doBindController(pattern+"@"+domain, c, methods, middleware, source)
|
2019-12-04 10:03:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 15:21:00 +08:00
|
|
|
func (d *Domain) BindControllerMethod(pattern string, c Controller, method string) {
|
2020-03-17 14:48:52 +08:00
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.BindControllerMethod(pattern+"@"+domain, c, method)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2017-12-30 18:35:24 +08:00
|
|
|
}
|
|
|
|
|
2020-03-17 14:48:52 +08:00
|
|
|
func (d *Domain) doBindControllerMethod(
|
|
|
|
pattern string, c Controller, method string,
|
|
|
|
middleware []HandlerFunc, source string,
|
|
|
|
) {
|
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.doBindControllerMethod(pattern+"@"+domain, c, method, middleware, source)
|
2019-12-04 10:03:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 15:21:00 +08:00
|
|
|
func (d *Domain) BindControllerRest(pattern string, c Controller) {
|
2020-03-17 14:48:52 +08:00
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.BindControllerRest(pattern+"@"+domain, c)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2017-12-30 18:35:24 +08:00
|
|
|
}
|
2018-03-13 17:57:41 +08:00
|
|
|
|
2020-03-17 14:48:52 +08:00
|
|
|
func (d *Domain) doBindControllerRest(
|
|
|
|
pattern string, c Controller,
|
|
|
|
middleware []HandlerFunc, source string,
|
|
|
|
) {
|
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.doBindControllerRest(pattern+"@"+domain, c, middleware, source)
|
2019-12-04 10:03:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (d *Domain) BindHookHandler(pattern string, hook string, handler HandlerFunc) {
|
2020-03-17 14:48:52 +08:00
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.BindHookHandler(pattern+"@"+domain, hook, handler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Domain) doBindHookHandler(pattern string, hook string, handler HandlerFunc, source string) {
|
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.doBindHookHandler(pattern+"@"+domain, hook, handler, source)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2018-04-08 23:47:40 +08:00
|
|
|
}
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (d *Domain) BindHookHandlerByMap(pattern string, hookmap map[string]HandlerFunc) {
|
2020-03-17 14:48:52 +08:00
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.BindHookHandlerByMap(pattern+"@"+domain, hookmap)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2018-04-09 17:55:46 +08:00
|
|
|
}
|
2018-05-04 14:35:20 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (d *Domain) BindStatusHandler(status int, handler HandlerFunc) {
|
2020-03-17 14:48:52 +08:00
|
|
|
for domain, _ := range d.domains {
|
2020-11-25 16:37:41 +08:00
|
|
|
d.server.addStatusHandler(d.server.statusHandlerKey(status, domain), handler)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2018-05-04 14:35:20 +08:00
|
|
|
}
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (d *Domain) BindStatusHandlerByMap(handlerMap map[int]HandlerFunc) {
|
|
|
|
for k, v := range handlerMap {
|
|
|
|
d.BindStatusHandler(k, v)
|
|
|
|
}
|
|
|
|
}
|
2019-08-06 20:40:04 +08:00
|
|
|
|
|
|
|
func (d *Domain) BindMiddleware(pattern string, handlers ...HandlerFunc) {
|
2020-03-17 14:48:52 +08:00
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.BindMiddleware(pattern+"@"+domain, handlers...)
|
2019-08-06 20:40:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 23:05:05 +08:00
|
|
|
func (d *Domain) BindMiddlewareDefault(handlers ...HandlerFunc) {
|
2020-03-17 14:48:52 +08:00
|
|
|
for domain, _ := range d.domains {
|
|
|
|
d.server.BindMiddleware(gDEFAULT_MIDDLEWARE_PATTERN+"@"+domain, handlers...)
|
2019-08-06 20:40:04 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-28 23:00:05 +08:00
|
|
|
|
|
|
|
func (d *Domain) Use(handlers ...HandlerFunc) {
|
|
|
|
d.BindMiddlewareDefault(handlers...)
|
|
|
|
}
|