@@ -20,60 +20,136 @@ A blazingly fast gRPC client & server implementation in Zig, designed for maximu
2020## 🚀 Quick Start
2121
2222``` zig
23- // Server
24- const server = try GrpcServer.init(allocator, 50051, "secret-key");
25- try server.handlers.append(.{
26- .name = "SayHello",
27- .handler_fn = sayHello,
28- });
29- try server.start();
23+ const std = @import("std");
24+ const GrpcServer = @import("grpc-server").GrpcServer;
25+
26+ pub fn main() !void {
27+ var gpa = std.heap.GeneralPurposeAllocator(.{}){};
28+ defer _ = gpa.deinit();
29+ const allocator = gpa.allocator();
30+
31+ // Create and configure server
32+ var server = try GrpcServer.init(allocator, 50051, "secret-key");
33+ defer server.deinit();
3034
31- // Client
32- var client = try GrpcClient.init(allocator, "localhost", 50051);
33- const response = try client.call("SayHello", "World", .none);
35+ // Register handlers
36+ try server.handlers.append(allocator, .{
37+ .name = "SayHello",
38+ .handler_fn = sayHello,
39+ });
40+
41+ // Start server
42+ try server.start();
43+ }
44+
45+ fn sayHello(request: []const u8, allocator: std.mem.Allocator) ![]u8 {
46+ _ = request;
47+ return allocator.dupe(u8, "Hello from gRPC-zig!");
48+ }
3449```
3550
3651## 📚 Examples
3752
3853### Basic Server
3954
55+ See [ examples/basic_server.zig] ( examples/basic_server.zig ) for a complete example.
56+
4057``` zig
4158const std = @import("std");
42- const GrpcServer = @import("server.zig ").GrpcServer;
59+ const GrpcServer = @import("grpc- server").GrpcServer;
4360
4461pub fn main() !void {
4562 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
4663 defer _ = gpa.deinit();
64+ const allocator = gpa.allocator();
4765
48- var server = try GrpcServer.init(gpa. allocator() , 50051, "secret-key");
66+ var server = try GrpcServer.init(allocator, 50051, "secret-key");
4967 defer server.deinit();
5068
5169 try server.start();
5270}
5371```
5472
55- ### Streaming
73+ ### Basic Client
74+
75+ See [ examples/basic_client.zig] ( examples/basic_client.zig ) for a complete example.
5676
5777``` zig
58- var stream = streaming.MessageStream.init(allocator, 5);
59- try stream.push("First message", false);
60- try stream.push("Final message", true);
78+ const std = @import("std");
79+ const GrpcClient = @import("grpc-client").GrpcClient;
80+
81+ pub fn main() !void {
82+ var gpa = std.heap.GeneralPurposeAllocator(.{}){};
83+ defer _ = gpa.deinit();
84+ const allocator = gpa.allocator();
85+
86+ var client = try GrpcClient.init(allocator, "localhost", 50051);
87+ defer client.deinit();
88+
89+ const response = try client.call("SayHello", "World", .none);
90+ defer allocator.free(response);
91+
92+ std.debug.print("Response: {s}\n", .{response});
93+ }
6194```
6295
96+ ### Features
97+
98+ All features are demonstrated in the [ examples/] ( examples/ ) directory:
99+
100+ - ** [ Authentication] ( examples/auth.zig ) ** : JWT token generation and verification
101+ - ** [ Compression] ( examples/compression.zig ) ** : gzip/deflate support
102+ - ** [ Streaming] ( examples/streaming.zig ) ** : Bi-directional message streaming
103+ - ** [ Health Checks] ( examples/health.zig ) ** : Service health monitoring
104+
63105## 🔧 Installation
64106
65- 1 . Fetch the dependency:
107+ ### Option 1: Using zig fetch (Recommended)
108+
109+ 1 . Add the dependency to your project:
66110
67111``` sh
68- zig fetch --save " git+https://ziglana/grpc-zig/ gRPC-zig#main"
112+ zig fetch --save git+https://github.com/ ziglana/gRPC-zig#main
69113```
70114
711152 . Add to your ` build.zig ` :
72116
73117``` zig
74- const grpc_zig = b.dependency("grpc_zig", .{});
118+ const grpc_zig_dep = b.dependency("grpc_zig", .{
119+ .target = target,
120+ .optimize = optimize,
121+ });
122+
123+ // For server development
124+ exe.root_module.addImport("grpc-server", grpc_zig_dep.module("grpc-server"));
125+
126+ // For client development
127+ exe.root_module.addImport("grpc-client", grpc_zig_dep.module("grpc-client"));
128+ ```
129+
130+ 3 . Import in your code:
131+
132+ ``` zig
133+ const GrpcServer = @import("grpc-server").GrpcServer;
134+ const GrpcClient = @import("grpc-client").GrpcClient;
135+ ```
136+
137+ ### Option 2: Manual setup
75138
76- exe.addModule("grpc", grpc_zig.module("grpc"));
139+ Clone the repository and add it to your ` build.zig.zon ` :
140+
141+ ``` zig
142+ .{
143+ .name = "my-project",
144+ .version = "0.1.0",
145+ .dependencies = .{
146+ .grpc_zig = .{
147+ .url = "https://github.com/ziglana/gRPC-zig/archive/refs/heads/main.tar.gz",
148+ // Replace with actual hash after first fetch
149+ .hash = "...",
150+ },
151+ },
152+ }
77153```
78154
79155## 🏃 Performance
@@ -123,6 +199,54 @@ The benchmarks automatically run in CI/CD on every pull request and provide perf
123199
124200📖 ** [ Detailed Benchmarking Guide] ( docs/benchmarking.md ) **
125201
202+ ## 🧪 Testing
203+
204+ ### Unit Tests
205+
206+ Run the unit test suite:
207+
208+ ``` bash
209+ zig build test
210+ ```
211+
212+ The test suite covers:
213+ - Compression algorithms (gzip, deflate, none)
214+ - Benchmark handler functionality
215+ - Core protocol functionality
216+
217+ ### Integration Tests
218+
219+ Run integration tests with a Python client validating the Zig server:
220+
221+ ``` bash
222+ cd integration_test
223+ ./run_tests.sh
224+ ```
225+
226+ Or manually:
227+
228+ ``` bash
229+ # Build and start the test server
230+ zig build integration_test
231+ ./zig-out/bin/grpc-test-server
232+
233+ # In another terminal, run Python tests
234+ cd integration_test
235+ python3 -m venv venv
236+ source venv/bin/activate
237+ pip install -r requirements.txt
238+ python3 test_client.py
239+ ```
240+
241+ The integration tests validate:
242+ - HTTP/2 protocol compliance
243+ - gRPC request/response flow
244+ - Compression functionality
245+ - Health checking
246+ - Authentication integration
247+
248+ 📖 ** [ Integration Test Documentation] ( integration_test/README.md ) **
249+
126250## 🤝 Contributing
127251
128252Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
0 commit comments