|
| 1 | +# gRPC Multi-Language Code Generator |
| 2 | + |
| 3 | +A unified build tool that generates gRPC server and client code for multiple languages from Protocol Buffer (.proto) files. |
| 4 | + |
| 5 | +## Supported Languages |
| 6 | + |
| 7 | +- **Go**: Server and client code using protoc-gen-go and protoc-gen-go-grpc |
| 8 | +- **Python**: Server and client code using grpcio-tools |
| 9 | +- **TypeScript/NestJS**: Both general TypeScript and NestJS-specific code using ts-proto |
| 10 | +- **Rust**: Server and client code using tonic-build |
| 11 | + |
| 12 | +## Directory Structure |
| 13 | + |
| 14 | +``` |
| 15 | +├── proto/ # Protocol Buffer definitions |
| 16 | +│ ├── hero.proto |
| 17 | +│ └── calculator.proto |
| 18 | +├── generated/ # Generated code output |
| 19 | +│ ├── go/ # Go generated files |
| 20 | +│ ├── python/ # Python generated files |
| 21 | +│ ├── typescript/ # TypeScript generated files |
| 22 | +│ │ └── nestjs/ # NestJS-specific files |
| 23 | +│ └── rust/ # Rust generated files |
| 24 | +├── codegen.js # Main build tool |
| 25 | +└── package.json # Node.js dependencies |
| 26 | +``` |
| 27 | + |
| 28 | +## Installation |
| 29 | + |
| 30 | +1. Install dependencies: |
| 31 | +```bash |
| 32 | +npm install |
| 33 | +``` |
| 34 | + |
| 35 | +2. The tool will automatically install language-specific dependencies when needed. |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +### Generate for All Languages |
| 40 | +```bash |
| 41 | +npm run build |
| 42 | +# or |
| 43 | +node codegen.js |
| 44 | +``` |
| 45 | + |
| 46 | +### Generate for Specific Language |
| 47 | +```bash |
| 48 | +# Go |
| 49 | +npm run build:go |
| 50 | +node codegen.js --language=go |
| 51 | + |
| 52 | +# Python |
| 53 | +npm run build:python |
| 54 | +node codegen.js --language=python |
| 55 | + |
| 56 | +# TypeScript/NestJS |
| 57 | +npm run build:typescript |
| 58 | +node codegen.js --language=typescript |
| 59 | + |
| 60 | +# Rust |
| 61 | +npm run build:rust |
| 62 | +node codegen.js --language=rust |
| 63 | +``` |
| 64 | + |
| 65 | +### List Supported Languages |
| 66 | +```bash |
| 67 | +node codegen.js --list |
| 68 | +``` |
| 69 | + |
| 70 | +## Adding New Proto Files |
| 71 | + |
| 72 | +1. Add your `.proto` files to the `proto/` directory |
| 73 | +2. Run the code generator |
| 74 | +3. The generated code will be available in the `generated/` directory |
| 75 | + |
| 76 | +## Example Proto File |
| 77 | + |
| 78 | +```proto |
| 79 | +syntax = "proto3"; |
| 80 | +package myservice; |
| 81 | +
|
| 82 | +option go_package = "./myservice"; |
| 83 | +
|
| 84 | +message MyRequest { |
| 85 | + string name = 1; |
| 86 | +} |
| 87 | +
|
| 88 | +message MyResponse { |
| 89 | + string message = 1; |
| 90 | +} |
| 91 | +
|
| 92 | +service MyService { |
| 93 | + rpc SayHello(MyRequest) returns (MyResponse) {} |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +## Generated Code Usage |
| 98 | + |
| 99 | +### Go |
| 100 | +```go |
| 101 | +import "path/to/generated/go/myservice" |
| 102 | +``` |
| 103 | + |
| 104 | +### Python |
| 105 | +```python |
| 106 | +import myservice_pb2 |
| 107 | +import myservice_pb2_grpc |
| 108 | +``` |
| 109 | + |
| 110 | +### TypeScript |
| 111 | +```typescript |
| 112 | +import { MyServiceClient } from './generated/typescript/myservice'; |
| 113 | +``` |
| 114 | + |
| 115 | +### Rust |
| 116 | +```rust |
| 117 | +use grpc_generated::myservice::{MyRequest, MyResponse}; |
| 118 | +``` |
| 119 | + |
| 120 | +## Requirements |
| 121 | + |
| 122 | +- Node.js (for the build tool) |
| 123 | +- Protocol Buffers compiler (included) |
| 124 | +- Language-specific requirements: |
| 125 | + - Go: Go compiler and tools |
| 126 | + - Python: Python 3 and pip |
| 127 | + - TypeScript: Node.js and npm |
| 128 | + - Rust: Cargo and Rust toolchain |
0 commit comments