gRPC Server & Interceptors
boot/server/grpc.go and boot/server/middleware/grpc.go run a hardened google.golang.org/grpc server alongside HTTP whenever GRPC_ENABLE=true.
Registration (GRPCRegistry)
Any service implementing server.GRPCRegistry is auto-collected by boot/kernel.go via di.AutoCollect[server.GRPCRegistry]():
type GRPCRegistry interface {
Register(s *grpc.Server)
}Legacy global registration (server.RegisterGRPC(...) and server.GetGRPCRegistries(), guarded by sync.RWMutex) is also merged by NewGRPCServer for backwards compatibility.
Server Features (boot/server/grpc.go)
- Binding: listens on
net.JoinHostPort(cfg.GRPC.Host, strconv.Itoa(cfg.GRPC.Port))(default127.0.0.1:50051). - TLS: when
GRPC_TLS_ENABLED=true, loadsGRPC_TLS_CERT_FILEandGRPC_TLS_KEY_FILEviacredentials.NewServerTLSFromFile. Load errors fail fast onStart(). - Health Checking: registers standard
grpc_health_v1(health.NewServer()), marksSERVINGon start andNOT_SERVINGon stop. - Reflection: enables
reflection.Register(s)whenGRPC_REFLECTION=true. - Graceful Shutdown:
Stop(ctx)closes the ownedLimiterStore, callss.server.GracefulStop(), and falls back tos.server.Stop()ifctx.Done()fires.
Unary & Stream Interceptor Chain
middleware.GrpcServerOptions(cfg, store) attaches both ChainUnaryInterceptor and ChainStreamInterceptor in this exact order:
- Recovery — catches panics, logs stack trace via zerolog, returns
codes.Internal. - Logger — logs
Msg("gRPC Request")withmethod,code,ip(port-stripped),latency(Errorlevel on non-nil error,Infootherwise). - AppKey Auth — reads incoming gRPC metadata
x-app-key, validates viaValidateAppKeyValue, returnscodes.Unauthenticatedif invalid. - Rate Limiter — calls
store.Allow(peerIP(ctx));peerIPusesnet.SplitHostPortto strip ephemeral client TCP ports so limits apply per IP rather than per connection. Exceeded limits returncodes.ResourceExhausted.
Proto Layout & Codegen
Place .proto files at internal/proto/<service>/<service>.proto:
option go_package = "github.com/myorg/myapp/internal/proto/<service>/gen";Run goforge gen:proto <service> to compile .pb.go + _grpc.pb.go into internal/proto/<service>/gen/, generate internal/services/<service>/grpc.go, and register New<Type>GRPC in module.go.
