-
Notifications
You must be signed in to change notification settings - Fork 1
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 #73 from RealFax/dev
feat. better README, grpc basic auth support
- Loading branch information
Showing
14 changed files
with
241 additions
and
28 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
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 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 |
---|---|---|
|
@@ -4,7 +4,7 @@ import "strings" | |
|
||
const ( | ||
Major = "0" | ||
Minor = "7" | ||
Minor = "8" | ||
Patch = "1" | ||
) | ||
|
||
|
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 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,34 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"github.com/RealFax/RedQueen/pkg/client" | ||
"github.com/RealFax/RedQueen/pkg/grpcutil" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
"log" | ||
) | ||
|
||
func main() { | ||
// create basic-auth client | ||
authBroker := grpcutil.NewBasicAuthClient("admin", "123456") | ||
|
||
c, err := client.New(context.Background(), []string{ | ||
"127.0.0.1:3230", | ||
"127.0.0.1:4230", | ||
"127.0.0.1:5230", | ||
}, | ||
grpc.WithTransportCredentials(insecure.NewCredentials()), | ||
// setup auth client interceptor | ||
grpc.WithUnaryInterceptor(authBroker.Unary), | ||
grpc.WithStreamInterceptor(authBroker.Stream), | ||
) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer c.Close() | ||
|
||
if _, err = c.Get(context.Background(), []byte("Key1"), nil); err != nil { | ||
log.Fatal("client get error:", err) | ||
} | ||
} |
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,42 @@ | ||
package grpcutil | ||
|
||
import ( | ||
"context" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
type BasicAuthClient struct { | ||
authKey string | ||
} | ||
|
||
func (c BasicAuthClient) ctxWrap(ctx context.Context) context.Context { | ||
return metadata.NewOutgoingContext(ctx, metadata.MD{ | ||
MetadataAuthorization: []string{c.authKey}, | ||
}) | ||
} | ||
|
||
func (c BasicAuthClient) Unary( | ||
ctx context.Context, | ||
method string, req, reply any, | ||
cc *grpc.ClientConn, | ||
invoker grpc.UnaryInvoker, | ||
opts ...grpc.CallOption, | ||
) error { | ||
return invoker(c.ctxWrap(ctx), method, req, reply, cc, opts...) | ||
} | ||
|
||
func (c BasicAuthClient) Stream( | ||
ctx context.Context, | ||
desc *grpc.StreamDesc, | ||
cc *grpc.ClientConn, | ||
method string, | ||
streamer grpc.Streamer, | ||
opts ...grpc.CallOption, | ||
) (grpc.ClientStream, error) { | ||
return streamer(c.ctxWrap(ctx), desc, cc, method, opts...) | ||
} | ||
|
||
func NewBasicAuthClient(username, password string) *BasicAuthClient { | ||
return &BasicAuthClient{authKey: BuildAuthorization(username, password)} | ||
} |
Oops, something went wrong.