feat(transport): added ability to enable gRPC reflection

This commit is contained in:
ITQ
2025-10-27 22:35:53 +03:00
parent db44030c9f
commit 21935804f9
4 changed files with 20 additions and 4 deletions
+1
View File
@@ -2,4 +2,5 @@
# Below all environment variables and default values # Below all environment variables and default values
GRPC_PORT=50051 GRPC_PORT=50051
GRPC_ENABLE_REFLECTION=false
LOG_LEVEL=info LOG_LEVEL=info
+1
View File
@@ -34,6 +34,7 @@ And setup env vars according to your needs.
```bash ```bash
GRPC_PORT=50051 # gRPC server port GRPC_PORT=50051 # gRPC server port
GRPC_ENABLE_REFLECTION=false # whether to enable gRPC reflection or not
LOG_LEVEL=info # logging severity (debug, info, warn, error) LOG_LEVEL=info # logging severity (debug, info, warn, error)
``` ```
+8
View File
@@ -10,6 +10,7 @@ import (
type Config struct { type Config struct {
GRPCPort int GRPCPort int
GRPCEnableReflection bool
LogLevel string LogLevel string
} }
@@ -27,6 +28,13 @@ func Load() (*Config, error) {
} }
config.GRPCPort = port config.GRPCPort = port
enableReflectionStr := getEnv("GRPC_ENABLE_REFLECTION", "false")
enableReflection, err := strconv.ParseBool(enableReflectionStr)
if err != nil {
return nil, err
}
config.GRPCEnableReflection = enableReflection
config.LogLevel = getEnv("LOG_LEVEL", "info") config.LogLevel = getEnv("LOG_LEVEL", "info")
return config, nil return config, nil
+6
View File
@@ -12,6 +12,7 @@ import (
pb "orderservice/pkg/api/order" pb "orderservice/pkg/api/order"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/reflection"
) )
type Server struct { type Server struct {
@@ -36,6 +37,11 @@ func New(cfg *config.Config) *Server {
func (s *Server) RegisterServices() { func (s *Server) RegisterServices() {
orderService := service.NewOrderServiceServer() orderService := service.NewOrderServiceServer()
pb.RegisterOrderServiceServer(s.grpcServer, orderService) pb.RegisterOrderServiceServer(s.grpcServer, orderService)
if s.config.GRPCEnableReflection {
reflection.Register(s.grpcServer)
log.Println("gRPC server will start with reflection")
}
} }
func (s *Server) Start() error { func (s *Server) Start() error {