-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathbasic_server.zig
More file actions
42 lines (35 loc) · 1.12 KB
/
basic_server.zig
File metadata and controls
42 lines (35 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const std = @import("std");
const GrpcServer = @import("grpc").GrpcServer;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var server = try GrpcServer.init(allocator, 50051, "secret-key");
defer server.deinit();
// Register handlers
try server.handlers.append(
allocator,
.{
.name = "SayHello",
.handler_fn = sayHello,
},
);
// Register benchmark handler
try server.handlers.append(
allocator,
.{
.name = "Benchmark",
.handler_fn = benchmarkHandler,
},
);
try server.start();
}
fn sayHello(request: []const u8, allocator: std.mem.Allocator) ![]u8 {
_ = request;
return allocator.dupe(u8, "Hello from gRPC!");
}
fn benchmarkHandler(request: []const u8, allocator: std.mem.Allocator) ![]u8 {
// Echo the request back with a timestamp for benchmarking
const response = try std.fmt.allocPrint(allocator, "Echo: {s} (processed at {d})", .{ request, std.time.milliTimestamp() });
return response;
}