refacto
This commit is contained in:
@@ -2,32 +2,48 @@ package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"git.coopgo.io/coopgo-apps/silvermobi/models"
|
||||
grpcproto "git.coopgo.io/coopgo-apps/silvermobi/servers/grpcapi/proto"
|
||||
"github.com/rs/zerolog/log"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s SilvermobiGRPCService) GetAccountInfo(ctx context.Context, req *grpcproto.AccountInfoRequest) (res *grpcproto.AccountInfoResponse, err error) {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
func (s SilvermobiGRPCService) GetAccountInfo(ctx context.Context,
|
||||
req *grpcproto.AccountInfoRequest) (res *grpcproto.AccountInfoResponse, err error) {
|
||||
|
||||
var (
|
||||
md metadata.MD
|
||||
account *models.Account
|
||||
authHeader []string
|
||||
ok bool
|
||||
)
|
||||
|
||||
if md, ok = metadata.FromIncomingContext(ctx); !ok {
|
||||
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
|
||||
}
|
||||
authHeader, ok := md["authorization"]
|
||||
|
||||
authHeader, ok = md["authorization"]
|
||||
|
||||
if !ok || len(authHeader) == 0 {
|
||||
return nil, status.Errorf(codes.Unauthenticated, "Missing authorization header")
|
||||
}
|
||||
|
||||
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
|
||||
|
||||
id := ExtractIdFromToken(tokenString)
|
||||
account, err := s.Handler.GetAccountInfos(context.Background(), id)
|
||||
|
||||
if account, err = s.Handler.GetAccountInfos(context.Background(), id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Info().
|
||||
Str("ID", account.ID).
|
||||
Msg("GetAccountInfo")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &grpcproto.AccountInfoResponse{
|
||||
FirstName: account.FirstName,
|
||||
LastName: account.LastName,
|
||||
|
||||
@@ -8,13 +8,15 @@ import (
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (s SilvermobiGRPCService) AuthLogin(ctx context.Context, req *grpcproto.AuthLoginRequest) (res *grpcproto.AuthLoginResponse, err error) {
|
||||
func (s SilvermobiGRPCService) AuthLogin(ctx context.Context,
|
||||
req *grpcproto.AuthLoginRequest) (res *grpcproto.AuthLoginResponse, err error) {
|
||||
var jwt string
|
||||
|
||||
log.Info().
|
||||
Str("username", req.Username).
|
||||
Msg("AuthLogin")
|
||||
|
||||
jwt, err := s.Handler.Login(ctx, req.Username, req.Password)
|
||||
if err != nil {
|
||||
if jwt, err = s.Handler.Login(ctx, req.Username, req.Password); err != nil {
|
||||
log.Error().
|
||||
Err(err).
|
||||
Str("username", req.Username).
|
||||
@@ -28,13 +30,16 @@ func (s SilvermobiGRPCService) AuthLogin(ctx context.Context, req *grpcproto.Aut
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s SilvermobiGRPCService) AuthRegister(ctx context.Context, req *grpcproto.AuthRegisterRequest) (res *grpcproto.AuthRegisterResponse, err error) {
|
||||
func (s SilvermobiGRPCService) AuthRegister(ctx context.Context,
|
||||
req *grpcproto.AuthRegisterRequest) (res *grpcproto.AuthRegisterResponse, err error) {
|
||||
var jwt string
|
||||
|
||||
log.Info().
|
||||
Str("username", req.Email).
|
||||
Msg("AuthRegister")
|
||||
|
||||
jwt, err := s.Handler.Register(ctx, req.Email, req.Password, req.Email, req.PhoneNumber, req.FirstName, req.LastName)
|
||||
if err != nil {
|
||||
if jwt, err = s.Handler.Register(ctx, req.Email, req.Password,
|
||||
req.Email, req.PhoneNumber, req.FirstName, req.LastName); err != nil {
|
||||
log.Error().Err(err).Msg("AuthRegister failed")
|
||||
return nil, errors.New("could not register user")
|
||||
}
|
||||
|
||||
@@ -2,44 +2,57 @@ package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
grpcproto "git.coopgo.io/coopgo-apps/silvermobi/servers/grpcapi/proto"
|
||||
"github.com/rs/zerolog/log"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s SilvermobiGRPCService) PutFirebaseToken(ctx context.Context, req *grpcproto.FirebaseTokenRequest) (resp *grpcproto.FirebaseTokenResponse, err error) {
|
||||
func (s SilvermobiGRPCService) PutFirebaseToken(ctx context.Context,
|
||||
req *grpcproto.FirebaseTokenRequest) (resp *grpcproto.FirebaseTokenResponse, err error) {
|
||||
|
||||
var (
|
||||
ok bool
|
||||
md metadata.MD
|
||||
authHeader []string
|
||||
)
|
||||
|
||||
if req.Token == "" || req.DevicePlatform == "" {
|
||||
return &grpcproto.FirebaseTokenResponse{
|
||||
Result: false,
|
||||
}, nil
|
||||
}
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
|
||||
if md, ok = metadata.FromIncomingContext(ctx); !ok {
|
||||
return &grpcproto.FirebaseTokenResponse{
|
||||
Result: false,
|
||||
}, status.Errorf(codes.Unauthenticated, "Missing metadata")
|
||||
}
|
||||
authHeader, ok := md["authorization"]
|
||||
|
||||
authHeader, ok = md["authorization"]
|
||||
|
||||
if !ok || len(authHeader) == 0 {
|
||||
return &grpcproto.FirebaseTokenResponse{
|
||||
Result: false,
|
||||
}, status.Errorf(codes.Unauthenticated, "Missing authorization header")
|
||||
}
|
||||
|
||||
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
|
||||
|
||||
id := ExtractIdFromToken(tokenString)
|
||||
log.Info().
|
||||
Str("User ID", id).
|
||||
Msg("PutFirebaseToken")
|
||||
err = s.Handler.PutFirebase(context.Background(), id, req.Token, req.DevicePlatform)
|
||||
if err != nil {
|
||||
|
||||
if err = s.Handler.PutFirebase(context.Background(), id, req.Token, req.DevicePlatform); err != nil {
|
||||
return &grpcproto.FirebaseTokenResponse{
|
||||
Result: false,
|
||||
}, status.Errorf(codes.Unknown, "Database error")
|
||||
}
|
||||
|
||||
return &grpcproto.FirebaseTokenResponse{
|
||||
Result: true,
|
||||
}, nil
|
||||
|
||||
@@ -2,26 +2,30 @@ package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
grpcproto "git.coopgo.io/coopgo-apps/silvermobi/servers/grpcapi/proto"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (s SilvermobiGRPCService) ForgetAccount(ctx context.Context, req *grpcproto.ForgetAccountRequest) (res *grpcproto.ForgetAccountResponse, err error) {
|
||||
func (s SilvermobiGRPCService) ForgetAccount(ctx context.Context,
|
||||
req *grpcproto.ForgetAccountRequest) (res *grpcproto.ForgetAccountResponse, err error) {
|
||||
|
||||
log.Info().
|
||||
Str("username", req.Username).
|
||||
Str("namespace", req.Namespace).
|
||||
Msg("ForgetAccount")
|
||||
response, access_code := s.Handler.ForgetAccount(ctx, req.Username, req.Namespace)
|
||||
|
||||
response, accessCode := s.Handler.ForgetAccount(ctx, req.Username, req.Namespace)
|
||||
|
||||
if response == true {
|
||||
return &grpcproto.ForgetAccountResponse{
|
||||
Response: true,
|
||||
AccessCode: access_code,
|
||||
}, nil
|
||||
} else {
|
||||
return &grpcproto.ForgetAccountResponse{
|
||||
Response: false,
|
||||
AccessCode: "",
|
||||
AccessCode: accessCode,
|
||||
}, nil
|
||||
}
|
||||
return &grpcproto.ForgetAccountResponse{
|
||||
Response: false,
|
||||
AccessCode: "",
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
@@ -2,41 +2,39 @@ package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
grpcproto "git.coopgo.io/coopgo-apps/silvermobi/servers/grpcapi/proto"
|
||||
"github.com/google/uuid"
|
||||
"github.com/paulmach/orb/geojson"
|
||||
"github.com/rs/zerolog/log"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"io"
|
||||
)
|
||||
|
||||
func (s SilvermobiGRPCService) GeoAutocomplete(ctx context.Context, in *grpcproto.GeoAutocompleteRequest) (resp *grpcproto.GeoAutocompleteResponse, err error) {
|
||||
func (s SilvermobiGRPCService) GeoAutocomplete(ctx context.Context,
|
||||
in *grpcproto.GeoAutocompleteRequest) (resp *grpcproto.GeoAutocompleteResponse, err error) {
|
||||
|
||||
var (
|
||||
results *geojson.FeatureCollection
|
||||
rawfc []byte
|
||||
)
|
||||
|
||||
log.Info().
|
||||
Str("text", in.Text).
|
||||
Msg("GeoAutocompleteRequest")
|
||||
requestid := uuid.NewString()
|
||||
log.Debug().Str("requestid", requestid).Msg("GRPC GeoAutocomplete start")
|
||||
|
||||
if err == io.EOF {
|
||||
log.Debug().Str("requestid", requestid).Msg("GRPC GeoAutocomplete EOF")
|
||||
requestID := uuid.NewString()
|
||||
|
||||
log.Debug().Str("requestID", requestID).Msg("GRPC GeoAutocomplete start")
|
||||
|
||||
if results, err = s.Handler.GeoAutocomplete(in.Text); err != nil {
|
||||
log.Error().Str("requestID", requestID).Err(err).Msg("GRPC GeoAutocomplete geocoding error")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Error().Str("requestid", requestid).Err(err).Msg("GRPC GeoAutocomplete other error")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
results, err := s.Handler.GeoAutocomplete(in.Text, in.Lat, in.Lon)
|
||||
if err != nil {
|
||||
log.Error().Str("requestid", requestid).Err(err).Msg("GRPC GeoAutocomplete geocoding error")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rawfc, err := results.MarshalJSON()
|
||||
if err != nil {
|
||||
log.Error().Str("requestid", requestid).Err(err).Msg("GRPC GeoAutocomplete protocol buffer conversion error")
|
||||
if rawfc, err = results.MarshalJSON(); err != nil {
|
||||
log.Error().Str("requestID", requestID).Err(err).Msg("GRPC GeoAutocomplete " +
|
||||
"protocol buffer conversion error")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -50,50 +48,58 @@ func (s SilvermobiGRPCService) GeoAutocomplete(ctx context.Context, in *grpcprot
|
||||
|
||||
}
|
||||
|
||||
func (s SilvermobiGRPCService) GeoRoute(ctx context.Context, req *grpcproto.GeoRouteRequest) (*grpcproto.GeoRouteResponse, error) {
|
||||
locations_raw, ok := req.Locations.(*grpcproto.GeoRouteRequest_LocationsRaw)
|
||||
func (s SilvermobiGRPCService) GeoRoute(ctx context.Context,
|
||||
req *grpcproto.GeoRouteRequest) (*grpcproto.GeoRouteResponse, error) {
|
||||
locationsRaw, ok := req.Locations.(*grpcproto.GeoRouteRequest_LocationsRaw)
|
||||
|
||||
if !ok {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "could not read departure")
|
||||
}
|
||||
|
||||
locations, err := geojson.UnmarshalFeatureCollection([]byte(locations_raw.LocationsRaw))
|
||||
locations, err := geojson.UnmarshalFeatureCollection([]byte(locationsRaw.LocationsRaw))
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
route, err := s.Handler.GeoRoute(*locations)
|
||||
if err != nil {
|
||||
if _, err = s.Handler.GeoRoute(*locations); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
route, _ := s.Handler.GeoRoute(*locations)
|
||||
|
||||
return &grpcproto.GeoRouteResponse{
|
||||
Polyline: route.Polyline.String(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s SilvermobiGRPCService) GeoRouteWithReturn(ctx context.Context, req *grpcproto.GeoRouteWithReturnRequest) (*grpcproto.GeoRouteWithReturnResponse, error) {
|
||||
locations_raw, ok := req.Locations.(*grpcproto.GeoRouteWithReturnRequest_LocationsRaw)
|
||||
func (s SilvermobiGRPCService) GeoRouteWithReturn(ctx context.Context,
|
||||
req *grpcproto.GeoRouteWithReturnRequest) (*grpcproto.GeoRouteWithReturnResponse, error) {
|
||||
|
||||
locationsRaw, ok := req.Locations.(*grpcproto.GeoRouteWithReturnRequest_LocationsRaw)
|
||||
|
||||
if !ok {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "could not read departure")
|
||||
}
|
||||
|
||||
locations, err := geojson.UnmarshalFeatureCollection([]byte(locations_raw.LocationsRaw))
|
||||
locations, err := geojson.UnmarshalFeatureCollection([]byte(locationsRaw.LocationsRaw))
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
route, err := s.Handler.GeoRoute(*locations)
|
||||
if err != nil {
|
||||
if _, err = s.Handler.GeoRoute(*locations); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return_route, err := s.Handler.GeoReturnRoute(*locations)
|
||||
if err != nil {
|
||||
route, _ := s.Handler.GeoRoute(*locations)
|
||||
|
||||
if _, err = s.Handler.GeoReturnRoute(*locations); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
returnRoute, _ := s.Handler.GeoReturnRoute(*locations)
|
||||
|
||||
return &grpcproto.GeoRouteWithReturnResponse{
|
||||
Polyline: route.Polyline.String(),
|
||||
ReturnPolyline: return_route.Polyline.String(),
|
||||
ReturnPolyline: returnRoute.Polyline.String(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -4,28 +4,38 @@ import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
grpcproto "git.coopgo.io/coopgo-apps/silvermobi/servers/grpcapi/proto"
|
||||
"github.com/rs/zerolog/log"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (s SilvermobiGRPCService) SetPhoneNumber(ctx context.Context, req *grpcproto.SetPhoneNumberRequest) (res *grpcproto.SetPhoneNumberResponse, err error) {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
func (s SilvermobiGRPCService) SetPhoneNumber(ctx context.Context,
|
||||
req *grpcproto.SetPhoneNumberRequest) (res *grpcproto.SetPhoneNumberResponse, err error) {
|
||||
|
||||
var (
|
||||
md metadata.MD
|
||||
authHeader []string
|
||||
ok bool
|
||||
)
|
||||
|
||||
if md, ok = metadata.FromIncomingContext(ctx); !ok {
|
||||
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
|
||||
}
|
||||
|
||||
authHeader, ok := md["authorization"]
|
||||
authHeader, ok = md["authorization"]
|
||||
if !ok || len(authHeader) == 0 {
|
||||
return nil, status.Errorf(codes.Unauthenticated, "Missing authorization header")
|
||||
}
|
||||
|
||||
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
|
||||
|
||||
id := ExtractIdFromToken(tokenString)
|
||||
|
||||
log.Info().
|
||||
Str("User ID", id).
|
||||
Msg("SetPhoneNumber")
|
||||
@@ -40,20 +50,33 @@ func (s SilvermobiGRPCService) SetPhoneNumber(ctx context.Context, req *grpcprot
|
||||
return &grpcproto.SetPhoneNumberResponse{Ok: true}, nil
|
||||
}
|
||||
|
||||
func (s SilvermobiGRPCService) VerifyPhoneNumber(ctx context.Context, req *grpcproto.VerifyPhoneNumberRequest) (res *grpcproto.VerifyPhoneNumberResponse, err error) {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
func (s SilvermobiGRPCService) VerifyPhoneNumber(ctx context.Context,
|
||||
req *grpcproto.VerifyPhoneNumberRequest) (res *grpcproto.VerifyPhoneNumberResponse, err error) {
|
||||
|
||||
var (
|
||||
md metadata.MD
|
||||
authHeader []string
|
||||
ok bool
|
||||
)
|
||||
|
||||
if md, ok = metadata.FromIncomingContext(ctx); !ok {
|
||||
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
|
||||
}
|
||||
authHeader, ok := md["authorization"]
|
||||
|
||||
authHeader, ok = md["authorization"]
|
||||
|
||||
if !ok || len(authHeader) == 0 {
|
||||
return nil, status.Errorf(codes.Unauthenticated, "Missing authorization header")
|
||||
return nil, status.Errorf(codes.Unauthenticated,
|
||||
"Missing authorization header")
|
||||
}
|
||||
|
||||
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
|
||||
|
||||
id := ExtractIdFromToken(tokenString)
|
||||
log.Info().
|
||||
Str("User ID", id).
|
||||
Msg("VerifyPhoneNumber")
|
||||
|
||||
if err = s.Handler.VerifyPhoneNumber(
|
||||
context.Background(),
|
||||
id,
|
||||
@@ -66,21 +89,31 @@ func (s SilvermobiGRPCService) VerifyPhoneNumber(ctx context.Context, req *grpcp
|
||||
return &grpcproto.VerifyPhoneNumberResponse{Ok: true}, nil
|
||||
}
|
||||
|
||||
func (s SilvermobiGRPCService) SetBirthDate(ctx context.Context, req *grpcproto.BirthDateRequest) (res *grpcproto.BirthDateResponse, err error) {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
func (s SilvermobiGRPCService) SetBirthDate(ctx context.Context,
|
||||
req *grpcproto.BirthDateRequest) (res *grpcproto.BirthDateResponse, err error) {
|
||||
|
||||
var (
|
||||
md metadata.MD
|
||||
authHeader []string
|
||||
ok bool
|
||||
)
|
||||
|
||||
if md, ok = metadata.FromIncomingContext(ctx); !ok {
|
||||
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
|
||||
}
|
||||
|
||||
authHeader, ok := md["authorization"]
|
||||
authHeader, ok = md["authorization"]
|
||||
if !ok || len(authHeader) == 0 {
|
||||
return nil, status.Errorf(codes.Unauthenticated, "Missing authorization header")
|
||||
}
|
||||
|
||||
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
|
||||
id := ExtractIdFromToken(tokenString)
|
||||
|
||||
log.Info().
|
||||
Str("User ID", id).
|
||||
Msg("SetBirthDate")
|
||||
|
||||
birthdate := time.Unix(req.Birthdate.Seconds, int64(req.Birthdate.Nanos)).UTC()
|
||||
birthdateString := birthdate.Format("2006-01-02T15:04:05Z")
|
||||
if err = s.Handler.UpdateBirthDate(ctx, id, birthdateString); err != nil {
|
||||
@@ -92,55 +125,85 @@ func (s SilvermobiGRPCService) SetBirthDate(ctx context.Context, req *grpcproto.
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s SilvermobiGRPCService) SetAccountType(ctx context.Context, req *grpcproto.AccountTypeRequest) (res *grpcproto.AccountTypeResponse, err error) {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
func (s SilvermobiGRPCService) SetAccountType(ctx context.Context,
|
||||
req *grpcproto.AccountTypeRequest) (res *grpcproto.AccountTypeResponse, err error) {
|
||||
|
||||
var (
|
||||
md metadata.MD
|
||||
authHeader []string
|
||||
ok bool
|
||||
)
|
||||
|
||||
if md, ok = metadata.FromIncomingContext(ctx); !ok {
|
||||
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
|
||||
}
|
||||
authHeader, ok := md["authorization"]
|
||||
|
||||
authHeader, ok = md["authorization"]
|
||||
if !ok || len(authHeader) == 0 {
|
||||
return nil, status.Errorf(codes.Unauthenticated, "Missing authorization header")
|
||||
return nil, status.Errorf(codes.Unauthenticated,
|
||||
"Missing authorization header")
|
||||
}
|
||||
|
||||
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
|
||||
|
||||
id := ExtractIdFromToken(tokenString)
|
||||
|
||||
log.Info().
|
||||
Str("User ID", id).
|
||||
Msg("SetAccountType")
|
||||
|
||||
if req.GetType() != grpcproto.AccountTypeRequest_PASSENGER && req.GetType() != grpcproto.AccountTypeRequest_DRIVER {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "Type should be PASSENGER or DRIVER")
|
||||
}
|
||||
|
||||
if err = s.Handler.SetAccountType(ctx, id, req.GetType().String()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &grpcproto.AccountTypeResponse{
|
||||
Ok: true,
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
func (s SilvermobiGRPCService) GetAccountType(ctx context.Context, req *grpcproto.AccountTypeRequest) (res *grpcproto.AccountTypeResponse, err error) {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
func (s SilvermobiGRPCService) GetAccountType(ctx context.Context,
|
||||
req *grpcproto.AccountTypeRequest) (res *grpcproto.AccountTypeResponse, err error) {
|
||||
|
||||
var (
|
||||
md metadata.MD
|
||||
authHeader []string
|
||||
ok bool
|
||||
accountType string
|
||||
responseType grpcproto.AccountTypeResponse_AccountType
|
||||
)
|
||||
|
||||
if md, ok = metadata.FromIncomingContext(ctx); !ok {
|
||||
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
|
||||
}
|
||||
authHeader, ok := md["authorization"]
|
||||
|
||||
authHeader, ok = md["authorization"]
|
||||
|
||||
if !ok || len(authHeader) == 0 {
|
||||
return nil, status.Errorf(codes.Unauthenticated, "Missing authorization header")
|
||||
}
|
||||
|
||||
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
|
||||
|
||||
id := ExtractIdFromToken(tokenString)
|
||||
|
||||
log.Info().
|
||||
Str("User ID", id).
|
||||
Msg("GetAccountType")
|
||||
|
||||
if req.Request == false || !req.Request {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "request arg should be true")
|
||||
}
|
||||
account_type, err := s.Handler.GetAccountType(ctx, id)
|
||||
if err != nil {
|
||||
|
||||
if accountType, err = s.Handler.GetAccountType(ctx, id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var responseType grpcproto.AccountTypeResponse_AccountType
|
||||
switch account_type {
|
||||
|
||||
switch accountType {
|
||||
case "PASSENGER":
|
||||
responseType = grpcproto.AccountTypeResponse_PASSENGER
|
||||
case "DRIVER":
|
||||
@@ -157,45 +220,66 @@ func (s SilvermobiGRPCService) GetAccountType(ctx context.Context, req *grpcprot
|
||||
|
||||
}
|
||||
|
||||
func (s SilvermobiGRPCService) GetValidation(ctx context.Context, req *grpcproto.ValidationRequest) (res *grpcproto.ValidationResponse, err error) {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
func (s SilvermobiGRPCService) GetValidation(ctx context.Context,
|
||||
req *grpcproto.ValidationRequest) (res *grpcproto.ValidationResponse, err error) {
|
||||
|
||||
var (
|
||||
md metadata.MD
|
||||
authHeader []string
|
||||
ok, phoneValidation, birthValidation, typeValidation bool
|
||||
)
|
||||
|
||||
if md, ok = metadata.FromIncomingContext(ctx); ok {
|
||||
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
|
||||
}
|
||||
authHeader, ok := md["authorization"]
|
||||
|
||||
authHeader, ok = md["authorization"]
|
||||
|
||||
if !ok || len(authHeader) == 0 {
|
||||
return nil, status.Errorf(codes.Unauthenticated, "Missing authorization header")
|
||||
}
|
||||
|
||||
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
|
||||
|
||||
id := ExtractIdFromToken(tokenString)
|
||||
fmt.Println(id)
|
||||
phone_validation, birth_validation, type_validation, err := s.Handler.CheckValidation(ctx, id)
|
||||
if err != nil {
|
||||
|
||||
if phoneValidation, birthValidation, typeValidation, err = s.Handler.CheckValidation(ctx, id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &grpcproto.ValidationResponse{
|
||||
Phone: phone_validation,
|
||||
Birthdate: birth_validation,
|
||||
Type: type_validation,
|
||||
Phone: phoneValidation,
|
||||
Birthdate: birthValidation,
|
||||
Type: typeValidation,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ExtractIdFromToken(tokenString string) string {
|
||||
var (
|
||||
err error
|
||||
payloadBytes []byte
|
||||
payloadMap map[string]interface{}
|
||||
id string
|
||||
ok bool
|
||||
)
|
||||
|
||||
parts := strings.Split(tokenString, ".")
|
||||
|
||||
if len(parts) != 3 {
|
||||
return ""
|
||||
}
|
||||
payloadBytes, err := base64.RawStdEncoding.DecodeString(parts[1])
|
||||
if err != nil {
|
||||
|
||||
if payloadBytes, err = base64.RawStdEncoding.DecodeString(parts[1]); err != nil {
|
||||
return ""
|
||||
}
|
||||
var payloadMap map[string]interface{}
|
||||
if err := json.Unmarshal(payloadBytes, &payloadMap); err != nil {
|
||||
|
||||
if err = json.Unmarshal(payloadBytes, &payloadMap); err != nil {
|
||||
return ""
|
||||
}
|
||||
id, ok := payloadMap["sub"].(string)
|
||||
if !ok {
|
||||
|
||||
if id, ok = payloadMap["sub"].(string); !ok {
|
||||
return ""
|
||||
}
|
||||
|
||||
return id
|
||||
}
|
||||
|
||||
@@ -79,18 +79,18 @@ func StreamAuthServerInterceptor(authFunc grpc_auth.AuthFunc) grpc.StreamServerI
|
||||
|
||||
type SilvermobiGRPCService struct {
|
||||
Config *viper.Viper
|
||||
Handler *handler.SilvermobiHandler
|
||||
Handler *handler.SilverMobiHandler
|
||||
grpcproto.UnimplementedSilvermobiGRPCServer
|
||||
}
|
||||
|
||||
type SolidarityService struct {
|
||||
Config *viper.Viper
|
||||
Handler *handler.SilvermobiHandler
|
||||
Handler *handler.SilverMobiHandler
|
||||
SolidarityClient grpcproto.SolidarityServiceClient
|
||||
grpcproto.UnimplementedSolidarityServiceServer // Add this client
|
||||
}
|
||||
|
||||
func NewSolidarityService(cfg *viper.Viper, handler *handler.SilvermobiHandler) SolidarityService {
|
||||
func NewSolidarityService(cfg *viper.Viper, handler *handler.SilverMobiHandler) SolidarityService {
|
||||
solidarityServiceAddress := cfg.GetString("solidarity_service.address")
|
||||
conn, err := grpc.Dial(solidarityServiceAddress, grpc.WithInsecure())
|
||||
if err != nil {
|
||||
@@ -105,14 +105,14 @@ func NewSolidarityService(cfg *viper.Viper, handler *handler.SilvermobiHandler)
|
||||
}
|
||||
}
|
||||
|
||||
func NewSilvermobiGRPCService(cfg *viper.Viper, handler *handler.SilvermobiHandler) SilvermobiGRPCService {
|
||||
func NewSilvermobiGRPCService(cfg *viper.Viper, handler *handler.SilverMobiHandler) SilvermobiGRPCService {
|
||||
return SilvermobiGRPCService{
|
||||
Config: cfg,
|
||||
Handler: handler,
|
||||
}
|
||||
}
|
||||
|
||||
func Run(done chan error, cfg *viper.Viper, handler *handler.SilvermobiHandler) {
|
||||
func Run(done chan error, cfg *viper.Viper, handler *handler.SilverMobiHandler) {
|
||||
var (
|
||||
address = cfg.GetString("services.external.grpc.ip") + ":" + cfg.GetString("services.external.grpc.port")
|
||||
jwt_secret = cfg.GetString("identification.local.jwt_secret")
|
||||
|
||||
@@ -2,76 +2,106 @@ package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.coopgo.io/coopgo-apps/silvermobi/servers/grpcapi/proto"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (s *SolidarityService) SetDriverRegularAvailabilities(ctx context.Context, req *proto.DriverRegularAvailabilities) (resp *proto.DriverAvailabilitiesResponse, err error) {
|
||||
func (s *SolidarityService) SetDriverRegularAvailabilities(ctx context.Context,
|
||||
req *proto.DriverRegularAvailabilities) (resp *proto.DriverAvailabilitiesResponse, err error) {
|
||||
log.Info().
|
||||
Str("Driver ID", req.DriverRequest.Driver.Id).
|
||||
Msg("SetDriverRegularAvailabilities")
|
||||
resp, err = s.SolidarityClient.SetDriverRegularAvailabilities(ctx, req)
|
||||
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (s *SolidarityService) SetDriverPunctualAvailabilities(ctx context.Context, req *proto.DriverPunctualAvailabilities) (resp *proto.DriverAvailabilitiesResponse, err error) {
|
||||
func (s *SolidarityService) SetDriverPunctualAvailabilities(ctx context.Context,
|
||||
req *proto.DriverPunctualAvailabilities) (resp *proto.DriverAvailabilitiesResponse, err error) {
|
||||
log.Info().
|
||||
Str("Driver ID", req.DriverRequest.Driver.Id).
|
||||
Msg("SetDriverRegularAvailabilities")
|
||||
resp, err = s.SolidarityClient.SetDriverPunctualAvailabilities(ctx, req)
|
||||
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (s *SolidarityService) CreateBooking(ctx context.Context, req *proto.CreateBookingRequest) (resp *proto.CreateBookingResponse, err error) {
|
||||
func (s *SolidarityService) CreateBooking(ctx context.Context,
|
||||
req *proto.CreateBookingRequest) (resp *proto.CreateBookingResponse, err error) {
|
||||
|
||||
log.Info().
|
||||
Str("Booking ID", req.Booking.Id).
|
||||
Msg("CreateBooking")
|
||||
resp, err = s.SolidarityClient.CreateBooking(ctx, req)
|
||||
if err == nil {
|
||||
_ = s.Handler.SendNotification(req.Booking.DriverId, "Silvermobi", "Vous avez reçu une demande de trajet. \n Pour plus de détails, veuillez consulter l'interface \"Mes Trajets\" dans l'application SilverMobi.")
|
||||
err = s.Handler.SendEmail(req.Booking.DriverId, "Silvermobi", "Vous avez reçu une demande de trajet. \n Pour plus de détails, veuillez consulter l'interface \"Mes Trajets\" dans l'application SilverMobi.")
|
||||
fmt.Println(err)
|
||||
|
||||
if resp, err = s.SolidarityClient.CreateBooking(ctx, req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = s.Handler.SendNotification(req.Booking.DriverId,
|
||||
"Silvermobi", "Vous avez reçu une demande de trajet. "+
|
||||
"\n Pour plus de détails, veuillez consulter"+
|
||||
" l'interface \"Mes Trajets\" dans l'application SilverMobi."); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = s.Handler.SendEmail(req.Booking.DriverId, "Silvermobi",
|
||||
"Vous avez reçu une demande de trajet. \n Pour plus de détails, "+
|
||||
"veuillez consulter l'interface \"Mes Trajets\" dans l'application SilverMobi."); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (s *SolidarityService) UpdateBooking(ctx context.Context, req *proto.UpdateBookingRequest) (resp *proto.UpdateBookingResponse, err error) {
|
||||
func (s *SolidarityService) UpdateBooking(ctx context.Context,
|
||||
req *proto.UpdateBookingRequest) (resp *proto.UpdateBookingResponse, err error) {
|
||||
|
||||
log.Info().
|
||||
Str("Booking ID", req.BookingId).
|
||||
Msg("UpdateBooking")
|
||||
resp, err = s.SolidarityClient.UpdateBooking(ctx, req)
|
||||
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (s *SolidarityService) GetBooking(ctx context.Context, req *proto.GetBookingRequest) (resp *proto.GetBookingResponse, err error) {
|
||||
func (s *SolidarityService) GetBooking(ctx context.Context,
|
||||
req *proto.GetBookingRequest) (resp *proto.GetBookingResponse, err error) {
|
||||
log.Info().
|
||||
Str("Booking ID", req.BookingId).
|
||||
Msg("GetBooking")
|
||||
resp, err = s.SolidarityClient.GetBooking(ctx, req)
|
||||
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (s *SolidarityService) GetBookingsByStatus(ctx context.Context, req *proto.GetBookingsByStatusRequest) (resp *proto.GetBookingsByStatusResponse, err error) {
|
||||
func (s *SolidarityService) GetBookingsByStatus(ctx context.Context,
|
||||
req *proto.GetBookingsByStatusRequest) (resp *proto.GetBookingsByStatusResponse, err error) {
|
||||
|
||||
log.Info().
|
||||
Str("User ID", req.UserId).
|
||||
Msg("GetBookingByStatus")
|
||||
resp, err = s.SolidarityClient.GetBookingsByStatus(ctx, req)
|
||||
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (s *SolidarityService) DriverJourneys(ctx context.Context, req *proto.DriverJourneysRequest) (resp *proto.DriverJourneysResponse, err error) {
|
||||
func (s *SolidarityService) DriverJourneys(ctx context.Context,
|
||||
req *proto.DriverJourneysRequest) (resp *proto.DriverJourneysResponse, err error) {
|
||||
log.Info().
|
||||
Str("Address", req.Departure.Address).
|
||||
Msg("DriverJourneys")
|
||||
resp, err = s.SolidarityClient.DriverJourneys(ctx, req)
|
||||
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (s *SolidarityService) SetPassengerTrip(ctx context.Context, req *proto.PassengerTripRequest) (resp *proto.PassengerTripResponse, err error) {
|
||||
func (s *SolidarityService) SetPassengerTrip(ctx context.Context,
|
||||
req *proto.PassengerTripRequest) (resp *proto.PassengerTripResponse, err error) {
|
||||
log.Info().
|
||||
Str("Passenger ID", req.Passenger.Id).
|
||||
Msg("SetPassengerTrip")
|
||||
resp, err = s.SolidarityClient.SetPassengerTrip(ctx, req)
|
||||
|
||||
return resp, err
|
||||
}
|
||||
|
||||
@@ -2,22 +2,27 @@ package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
grpcproto "git.coopgo.io/coopgo-apps/silvermobi/servers/grpcapi/proto"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (s SilvermobiGRPCService) UpdatePassword(ctx context.Context, req *grpcproto.UpdatePasswordRequest) (res *grpcproto.UpdatePasswordResponse, err error) {
|
||||
func (s SilvermobiGRPCService) UpdatePassword(ctx context.Context,
|
||||
req *grpcproto.UpdatePasswordRequest) (res *grpcproto.UpdatePasswordResponse, err error) {
|
||||
|
||||
log.Info().
|
||||
Str("username", req.Email).
|
||||
Msg("Update Password")
|
||||
|
||||
result := s.Handler.UpdatePassword(ctx, req.Email, req.Password)
|
||||
if result == true {
|
||||
if result {
|
||||
return &grpcproto.UpdatePasswordResponse{
|
||||
Response: true,
|
||||
}, nil
|
||||
} else {
|
||||
return &grpcproto.UpdatePasswordResponse{
|
||||
Response: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &grpcproto.UpdatePasswordResponse{
|
||||
Response: false,
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user