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