github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/network/transport/http/options.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); 2 // you may not use this file except in compliance with the License. 3 // You may obtain a copy of the License at 4 // 5 // https://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, 9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 // See the License for the specific language governing permissions and 11 // limitations under the License. 12 // 13 // Original source: github.com/micro/go-micro/v3/network/transport/http/options.go 14 15 package http 16 17 import ( 18 "context" 19 "net" 20 "net/http" 21 22 "github.com/tickoalcantara12/micro/v3/service/network/transport" 23 ) 24 25 type netListener struct{} 26 27 // getNetListener Get net.Listener from ListenOptions 28 func getNetListener(o *transport.ListenOptions) net.Listener { 29 if o.Context == nil { 30 return nil 31 } 32 33 if l, ok := o.Context.Value(netListener{}).(net.Listener); ok && l != nil { 34 return l 35 } 36 37 return nil 38 } 39 40 // Handle registers the handler for the given pattern. 41 func Handle(pattern string, handler http.Handler) transport.Option { 42 return func(o *transport.Options) { 43 if o.Context == nil { 44 o.Context = context.Background() 45 } 46 handlers, ok := o.Context.Value("http_handlers").(map[string]http.Handler) 47 if !ok { 48 handlers = make(map[string]http.Handler) 49 } 50 handlers[pattern] = handler 51 o.Context = context.WithValue(o.Context, "http_handlers", handlers) 52 } 53 } 54 55 // NetListener Set net.Listener for httpTransport 56 func NetListener(customListener net.Listener) transport.ListenOption { 57 return func(o *transport.ListenOptions) { 58 if customListener == nil { 59 return 60 } 61 if o.Context == nil { 62 o.Context = context.TODO() 63 } 64 o.Context = context.WithValue(o.Context, netListener{}, customListener) 65 } 66 }