Skip to content

gRPC Server

pkg/grpc/server runs a gRPC server as a SyncModule, pre-wired with grpc-ecosystem middleware for logging, recovery, and tracing.

Pass your service implementation directly via WithService. No separate module or DI wiring needed.

lakta.NewRuntime(
config.NewModule(
config.WithConfigDirs(".", "./config"),
config.WithArgs(os.Args[1:]),
),
tint.NewModule(),
slog.NewModule(),
otel.NewModule(),
health.NewModule(),
grpcserver.NewModule(
grpcserver.WithService(&v1.MyService_ServiceDesc, NewServer()),
),
)
type MyServer struct {
v1.UnimplementedMyServiceServer
}
func NewServer() *MyServer { return &MyServer{} }
func (s *MyServer) GetThing(ctx context.Context, req *pb.GetThingRequest) (*pb.GetThingResponse, error) {
svc, err := lakta.Invoke[*MyService](ctx)
if err != nil {
return nil, err
}
return svc.GetThing(ctx, req.Id)
}

Embed Unimplemented*Server to satisfy the interface for any methods you haven’t implemented yet. Use lakta.Invoke[T](ctx) inside handlers to access DI — the request context carries the injector.

The following middleware is applied automatically:

  • Recovery — converts panics to gRPC INTERNAL errors
  • Logging — structured request/response logging via slox
  • OpenTelemetry — trace propagation (when otel is enabled)
grpcserver.NewModule(
grpcserver.WithName("internal"),
grpcserver.WithService(&v1.InternalService_ServiceDesc, NewInternalServer()),
),
grpcserver.NewModule(
grpcserver.WithName("public"),
grpcserver.WithService(&v1.PublicService_ServiceDesc, NewPublicServer()),
),

Config path: modules.grpc.server.<name>

host string default: 0.0.0.0

host specifies the address for the GRPC server to bind to

env LAKTA_MODULES_GRPC_SERVER_<NAME>_HOST
port uint16 default: 50051

port represents the port number on which the GRPC server listens

env LAKTA_MODULES_GRPC_SERVER_<NAME>_PORT
health_check bool

healthCheck determines whether gRPC health checking is enabled or disabled

env LAKTA_MODULES_GRPC_SERVER_<NAME>_HEALTH_CHECK

These options can only be set in Go code via With*() functions, not via config files or environment variables.

OptionTypeDescription
WithService(...)map[*grpc.ServiceDesc]anyadds service to the list of services to be registered (code-only)