1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-06-18 22:17:44 +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,
}
}

View File

@ -12,7 +12,7 @@ import (
"github.com/gobwas/httphead"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"go-micro.dev/v4/api"
"go-micro.dev/v4/api/router"
"go-micro.dev/v4/client"
raw "go-micro.dev/v4/codec/bytes"
"go-micro.dev/v4/logger"
@ -20,7 +20,7 @@ import (
)
// serveWebsocket will stream rpc back over websockets assuming json
func serveWebsocket(ctx context.Context, w http.ResponseWriter, r *http.Request, service *api.Service, c client.Client) {
func serveWebsocket(ctx context.Context, w http.ResponseWriter, r *http.Request, service *router.Route, c client.Client) {
var op ws.OpCode
ct := r.Header.Get("Content-Type")
@ -103,14 +103,14 @@ func serveWebsocket(ctx context.Context, w http.ResponseWriter, r *http.Request,
ct = "application/json"
}
req := c.NewRequest(
service.Name,
service.Service,
service.Endpoint.Name,
request,
client.WithContentType(ct),
client.StreamingRequest(),
)
so := selector.WithStrategy(strategy(service.Services))
so := selector.WithStrategy(strategy(service.Versions))
// create a new stream
stream, err := c.Stream(ctx, req, client.WithSelectOption(so))
if err != nil {
@ -219,13 +219,13 @@ func writeLoop(rw io.ReadWriter, stream client.Stream) {
}
}
func isStream(r *http.Request, srv *api.Service) bool {
func isStream(r *http.Request, srv *router.Route) bool {
// check if it's a web socket
if !isWebSocket(r) {
return false
}
// check if the endpoint supports streaming
for _, service := range srv.Services {
for _, service := range srv.Versions {
for _, ep := range service.Endpoints {
// skip if it doesn't match the name
if ep.Name != srv.Endpoint.Name {