Protoc-gen-httpgo is a powerful plugin that creates native HTTP server and client code directly from your Protobuf files. It offers a lightweight, high-performance alternative to grpc-gateway, allowing for direct HTTP serving while eliminating proxy overhead. With support for dual-stack, various marshaling options, and minimal memory usage, it streamlines API development.
protoc-gen-httpgo is a powerful protoc plugin designed to generate native HTTP server and client code directly from your Protocol Buffers (proto) files. This tool serves as a lightweight, high-performance alternative to grpc-gateway, allowing direct HTTP serving from applications without the overhead typically associated with proxy layers.
The plugin demonstrates significant performance gains compared to grpc-gateway:
encoding/json.To use this plugin, define RPC methods in your proto files. For example:
import "google/api/annotations.proto";
service TestService {
rpc TestMethod (TestMessage) returns (TestMessage) {
option (google.api.http) = {
get: "/v1/test/{field1}"
};
}
}
message TestMessage {
string field1 = 1;
string field2 = 2;
}
To generate code from your proto definition, run:
protoc -I=. --httpgo_out=paths=source_relative:. example/proto/example.proto
Explore parameters for customization in code generation, focusing on options such as marshaller, only, and library for precise control over functionality.
Custom middleware can be incorporated to log requests, manage timeouts, or enforce headers. For example, middleware definitions can facilitate request logging:
func LoggerServerMiddleware(ctx context.Context, arg any, next func(ctx context.Context, arg any) (resp any, err error)) (resp any, err error) {
log.Println("server request", arg)
resp, err = next(ctx, arg)
log.Println("server response", resp)
return resp, err
}
Future updates will include the implementation of additional web servers, dependency management tools, websocket support, and enhanced middleware examples to ensure applicability across various production environments.
To view example implementations and additional aspects of the library, please check the example directory. This plugin provides comprehensive tools to facilitate seamless HTTP server and client code generation from Protocol Buffers, enhancing both performance and developer productivity.
No comments yet.
Sign in to be the first to comment.