-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathghttp_server_domain.go
175 lines (154 loc) · 5.26 KB
/
ghttp_server_domain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// 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.
package ghttp
import (
"context"
"strings"
)
// Domain is used for route register for domains.
type Domain struct {
server *Server // Belonged server
domains map[string]struct{} // Support multiple domains.
}
// Domain creates and returns a domain object for management for one or more domains.
func (s *Server) Domain(domains string) *Domain {
d := &Domain{
server: s,
domains: make(map[string]struct{}),
}
for _, v := range strings.Split(domains, ",") {
d.domains[strings.TrimSpace(v)] = struct{}{}
}
return d
}
// BindHandler binds the handler for the specified pattern.
func (d *Domain) BindHandler(pattern string, handler interface{}) {
for domain := range d.domains {
d.server.BindHandler(patternBindDomain(pattern, domain), handler)
}
}
func (d *Domain) doBindHandler(ctx context.Context, in doBindHandlerInput) {
for domain := range d.domains {
d.server.doBindHandler(ctx, doBindHandlerInput{
Prefix: in.Prefix,
Pattern: patternBindDomain(in.Pattern, domain),
FuncInfo: in.FuncInfo,
Middleware: in.Middleware,
Source: in.Source,
})
}
}
// BindObject binds the object for the specified pattern.
func (d *Domain) BindObject(pattern string, obj interface{}, methods ...string) {
for domain := range d.domains {
d.server.BindObject(patternBindDomain(pattern, domain), obj, methods...)
}
}
func (d *Domain) doBindObject(ctx context.Context, in doBindObjectInput) {
for domain := range d.domains {
d.server.doBindObject(ctx, doBindObjectInput{
Prefix: in.Prefix,
Pattern: patternBindDomain(in.Pattern, domain),
Object: in.Object,
Method: in.Method,
Middleware: in.Middleware,
Source: in.Source,
})
}
}
// BindObjectMethod binds the method for the specified pattern.
func (d *Domain) BindObjectMethod(pattern string, obj interface{}, method string) {
for domain := range d.domains {
d.server.BindObjectMethod(patternBindDomain(pattern, domain), obj, method)
}
}
func (d *Domain) doBindObjectMethod(ctx context.Context, in doBindObjectMethodInput) {
for domain := range d.domains {
d.server.doBindObjectMethod(ctx, doBindObjectMethodInput{
Prefix: in.Prefix,
Pattern: patternBindDomain(in.Pattern, domain),
Object: in.Object,
Method: in.Method,
Middleware: in.Middleware,
Source: in.Source,
})
}
}
// BindObjectRest binds the RESTful API for the specified pattern.
func (d *Domain) BindObjectRest(pattern string, obj interface{}) {
for domain := range d.domains {
d.server.BindObjectRest(patternBindDomain(pattern, domain), obj)
}
}
func (d *Domain) doBindObjectRest(ctx context.Context, in doBindObjectInput) {
for domain := range d.domains {
d.server.doBindObjectRest(ctx, doBindObjectInput{
Prefix: in.Prefix,
Pattern: patternBindDomain(in.Pattern, domain),
Object: in.Object,
Method: in.Method,
Middleware: in.Middleware,
Source: in.Source,
})
}
}
// BindHookHandler binds the hook handler for the specified pattern.
func (d *Domain) BindHookHandler(pattern string, hook HookName, handler HandlerFunc) {
for domain := range d.domains {
d.server.BindHookHandler(patternBindDomain(pattern, domain), hook, handler)
}
}
func (d *Domain) doBindHookHandler(ctx context.Context, in doBindHookHandlerInput) {
for domain := range d.domains {
d.server.doBindHookHandler(ctx, doBindHookHandlerInput{
Prefix: in.Prefix,
Pattern: patternBindDomain(in.Pattern, domain),
HookName: in.HookName,
Handler: in.Handler,
Source: in.Source,
})
}
}
// BindHookHandlerByMap binds the hook handler for the specified pattern.
func (d *Domain) BindHookHandlerByMap(pattern string, hookMap map[HookName]HandlerFunc) {
for domain := range d.domains {
d.server.BindHookHandlerByMap(patternBindDomain(pattern, domain), hookMap)
}
}
// BindStatusHandler binds the status handler for the specified pattern.
func (d *Domain) BindStatusHandler(status int, handler HandlerFunc) {
for domain := range d.domains {
d.server.addStatusHandler(d.server.statusHandlerKey(status, domain), handler)
}
}
// BindStatusHandlerByMap binds the status handler for the specified pattern.
func (d *Domain) BindStatusHandlerByMap(handlerMap map[int]HandlerFunc) {
for k, v := range handlerMap {
d.BindStatusHandler(k, v)
}
}
// BindMiddleware binds the middleware for the specified pattern.
func (d *Domain) BindMiddleware(pattern string, handlers ...HandlerFunc) {
for domain := range d.domains {
d.server.BindMiddleware(patternBindDomain(pattern, domain), handlers...)
}
}
// BindMiddlewareDefault binds the default middleware for the specified pattern.
func (d *Domain) BindMiddlewareDefault(handlers ...HandlerFunc) {
for domain := range d.domains {
d.server.BindMiddleware(patternBindDomain(defaultMiddlewarePattern, domain), handlers...)
}
}
// Use adds middleware to the domain.
func (d *Domain) Use(handlers ...HandlerFunc) {
d.BindMiddlewareDefault(handlers...)
}
func patternBindDomain(pattern, domain string) string {
if domain != "" {
return pattern + "@" + domain
}
return pattern
}