Initial commit

This commit is contained in:
Zhongwei Li
2025-11-29 18:52:45 +08:00
commit 37518e0ca3
19 changed files with 1587 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
syntax = "proto3";
package examples;
option go_package = "examples";
// The greeting service definition.
service BidirectionalGreeter {
// A bidirectional streaming RPC.
//
// Accepts a stream of GreetingRequests and returns a stream of GreetingResponses.
BidirectionalGreeting (stream GreetingRequest) returns (stream GreetingResponse);
}
// The request message containing the user's name.
message GreetingRequest {
string name = 1;
// Add additional request fields here. Consider adding metadata or context.
string request_id = 2; // Example: a unique request identifier
}
// The response message containing the greetings.
message GreetingResponse {
string message = 1;
// Add additional response fields here. Consider adding status information.
string server_timestamp = 2; // Example: timestamp of the server when the response was generated
}
// Example usage comments:
//
// - The BidirectionalGreeting RPC allows the client and server to exchange multiple messages
// in a single connection. This is useful for real-time communication or data streaming.
//
// - The GreetingRequest can include metadata, such as a request ID, to track individual requests
// within the stream.
//
// - The GreetingResponse can include information about the server's processing, such as a timestamp.
//
// - Consider adding error handling and retry mechanisms to your gRPC client to handle potential
// network issues.
//
// - Implement appropriate logging and monitoring to track the performance of your gRPC service.
//
// - For production environments, enable TLS for secure communication between the client and server.
//
// - Implement interceptors for logging, authentication, and other cross-cutting concerns.

View File

@@ -0,0 +1,27 @@
syntax = "proto3";
package example;
option go_package = "example.com/grpc-service-generator/examples";
// The service definition.
service StreamingService {
// Sends a greeting
rpc ClientStreamingExample (stream ClientStreamingRequest) returns (ClientStreamingResponse) {}
}
// The request message containing the user's name.
message ClientStreamingRequest {
string message = 1; // The message from the client. Can represent chunks of data.
}
// The response message containing the greetings
message ClientStreamingResponse {
string result = 1; // The aggregated result based on the client's stream.
}
// Instructions:
// 1. Define your request and response messages
// 2. Define the RPC service, using the stream keyword for streaming RPCs
// 3. Implement the server and client code
// 4. Remember to handle errors and closing the stream gracefully.

View File

@@ -0,0 +1,38 @@
syntax = "proto3";
package example;
option go_package = "example.com/grpc-service-generator/examples";
// Define the service
service StreamingService {
// Server-side streaming RPC. The client sends a single request, and the
// server responds with a stream of messages.
rpc ServerStreamingExample (StreamingRequest) returns (stream StreamingResponse) {}
}
// The request message for the ServerStreamingExample RPC.
message StreamingRequest {
string request_id = 1; // A unique identifier for the request.
int32 num_responses = 2; // The number of responses the server should send.
string message_prefix = 3; // A prefix to add to each response message.
}
// The response message for the ServerStreamingExample RPC.
message StreamingResponse {
string response_id = 1; // A unique identifier for the response.
string message = 2; // The message content.
}
// Example usage notes:
//
// - The `request_id` field in `StreamingRequest` can be used for logging and
// correlation.
// - The `num_responses` field allows the client to control the number of
// messages received. Consider adding a maximum limit to prevent resource exhaustion.
// - The `message_prefix` field demonstrates how to parameterize the server's
// response. This could be used to customize the response based on user preferences.
// - The `response_id` field in `StreamingResponse` allows for identifying individual messages in the stream.
// - Consider adding error handling to the server implementation to gracefully
// handle situations where the client disconnects prematurely.
// - For production, consider adding authentication and authorization to the service.

View File

@@ -0,0 +1,28 @@
// examples/unary_rpc.proto
//
// This file defines a simple gRPC service with a unary RPC.
//
// To compile this .proto file:
// protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative examples/unary_rpc.proto
syntax = "proto3";
package example;
option go_package = "github.com/example/grpc-service-generator/example"; // Replace with your actual Go package
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}