-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from w-h-a/broker-endpoints
feat: use broker node option for custom resolution
- Loading branch information
Showing
16 changed files
with
261 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package snssqs | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/sns" | ||
"github.com/aws/aws-sdk-go-v2/service/sqs" | ||
transport "github.com/aws/smithy-go/endpoints" | ||
) | ||
|
||
type snsResolver struct { | ||
nodes []string | ||
} | ||
|
||
func (r *snsResolver) ResolveEndpoint(ctx context.Context, params sns.EndpointParameters) (transport.Endpoint, error) { | ||
if len(r.nodes) == 0 { | ||
return sns.NewDefaultEndpointResolverV2().ResolveEndpoint(ctx, params) | ||
} | ||
|
||
u, err := url.Parse(r.nodes[0]) | ||
if err != nil { | ||
return transport.Endpoint{}, err | ||
} | ||
|
||
return transport.Endpoint{ | ||
URI: *u, | ||
}, nil | ||
} | ||
|
||
type sqsResolver struct { | ||
nodes []string | ||
} | ||
|
||
func (r *sqsResolver) ResolveEndpoint(ctx context.Context, params sqs.EndpointParameters) (transport.Endpoint, error) { | ||
if len(r.nodes) == 0 { | ||
return sqs.NewDefaultEndpointResolverV2().ResolveEndpoint(ctx, params) | ||
} | ||
|
||
u, err := url.Parse(r.nodes[0]) | ||
if err != nil { | ||
return transport.Endpoint{}, err | ||
} | ||
|
||
return transport.Endpoint{ | ||
URI: *u, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package grpcserver | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/w-h-a/pkg/server" | ||
) | ||
|
||
type grpcHandler struct { | ||
options server.HandlerOptions | ||
name string | ||
receiver reflect.Value | ||
methods map[string]*grpcMethod | ||
} | ||
|
||
type grpcMethod struct { | ||
name string | ||
value reflect.Value | ||
ctxType reflect.Type | ||
reqType reflect.Type | ||
rspType reflect.Type | ||
stream bool | ||
} | ||
|
||
func (c *grpcHandler) Options() server.HandlerOptions { | ||
return c.options | ||
} | ||
|
||
func (c *grpcHandler) Name() string { | ||
return c.name | ||
} | ||
|
||
func (c *grpcHandler) String() string { | ||
return "grpc" | ||
} | ||
|
||
func NewHandler(handler interface{}, opts ...server.HandlerOption) server.Handler { | ||
// TODO: handler options | ||
options := server.HandlerOptions{} | ||
|
||
methods := map[string]*grpcMethod{} | ||
|
||
// used to get method data | ||
typeOfHandler := reflect.TypeOf(handler) | ||
|
||
for i := 0; i < typeOfHandler.NumMethod(); i++ { | ||
m := typeOfHandler.Method(i) | ||
|
||
method := &grpcMethod{ | ||
name: m.Name, | ||
value: m.Func, | ||
} | ||
|
||
// TODO: make this better/safer | ||
switch m.Type.NumIn() { | ||
case 3: | ||
method.ctxType = m.Type.In(1) | ||
method.rspType = m.Type.In(2) | ||
method.stream = true | ||
case 4: | ||
method.ctxType = m.Type.In(1) | ||
method.reqType = m.Type.In(2) | ||
method.rspType = m.Type.In(3) | ||
} | ||
|
||
methods[m.Name] = method | ||
} | ||
|
||
// keep the value to use as a receiver in function invocation | ||
valueOfHandler := reflect.ValueOf(handler) | ||
|
||
// get the name of the handler struct | ||
nameOfHandler := reflect.Indirect(valueOfHandler).Type().Name() | ||
|
||
c := &grpcHandler{ | ||
options: options, | ||
name: nameOfHandler, | ||
receiver: valueOfHandler, | ||
methods: methods, | ||
} | ||
|
||
return c | ||
} |
Oops, something went wrong.