github.com/annwntech/go-micro/v2@v2.9.5/api/resolver/path/path.go (about)

     1  // Package path resolves using http path
     2  package path
     3  
     4  import (
     5  	"net/http"
     6  	"strings"
     7  
     8  	"github.com/annwntech/go-micro/v2/api/resolver"
     9  )
    10  
    11  type Resolver struct {
    12  	opts resolver.Options
    13  }
    14  
    15  func (r *Resolver) Resolve(req *http.Request, opts ...resolver.ResolveOption) (*resolver.Endpoint, error) {
    16  	// parse options
    17  	options := resolver.NewResolveOptions(opts...)
    18  
    19  	if req.URL.Path == "/" {
    20  		return nil, resolver.ErrNotFound
    21  	}
    22  
    23  	parts := strings.Split(req.URL.Path[1:], "/")
    24  	ns := r.opts.Namespace(req)
    25  
    26  	return &resolver.Endpoint{
    27  		Name:   ns + "." + parts[0],
    28  		Host:   req.Host,
    29  		Method: req.Method,
    30  		Path:   req.URL.Path,
    31  		Domain: options.Domain,
    32  	}, nil
    33  }
    34  
    35  func (r *Resolver) String() string {
    36  	return "path"
    37  }
    38  
    39  func NewResolver(opts ...resolver.Option) resolver.Resolver {
    40  	return &Resolver{opts: resolver.NewOptions(opts...)}
    41  }