fix dialogue parcourmob <=> silvermobi

This commit is contained in:
2024-07-31 14:04:53 +02:00
parent cf792f2e7c
commit 9f9ab9f1c0
6 changed files with 387 additions and 63 deletions

View File

@@ -0,0 +1,77 @@
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) {
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) {
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) {
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)
}
return resp, err
}
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) {
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) {
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) {
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) {
log.Info().
Str("Passenger ID", req.Passenger.Id).
Msg("SetPassengerTrip")
resp, err = s.SolidarityClient.SetPassengerTrip(ctx, req)
return resp, err
}

View File

@@ -0,0 +1,167 @@
package grpcserver
import (
"context"
"fmt"
"git.coopgo.io/coopgo-apps/silvermobi/handler"
grpcproto "git.coopgo.io/coopgo-apps/silvermobi/servers/grpcapi/proto"
"github.com/golang-jwt/jwt/v4"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"net"
)
type contextKey string
var (
contextKeyUser = contextKey("user")
)
func NoAuth(method string) bool {
noAuthMethods := []string{
"/SilvermobiGRPC/ForgetAccount",
"/SilvermobiGRPC/UpdatePassword",
"/SilvermobiGRPC/AuthRegister",
"/SilvermobiGRPC/AuthLogin",
"/SilvermobiGRPC/GeoAutocomplete",
"/SilvermobiGRPC/GeoRouteWithReturn",
"/SilvermobiGRPC/GeoRoute",
}
for _, m := range noAuthMethods {
if method == m {
return true
}
}
return false
}
func UnaryAuthServerInterceptor(authFunc grpc_auth.AuthFunc) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
print(info.FullMethod)
if NoAuth(info.FullMethod) {
return handler(ctx, req)
}
var newCtx context.Context
var err error
newCtx, err = authFunc(ctx)
if err != nil {
return nil, err
}
return handler(newCtx, req)
}
}
func StreamAuthServerInterceptor(authFunc grpc_auth.AuthFunc) grpc.StreamServerInterceptor {
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
if NoAuth(info.FullMethod) {
wrapped := grpc_middleware.WrapServerStream(stream)
wrapped.WrappedContext = stream.Context()
return handler(srv, wrapped)
}
var newCtx context.Context
var err error
newCtx, err = authFunc(stream.Context())
if err != nil {
return err
}
wrapped := grpc_middleware.WrapServerStream(stream)
wrapped.WrappedContext = newCtx
return handler(srv, wrapped)
}
}
type SilvermobiGRPCService struct {
Config *viper.Viper
Handler *handler.SilvermobiHandler
grpcproto.UnimplementedSilvermobiGRPCServer
}
type SolidarityService struct {
Config *viper.Viper
Handler *handler.SilvermobiHandler
SolidarityClient grpcproto.SolidarityServiceClient
grpcproto.UnimplementedSolidarityServiceServer // Add this client
}
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 {
log.Fatal().Err(err)
}
solidarityClient := grpcproto.NewSolidarityServiceClient(conn)
return SolidarityService{
Config: cfg,
Handler: handler,
SolidarityClient: solidarityClient,
}
}
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) {
var (
address = cfg.GetString("services.external.grpc.ip") + ":" + cfg.GetString("services.external.grpc.port")
jwt_secret = cfg.GetString("identification.local.jwt_secret")
)
log.Info().Msg("GRPC server on " + address)
server := grpc.NewServer(
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
grpc_ctxtags.StreamServerInterceptor(),
StreamAuthServerInterceptor(GRPCAuthFunc(jwt_secret)),
)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpc_ctxtags.UnaryServerInterceptor(),
UnaryAuthServerInterceptor(GRPCAuthFunc(jwt_secret)),
)),
)
solidarity_service := NewSolidarityService(cfg, handler)
silvermobi_service := NewSilvermobiGRPCService(cfg, handler)
grpcproto.RegisterSilvermobiGRPCServer(server, silvermobi_service)
grpcproto.RegisterSolidarityServiceServer(server, &solidarity_service)
l, err := net.Listen("tcp", address)
if err != nil {
log.Fatal().Err(err)
}
if err := server.Serve(l); err != nil {
log.Error().Err(err).Msg("gRPC service ended")
done <- err
}
}
func GRPCAuthFunc(jwtKey string) grpc_auth.AuthFunc {
return func(ctx context.Context) (context.Context, error) {
tokenString, err := grpc_auth.AuthFromMD(ctx, "bearer")
if err != nil {
return nil, err
}
claims := jwt.MapClaims{}
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
return []byte(jwtKey), nil
})
if err != nil || !token.Valid {
fmt.Println(err)
return nil, status.Errorf(codes.Unauthenticated, "Invalid or expired token")
}
ctx = context.WithValue(ctx, contextKeyUser, claims["sub"].(string))
return ctx, nil
}
}