protoc-gen-connect-go-servicestruct

command
v0.0.0-...-b11eb7b Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2025 License: MIT Imports: 10 Imported by: 0

README

protoc-gen-connect-go-servicestruct

A protobuf compiler plugin that generates Go service structs with handler functions for Connect RPC services. Requires protoc-gen-connect-go as a dependency.

How-tos

Installation
go install github.com/TrogonStack/protoc-gen/cmd/protoc-gen-connect-go-servicestruct@latest
Basic Usage

Required: This plugin must be used alongside protoc-gen-connect-go as it generates structs that implement the Connect handler interfaces:

protoc --go_out=gen --connect-go_out=gen --connect-go-servicestruct_out=gen path/to/service.proto
With buf

Required: Add to your buf.gen.yaml alongside the required buf.build/connectrpc/go plugin:

version: v2
plugins:
  - remote: buf.build/protocolbuffers/go
    out: gen
    opt: paths=source_relative
  - remote: buf.build/connectrpc/go:v1.18.1  # Required dependency
    out: gen
    opt: paths=source_relative
  - local: protoc-gen-connect-go-servicestruct
    out: gen
    opt: 
      - paths=source_relative

Files are generated into {packagename}connect subdirectories alongside the connect plugin output.

Basic Service Implementation

For more realistic applications that require dependencies like database connections, use constructor functions to create handlers:

package main

import (
    "context"
    "fmt"
    "log"
    "net/http"

    "connectrpc.com/connect"
    "github.com/jackc/pgx/v5/pgxpool"
    greeterv1 "example.com/greeter/gen/greeter/v1"
    "example.com/greeter/gen/greeter/v1/greeterv1connect"
)

func main() {
    // Initialize database connection
    dbPool, err := pgxpool.New(context.Background(), "postgres://user:pass@localhost/db")
    if err != nil {
        log.Fatal("Failed to create connection pool:", err)
    }
    defer dbPool.Close()
    
    mux := http.NewServeMux()
    path, handler := greeterv1connect.NewGreeterServiceHandler(&greeterv1connect.GreeterServiceStruct{
        GreetFunc:       newGreetHandler(dbPool),
        GreetStreamFunc: newGreetStreamHandler(dbPool),
    })
    mux.Handle(path, handler)
    
    log.Println("Greeter server starting on :8080")
    http.ListenAndServe(":8080", mux)
}

func newGreetHandler(db *pgxpool.Pool) greeterv1.GreeterServiceGreetHandlerFunc {
    return func(ctx context.Context, req *connect.Request[greeterv1.GreetRequest]) (*connect.Response[greeterv1.GreetResponse], error) {
        //    ...
    }
}

func newGreetStreamHandler(db *pgxpool.Pool) greeterv1.GreeterServiceGreetStreamHandlerFunc {
    return func(ctx context.Context, stream *connect.BidiStream[greeterv1.GreetRequest, greeterv1.GreetResponse]) error {
        // ...
    }
}

Explanations

Overview

This plugin generates service structs that provide a convenient way to organize and access handler functions for Connect RPC services. This plugin requires protoc-gen-connect-go (or buf.build/connectrpc/go) to function - it generates structs that implement the Connect handler interfaces created by the standard Connect plugin.

Features
  • Service Structs: Generates public structs with handler function fields.
  • Type Safety: Uses strongly-typed handler function types.
  • Interface Compatibility: Generated structs implement the standard handler interfaces for backwards compatibility.

Documentation

Overview

protoc-gen-connect-go-servicestruct is a plugin for the Protobuf compiler that generates Go service structs with handler functions. To use it, build this program and make it available on your PATH as protoc-gen-connect-go-servicestruct.

The 'connect-go-servicestruct' suffix becomes part of the arguments for the Protobuf compiler. To generate service structs using protoc:

protoc --go_out=gen --connect-go-servicestruct_out=gen path/to/file.proto

With buf, your buf.gen.yaml will look like this:

version: v2
plugins:
  - local: protoc-gen-go
    out: gen
  - local: protoc-gen-connect-go-servicestruct
    out: gen

This generates service struct definitions for the Protobuf services defined by file.proto. If file.proto defines the foov1 Protobuf package, the invocations above will write output to:

gen/path/to/file.pb.go
gen/path/to/foov1connect/file.servicestruct.connect.go

Jump to

Keyboard shortcuts

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