github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/api/resolver/grpc/grpc.go (about)

     1  // Copyright 2020 Asim Aslam
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // Original source: github.com/micro/go-micro/v3/api/resolver/grpc/grpc.go
    16  
    17  // Package grpc resolves a grpc service like /greeter.Say/Hello to greeter service
    18  package grpc
    19  
    20  import (
    21  	"errors"
    22  	"net/http"
    23  	"strings"
    24  
    25  	"github.com/tickoalcantara12/micro/v3/service/api/resolver"
    26  )
    27  
    28  type Resolver struct {
    29  	opts resolver.Options
    30  }
    31  
    32  func (r *Resolver) Resolve(req *http.Request, opts ...resolver.ResolveOption) (*resolver.Endpoint, error) {
    33  	// parse options
    34  	options := resolver.NewResolveOptions(opts...)
    35  
    36  	// /foo.Bar/Service
    37  	if req.URL.Path == "/" {
    38  		return nil, errors.New("unknown name")
    39  	}
    40  	// [foo.Bar, Service]
    41  	parts := strings.Split(req.URL.Path[1:], "/")
    42  	// [foo, Bar]
    43  	name := strings.Split(parts[0], ".")
    44  	// foo
    45  	return &resolver.Endpoint{
    46  		Name:   strings.Join(name[:len(name)-1], "."),
    47  		Host:   req.Host,
    48  		Method: req.Method,
    49  		Path:   req.URL.Path,
    50  		Domain: options.Domain,
    51  	}, nil
    52  }
    53  
    54  func (r *Resolver) String() string {
    55  	return "grpc"
    56  }
    57  
    58  func NewResolver(opts ...resolver.Option) resolver.Resolver {
    59  	return &Resolver{opts: resolver.NewOptions(opts...)}
    60  }