replacing depracated grpc Dial + camelcases vars + fixing issues
This commit is contained in:
@@ -17,7 +17,7 @@ func (s SilvermobiGRPCService) GeoAutocomplete(ctx context.Context,
|
||||
|
||||
var (
|
||||
results *geojson.FeatureCollection
|
||||
rawfc []byte
|
||||
rawFc []byte
|
||||
)
|
||||
|
||||
log.Info().
|
||||
@@ -33,7 +33,7 @@ func (s SilvermobiGRPCService) GeoAutocomplete(ctx context.Context,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if rawfc, err = results.MarshalJSON(); err != nil {
|
||||
if rawFc, err = results.MarshalJSON(); err != nil {
|
||||
log.Error().Str("requestID", requestID).Err(err).Msg("GRPC GeoAutocomplete " +
|
||||
"protocol buffer conversion error")
|
||||
return nil, err
|
||||
@@ -41,7 +41,7 @@ func (s SilvermobiGRPCService) GeoAutocomplete(ctx context.Context,
|
||||
|
||||
resp = &grpcproto.GeoAutocompleteResponse{
|
||||
FeatureCollection: &grpcproto.GeoAutocompleteResponse_FeatureCollectionRaw{
|
||||
FeatureCollectionRaw: string(rawfc),
|
||||
FeatureCollectionRaw: string(rawFc),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -2,19 +2,20 @@ package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"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"
|
||||
grpcmiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
||||
grpcauth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
|
||||
grpcctxtags "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/credentials/insecure"
|
||||
"google.golang.org/grpc/status"
|
||||
"net"
|
||||
)
|
||||
|
||||
type contextKey string
|
||||
@@ -42,37 +43,51 @@ func NoAuth(method string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func UnaryAuthServerInterceptor(authFunc grpc_auth.AuthFunc) grpc.UnaryServerInterceptor {
|
||||
func UnaryAuthServerInterceptor(authFunc grpcauth.AuthFunc) grpc.UnaryServerInterceptor {
|
||||
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
||||
|
||||
var (
|
||||
newCtx context.Context
|
||||
err 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 {
|
||||
|
||||
if newCtx, err = authFunc(ctx); 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 {
|
||||
func StreamAuthServerInterceptor(authFunc grpcauth.AuthFunc) grpc.StreamServerInterceptor {
|
||||
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo,
|
||||
handler grpc.StreamHandler) error {
|
||||
|
||||
var (
|
||||
newCtx context.Context
|
||||
err error
|
||||
)
|
||||
|
||||
if NoAuth(info.FullMethod) {
|
||||
wrapped := grpc_middleware.WrapServerStream(stream)
|
||||
wrapped := grpcmiddleware.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 {
|
||||
|
||||
if newCtx, err = authFunc(stream.Context()); err != nil {
|
||||
return err
|
||||
}
|
||||
wrapped := grpc_middleware.WrapServerStream(stream)
|
||||
|
||||
wrapped := grpcmiddleware.WrapServerStream(stream)
|
||||
wrapped.WrappedContext = newCtx
|
||||
|
||||
return handler(srv, wrapped)
|
||||
}
|
||||
}
|
||||
@@ -84,20 +99,22 @@ type SilvermobiGRPCService struct {
|
||||
}
|
||||
|
||||
type SolidarityService struct {
|
||||
Config *viper.Viper
|
||||
Handler *handler.SilverMobiHandler
|
||||
SolidarityClient grpcproto.SolidarityServiceClient
|
||||
grpcproto.UnimplementedSolidarityServiceServer // Add this client
|
||||
Config *viper.Viper
|
||||
Handler *handler.SilverMobiHandler
|
||||
SolidarityClient grpcproto.SolidarityServiceClient
|
||||
grpcproto.UnimplementedSolidarityServiceServer
|
||||
}
|
||||
|
||||
func NewSolidarityService(cfg *viper.Viper, handler *handler.SilverMobiHandler) SolidarityService {
|
||||
solidarityServiceAddress := cfg.GetString("solidarity_service.address")
|
||||
conn, err := grpc.Dial(solidarityServiceAddress, grpc.WithInsecure())
|
||||
|
||||
conn, err := grpc.NewClient(solidarityServiceAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err)
|
||||
}
|
||||
|
||||
solidarityClient := grpcproto.NewSolidarityServiceClient(conn)
|
||||
|
||||
return SolidarityService{
|
||||
Config: cfg,
|
||||
Handler: handler,
|
||||
@@ -114,54 +131,61 @@ func NewSilvermobiGRPCService(cfg *viper.Viper, handler *handler.SilverMobiHandl
|
||||
|
||||
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")
|
||||
address = cfg.GetString("services.external.grpc.ip") + ":" + cfg.GetString("services.external.grpc.port")
|
||||
jwtSecret = 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.StreamInterceptor(grpcmiddleware.ChainStreamServer(
|
||||
grpcctxtags.StreamServerInterceptor(),
|
||||
StreamAuthServerInterceptor(GRPCAuthFunc(jwtSecret)),
|
||||
)),
|
||||
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
|
||||
grpc_ctxtags.UnaryServerInterceptor(),
|
||||
UnaryAuthServerInterceptor(GRPCAuthFunc(jwt_secret)),
|
||||
grpc.UnaryInterceptor(grpcmiddleware.ChainUnaryServer(
|
||||
grpcctxtags.UnaryServerInterceptor(),
|
||||
UnaryAuthServerInterceptor(GRPCAuthFunc(jwtSecret)),
|
||||
)),
|
||||
)
|
||||
|
||||
solidarity_service := NewSolidarityService(cfg, handler)
|
||||
solidarityService := NewSolidarityService(cfg, handler)
|
||||
|
||||
silvermobi_service := NewSilvermobiGRPCService(cfg, handler)
|
||||
grpcproto.RegisterSilvermobiGRPCServer(server, silvermobi_service)
|
||||
grpcproto.RegisterSolidarityServiceServer(server, &solidarity_service)
|
||||
silvermobiService := NewSilvermobiGRPCService(cfg, handler)
|
||||
grpcproto.RegisterSilvermobiGRPCServer(server, silvermobiService)
|
||||
grpcproto.RegisterSolidarityServiceServer(server, &solidarityService)
|
||||
l, err := net.Listen("tcp", address)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err)
|
||||
}
|
||||
|
||||
if err := server.Serve(l); err != nil {
|
||||
if err = server.Serve(l); err != nil {
|
||||
log.Error().Err(err).Msg("gRPC service ended")
|
||||
done <- err
|
||||
}
|
||||
}
|
||||
|
||||
func GRPCAuthFunc(jwtKey string) grpc_auth.AuthFunc {
|
||||
func GRPCAuthFunc(jwtKey string) grpcauth.AuthFunc {
|
||||
return func(ctx context.Context) (context.Context, error) {
|
||||
tokenString, err := grpc_auth.AuthFromMD(ctx, "bearer")
|
||||
|
||||
var token *jwt.Token
|
||||
|
||||
tokenString, err := grpcauth.AuthFromMD(ctx, "bearer")
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
claims := jwt.MapClaims{}
|
||||
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user