feat: added API gateway
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package grpc_client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
pb "datarush/pkg/api/auth"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type AuthClient struct {
|
||||
client pb.AuthServiceClient
|
||||
conn *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewAuthClient(ctx context.Context, address string, factory *ClientFactory) (*AuthClient, error) {
|
||||
conn, err := factory.GetConnection(ctx, address)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create auth client: %w", err)
|
||||
}
|
||||
|
||||
return &AuthClient{
|
||||
client: pb.NewAuthServiceClient(conn),
|
||||
conn: conn,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *AuthClient) SignUp(ctx context.Context, email, username, password string) (string, error) {
|
||||
req := &pb.SignUpRequest{
|
||||
Email: email,
|
||||
Username: username,
|
||||
Password: password,
|
||||
}
|
||||
|
||||
resp, err := c.client.SignUp(ctx, req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("sign up failed: %w", err)
|
||||
}
|
||||
|
||||
return resp.Token, nil
|
||||
}
|
||||
|
||||
func (c *AuthClient) SignIn(ctx context.Context, email, password string) (string, error) {
|
||||
req := &pb.SignInRequest{
|
||||
Email: email,
|
||||
Password: password,
|
||||
}
|
||||
|
||||
resp, err := c.client.SignIn(ctx, req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("sign in failed: %w", err)
|
||||
}
|
||||
|
||||
return resp.Token, nil
|
||||
}
|
||||
|
||||
func (c *AuthClient) ValidateToken(ctx context.Context, token string) (string, error) {
|
||||
req := &pb.ValidateTokenRequest{
|
||||
Token: token,
|
||||
}
|
||||
|
||||
resp, err := c.client.ValidateToken(ctx, req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("token validation failed: %w", err)
|
||||
}
|
||||
|
||||
return resp.UserId, nil
|
||||
}
|
||||
Reference in New Issue
Block a user