github.com/annwntech/go-micro/v2@v2.9.5/api/resolver/grpc/grpc.go (about) 1 // Package grpc resolves a grpc service like /greeter.Say/Hello to greeter service 2 package grpc 3 4 import ( 5 "errors" 6 "net/http" 7 "strings" 8 9 "github.com/annwntech/go-micro/v2/api/resolver" 10 ) 11 12 type Resolver struct { 13 opts resolver.Options 14 } 15 16 func (r *Resolver) Resolve(req *http.Request, opts ...resolver.ResolveOption) (*resolver.Endpoint, error) { 17 // parse options 18 options := resolver.NewResolveOptions(opts...) 19 20 // /foo.Bar/Service 21 if req.URL.Path == "/" { 22 return nil, errors.New("unknown name") 23 } 24 // [foo.Bar, Service] 25 parts := strings.Split(req.URL.Path[1:], "/") 26 // [foo, Bar] 27 name := strings.Split(parts[0], ".") 28 // foo 29 return &resolver.Endpoint{ 30 Name: strings.Join(name[:len(name)-1], "."), 31 Host: req.Host, 32 Method: req.Method, 33 Path: req.URL.Path, 34 Domain: options.Domain, 35 }, nil 36 } 37 38 func (r *Resolver) String() string { 39 return "grpc" 40 } 41 42 func NewResolver(opts ...resolver.Option) resolver.Resolver { 43 return &Resolver{opts: resolver.NewOptions(opts...)} 44 }