1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-06-12 22:07:47 +02:00

api package (#2522)

* api gateway

* add comment
This commit is contained in:
Asim Aslam
2022-07-02 12:11:59 +01:00
committed by GitHub
parent 28298a30e4
commit b6b866c0b2
13 changed files with 320 additions and 138 deletions

View File

@ -11,9 +11,9 @@ import (
jsonpatch "github.com/evanphx/json-patch/v5"
"github.com/oxtoacart/bpool"
"go-micro.dev/v4/api"
"go-micro.dev/v4/api/handler"
"go-micro.dev/v4/api/internal/proto"
"go-micro.dev/v4/api/router"
"go-micro.dev/v4/client"
"go-micro.dev/v4/codec"
"go-micro.dev/v4/codec/jsonrpc"
@ -54,7 +54,6 @@ var (
type rpcHandler struct {
opts handler.Options
s *api.Service
}
type buffer struct {
@ -82,12 +81,9 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, bsize)
defer r.Body.Close()
var service *api.Service
var service *router.Route
if h.s != nil {
// we were given the service
service = h.s
} else if h.opts.Router != nil {
if h.opts.Router != nil {
// try get service from router
s, err := h.opts.Router.Route(r)
if err != nil {
@ -142,7 +138,7 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
// create strategy
so := selector.WithStrategy(strategy(service.Services))
so := selector.WithStrategy(strategy(service.Versions))
// walk the standard call path
// get payload
@ -167,7 +163,7 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
response := &proto.Message{}
req := c.NewRequest(
service.Name,
service.Service,
service.Endpoint.Name,
request,
client.WithContentType(ct),
@ -203,7 +199,7 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var response json.RawMessage
req := c.NewRequest(
service.Name,
service.Service,
service.Endpoint.Name,
&request,
client.WithContentType(ct),
@ -512,11 +508,3 @@ func NewHandler(opts ...handler.Option) handler.Handler {
opts: options,
}
}
func WithService(s *api.Service, opts ...handler.Option) handler.Handler {
options := handler.NewOptions(opts...)
return &rpcHandler{
opts: options,
s: s,
}
}