github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/api/handler/http/http.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/handler/http/http.go
    16  
    17  // Package http is a http reverse proxy handler
    18  package http
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  	"math/rand"
    24  	"net/http"
    25  	"net/http/httputil"
    26  	"net/url"
    27  
    28  	"github.com/tickoalcantara12/micro/v3/service/api"
    29  	"github.com/tickoalcantara12/micro/v3/service/api/handler"
    30  	"github.com/tickoalcantara12/micro/v3/service/registry"
    31  )
    32  
    33  const (
    34  	Handler = "http"
    35  )
    36  
    37  type httpHandler struct {
    38  	options handler.Options
    39  
    40  	// set with different initializer
    41  	s *api.Service
    42  }
    43  
    44  func (h *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    45  	service, err := h.getService(r)
    46  	if err != nil {
    47  		w.WriteHeader(500)
    48  		return
    49  	}
    50  
    51  	if len(service) == 0 {
    52  		w.WriteHeader(404)
    53  		return
    54  	}
    55  
    56  	rp, err := url.Parse(service)
    57  	if err != nil {
    58  		w.WriteHeader(500)
    59  		return
    60  	}
    61  
    62  	httputil.NewSingleHostReverseProxy(rp).ServeHTTP(w, r)
    63  }
    64  
    65  // getService returns the service for this request from the selector
    66  func (h *httpHandler) getService(r *http.Request) (string, error) {
    67  	var service *api.Service
    68  
    69  	if v, ok := r.Context().(handler.Context); ok {
    70  		// we were given the service
    71  		service = v.Service()
    72  	} else if h.options.Router != nil {
    73  		// try get service from router
    74  		s, err := h.options.Router.Route(r)
    75  		if err != nil {
    76  			return "", err
    77  		}
    78  		service = s
    79  	} else {
    80  		// we have no way of routing the request
    81  		return "", errors.New("no route found")
    82  	}
    83  
    84  	// get the nodes for this service
    85  	var nodes []*registry.Node
    86  	for _, srv := range service.Services {
    87  		nodes = append(nodes, srv.Nodes...)
    88  	}
    89  
    90  	// select a random node
    91  	if len(nodes) == 0 {
    92  		return "", errors.New("no route found")
    93  	}
    94  	node := nodes[rand.Int()%len(nodes)]
    95  
    96  	return fmt.Sprintf("http://%s", node.Address), nil
    97  }
    98  
    99  func (h *httpHandler) String() string {
   100  	return "http"
   101  }
   102  
   103  // NewHandler returns a http proxy handler
   104  func NewHandler(opts ...handler.Option) handler.Handler {
   105  	options := handler.NewOptions(opts...)
   106  
   107  	return &httpHandler{
   108  		options: options,
   109  	}
   110  }