diff options
| author | physcik <mynameisgennadiy@vk.com> | 2026-04-17 21:21:57 +0500 |
|---|---|---|
| committer | physcik <mynameisgennadiy@vk.com> | 2026-04-17 21:21:57 +0500 |
| commit | 550e0183cf953932fa5c236ad8389febdc899c62 (patch) | |
| tree | 7ba8e6e927822c1edc0166499dbc5925d7aed9fc /backend | |
| parent | e7c7f2ed1211a801ceb5eed32b03a8fb9f791703 (diff) | |
redis connection
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/Makefile | 5 | ||||
| -rw-r--r-- | backend/build.zig | 10 | ||||
| -rw-r--r-- | backend/build.zig.zon | 4 | ||||
| -rw-r--r-- | backend/src/Redis/Connection.zig | 42 | ||||
| -rw-r--r-- | backend/src/main.zig | 11 |
5 files changed, 67 insertions, 5 deletions
diff --git a/backend/Makefile b/backend/Makefile index 5d8bdb4..e0f60e8 100644 --- a/backend/Makefile +++ b/backend/Makefile @@ -3,3 +3,8 @@ all: test: zig build test + +# It is important that this comand only runs on unix machines +deps: + git clone https://github.com/redis/hiredis + cd hiredis; sudo make install diff --git a/backend/build.zig b/backend/build.zig index 648b332..7310e6f 100644 --- a/backend/build.zig +++ b/backend/build.zig @@ -93,21 +93,21 @@ pub fn build(b: *std.Build) void { .target = target, .optimize = optimize, }); - exe.root_module.addImport("httpz", httpz.module("httpz")); - // postgres driver - const pg_module = b.dependency("pg", .{}).module("pg"); - exe.root_module.addImport("pg", pg_module); // .env - const dotenv_module = b.dependency("dotenv", .{}).module("dotenv"); exe.root_module.addImport("dotenv", dotenv_module); + // Redis + exe.root_module.linkSystemLibrary("hiredis", .{ + .needed = true, + }); + // This creates a top level step. Top level steps have a name and can be // invoked by name when running `zig build` (e.g. `zig build run`). // This will evaluate the `run` step rather than the default step. diff --git a/backend/build.zig.zon b/backend/build.zig.zon index 911bb3d..b1423b1 100644 --- a/backend/build.zig.zon +++ b/backend/build.zig.zon @@ -44,6 +44,10 @@ .url = "git+https://github.com/zigster64/dotenv.zig?ref=main#7d50f9fe0ce223d94d180d5a5a49fe0df5bc3743", .hash = "dotenv-0.1.0-Q9TxeuEMAADw6cHl9uKbjwunS0PlYYc1yw9Ol82TwAAm", }, + .okredis = .{ + .url = "git+https://github.com/kristoff-it/zig-okredis?ref=master#f00a50311e5fcc5a688fc027bd0a7ebbe8e2f0cc", + .hash = "okredis-0.1.0-Cg726sgHBAClOCW_xaaqoS6m_uyiw3iD9Qj20AAzGcTL", + }, }, .paths = .{ "build.zig", diff --git a/backend/src/Redis/Connection.zig b/backend/src/Redis/Connection.zig new file mode 100644 index 0000000..bde187b --- /dev/null +++ b/backend/src/Redis/Connection.zig @@ -0,0 +1,42 @@ +const std = @import("std"); +const redis = @cImport( + @cInclude("hiredis/hiredis.h") + ); + +var connection: ?*redis.redisContext = undefined; +const addr: [*]const u8 = "127.0.0.1"; +const port: u16 = 6379; + +pub fn Connect() !void { + connection = redis.redisConnect(addr, port); + if (connection == null or connection.?.err != 0) { + if (connection != null) { + @panic(&connection.?.errstr); + } + @panic("The redis connection failed: general error"); + } +} + +pub fn Disconnect() void { + redis.redisFree(connection); +} + +pub const Message = struct { + Key: []const u8, + Value: []const u8 +}; + +pub fn WriteToTopic(topic: []const u8, message: Message) !void { + _ = redis.redisCommand(connection, "SET %s:%s %s", + topic.ptr, + message.Key.ptr, + message.Value.ptr); +} + +pub fn ReadFromTopic(topic: []const u8, Key: []const u8) !?[]const u8 { + const resp: *redis.redisReply = @alignCast(@ptrCast(redis.redisCommand(connection, "GET %s:%s", + topic.ptr, + Key.ptr))); + return std.mem.span(resp.str); +} + diff --git a/backend/src/main.zig b/backend/src/main.zig index db48a38..d1fbde2 100644 --- a/backend/src/main.zig +++ b/backend/src/main.zig @@ -3,6 +3,7 @@ const backend = @import("backend"); const httpz = @import("httpz"); const db = @import("Database/Connection.zig"); const handler = @import("Handler.zig"); +const redis = @import("Redis/Connection.zig"); pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; @@ -20,6 +21,16 @@ pub fn main() !void { server.deinit(); } + try redis.Connect(); + defer redis.Disconnect(); + + try redis.WriteToTopic("test", redis.Message { + .Key = "xdd", + .Value = "ddx", + }); + + std.debug.print("redis response: {s}\n", .{try redis.ReadFromTopic("test", "xdd") orelse "not found"}); + var router = try server.router(.{}); router.get("/", index, .{}); |
