gf/net/ghttp/ghttp_server_domain.go

169 lines
4.9 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). 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,
// You can obtain one at https://github.com/gogf/gf.
2017-12-31 18:19:58 +08:00
package ghttp
import (
2021-09-27 21:27:24 +08:00
"context"
2019-06-19 09:06:52 +08:00
"strings"
)
2020-05-07 23:05:33 +08:00
// Domain is used for route register for domains.
type Domain struct {
2020-05-07 23:05:33 +08:00
server *Server // Belonged server
domains map[string]struct{} // Support multiple domains.
}
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{
server: s,
domains: make(map[string]struct{}),
2019-06-19 09:06:52 +08:00
}
for _, v := range strings.Split(domains, ",") {
d.domains[strings.TrimSpace(v)] = struct{}{}
2019-06-19 09:06:52 +08:00
}
return d
}
2022-03-19 17:58:21 +08:00
// BindHandler binds the handler for the specified pattern.
func (d *Domain) BindHandler(pattern string, handler interface{}) {
2022-03-19 17:58:21 +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
}
2021-11-07 21:31:33 +08:00
func (d *Domain) doBindHandler(ctx context.Context, in doBindHandlerInput) {
2022-03-19 17:58:21 +08:00
for domain := range d.domains {
2021-11-07 21:31:33 +08:00
d.server.doBindHandler(ctx, doBindHandlerInput{
Prefix: in.Prefix,
Pattern: in.Pattern + "@" + domain,
FuncInfo: in.FuncInfo,
Middleware: in.Middleware,
Source: in.Source,
})
}
}
2022-03-19 17:58:21 +08:00
// BindObject binds the object for the specified pattern.
2019-06-19 09:06:52 +08:00
func (d *Domain) BindObject(pattern string, obj interface{}, methods ...string) {
2022-03-19 17:58:21 +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
}
2021-11-07 21:31:33 +08:00
func (d *Domain) doBindObject(ctx context.Context, in doBindObjectInput) {
2022-03-19 17:58:21 +08:00
for domain := range d.domains {
2021-11-07 21:31:33 +08:00
d.server.doBindObject(ctx, doBindObjectInput{
Prefix: in.Prefix,
Pattern: in.Pattern + "@" + domain,
Object: in.Object,
Method: in.Method,
Middleware: in.Middleware,
Source: in.Source,
})
}
}
2022-03-19 17:58:21 +08:00
// BindObjectMethod binds the method for the specified pattern.
func (d *Domain) BindObjectMethod(pattern string, obj interface{}, method string) {
2022-03-19 17:58:21 +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
}
2021-11-07 21:31:33 +08:00
func (d *Domain) doBindObjectMethod(ctx context.Context, in doBindObjectMethodInput) {
2022-03-19 17:58:21 +08:00
for domain := range d.domains {
2021-11-07 21:31:33 +08:00
d.server.doBindObjectMethod(ctx, doBindObjectMethodInput{
Prefix: in.Prefix,
Pattern: in.Pattern + "@" + domain,
Object: in.Object,
Method: in.Method,
Middleware: in.Middleware,
Source: in.Source,
})
}
}
2022-03-19 17:58:21 +08:00
// BindObjectRest binds the RESTful API for the specified pattern.
func (d *Domain) BindObjectRest(pattern string, obj interface{}) {
2022-03-19 17:58:21 +08:00
for domain := range d.domains {
d.server.BindObjectRest(pattern+"@"+domain, obj)
2019-06-19 09:06:52 +08:00
}
}
2021-11-07 21:31:33 +08:00
func (d *Domain) doBindObjectRest(ctx context.Context, in doBindObjectInput) {
2022-03-19 17:58:21 +08:00
for domain := range d.domains {
2021-11-07 21:31:33 +08:00
d.server.doBindObjectRest(ctx, doBindObjectInput{
Prefix: in.Prefix,
Pattern: in.Pattern + "@" + domain,
Object: in.Object,
Method: in.Method,
Middleware: in.Middleware,
Source: in.Source,
})
}
}
2022-03-19 17:58:21 +08:00
// BindHookHandler binds the hook handler for the specified pattern.
2019-06-19 09:06:52 +08:00
func (d *Domain) BindHookHandler(pattern string, hook string, handler HandlerFunc) {
2022-03-19 17:58:21 +08:00
for domain := range d.domains {
d.server.BindHookHandler(pattern+"@"+domain, hook, handler)
}
}
2021-11-07 21:31:33 +08:00
func (d *Domain) doBindHookHandler(ctx context.Context, in doBindHookHandlerInput) {
2022-03-19 17:58:21 +08:00
for domain := range d.domains {
2021-11-07 21:31:33 +08:00
d.server.doBindHookHandler(ctx, doBindHookHandlerInput{
Prefix: in.Prefix,
Pattern: in.Pattern + "@" + domain,
HookName: in.HookName,
Handler: in.Handler,
Source: in.Source,
})
2019-06-19 09:06:52 +08:00
}
}
2022-03-19 17:58:21 +08:00
// BindHookHandlerByMap binds the hook handler for the specified pattern.
func (d *Domain) BindHookHandlerByMap(pattern string, hookMap map[string]HandlerFunc) {
for domain := range d.domains {
d.server.BindHookHandlerByMap(pattern+"@"+domain, hookMap)
2019-06-19 09:06:52 +08:00
}
}
2022-03-19 17:58:21 +08:00
// BindStatusHandler binds the status handler for the specified pattern.
2019-06-19 09:06:52 +08:00
func (d *Domain) BindStatusHandler(status int, handler HandlerFunc) {
2022-03-19 17:58:21 +08:00
for domain := range d.domains {
d.server.addStatusHandler(d.server.statusHandlerKey(status, domain), handler)
2019-06-19 09:06:52 +08:00
}
}
2022-03-19 17:58:21 +08:00
// BindStatusHandlerByMap binds the status handler for the specified pattern.
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)
}
}
2022-03-19 17:58:21 +08:00
// BindMiddleware binds the middleware for the specified pattern.
func (d *Domain) BindMiddleware(pattern string, handlers ...HandlerFunc) {
2022-03-19 17:58:21 +08:00
for domain := range d.domains {
d.server.BindMiddleware(pattern+"@"+domain, handlers...)
}
}
2022-03-19 17:58:21 +08:00
// BindMiddlewareDefault binds the default middleware for the specified pattern.
func (d *Domain) BindMiddlewareDefault(handlers ...HandlerFunc) {
2022-03-19 17:58:21 +08:00
for domain := range d.domains {
d.server.BindMiddleware(defaultMiddlewarePattern+"@"+domain, handlers...)
}
}
2022-03-19 17:58:21 +08:00
// Use adds middleware to the domain.
func (d *Domain) Use(handlers ...HandlerFunc) {
d.BindMiddlewareDefault(handlers...)
}