Help us shape the future of Go! Take the 2026 Go Developer Survey

grpc

package module
v0.0.0-...-59a70be Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 25, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package grpc provides a lagodev-flavoured gRPC server: Laravel-style graceful shutdown, structured logging, panic recovery, and an optional JWT auth interceptor that surfaces `auth_user_id` and `auth_role` on the request context — the same contract the lagodev web middleware uses.

The adapter is a separate Go module so the core lagodev module does not depend on google.golang.org/grpc:

go get github.com/devituz/lagodev/adapters/grpc@latest

Usage:

srv := lagogrpc.New(lagogrpc.Options{
    Addr:    ":50051",
    Auth:    lagogrpc.AuthFromManager(authMgr),
    Logger:  log.Default(),
})
mypb.RegisterMyServiceServer(srv.GRPC(), impl)
if err := srv.Run(ctx); err != nil { ... }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Claims

func Claims(ctx context.Context) *lagoauth.Claims

Claims returns the parsed *auth.Claims set by AuthFromManager.

func Role

func Role(ctx context.Context) string

Role returns the authenticated role set by AuthFromManager.

func UserID

func UserID(ctx context.Context) uint64

UserID returns the authenticated user ID set by AuthFromManager (zero when not set).

Types

type AuthFunc

type AuthFunc func(ctx context.Context, token string) (context.Context, error)

AuthFunc resolves the metadata-carried token to a claims-bearing context. Returning a non-nil error short-circuits the RPC with codes.Unauthenticated.

func AuthFromManager

func AuthFromManager(m *lagoauth.Manager) AuthFunc

AuthFromManager builds an AuthFunc using a lagodev/auth Manager. On success it populates "auth_user_id" and "auth_role" in the context so service handlers can pull them via FromContext.

type Options

type Options struct {
	// Addr to listen on (e.g. ":50051").
	Addr string
	// Logger receives access / panic logs. Default: log.Default().
	Logger *log.Logger
	// ShutdownTimeout caps GracefulStop. Default 10s.
	ShutdownTimeout time.Duration
	// Auth optionally enforces a JWT on every RPC. When non-nil it is
	// installed as a unary interceptor; pair with AuthFromManager to
	// reuse the lagodev/auth manager you already configured for HTTP.
	Auth AuthFunc
	// Recovery toggles panic recovery (default true).
	Recovery bool
	// Logging toggles access logging (default true).
	Logging bool
	// AdditionalUnaryInterceptors are appended after Recovery/Auth/
	// Logging so call sites can add tracing or rate-limit middleware.
	AdditionalUnaryInterceptors []grpc.UnaryServerInterceptor
	// AdditionalStreamInterceptors are appended after Recovery/Auth/
	// Logging in the streaming chain.
	AdditionalStreamInterceptors []grpc.StreamServerInterceptor
	// AdditionalOptions are appended to the underlying grpc.Server
	// construction (e.g. TLS credentials).
	AdditionalOptions []grpc.ServerOption
}

Options configures Server.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is the lagodev-flavoured gRPC server.

func New

func New(opts Options) *Server

New constructs a Server. Interceptors are installed in the order: Recovery → Auth → Logging → user-supplied.

func (*Server) GRPC

func (s *Server) GRPC() *grpc.Server

GRPC returns the underlying *grpc.Server so callers can RegisterX().

func (*Server) Run

func (s *Server) Run(ctx context.Context) error

Run starts the listener and blocks until ctx is cancelled or a SIGINT/SIGTERM is received. GracefulStop is called on shutdown.

func (*Server) Stop

func (s *Server) Stop()

Stop terminates the server immediately. Use for tests; production callers should rely on Run's graceful shutdown.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL