summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorphyscik <mynameisgennadiy@vk.com>2026-05-08 15:11:51 +0500
committerphyscik <mynameisgennadiy@vk.com>2026-05-08 15:11:51 +0500
commit41aad458b1876d37e52274b2921739928d7bf83c (patch)
tree755462c88cb0cad5550adebba81159c6e533e2d2
parent877973ea93cbe917e835c5a5d9880f9e6922ab47 (diff)
Backend settings fileHEADmaster
-rw-r--r--.gitignore1
-rw-r--r--backend/Dockerfile16
-rw-r--r--backend/Makefile3
-rw-r--r--backend/Settings.json17
-rw-r--r--backend/src/Redis/Connection.zig12
-rw-r--r--backend/src/Settings.zig78
-rw-r--r--backend/src/main.zig12
7 files changed, 133 insertions, 6 deletions
diff --git a/.gitignore b/.gitignore
index 7bc9cf6..8ea6c64 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,4 @@ zig-pkg
hiredis
.env
+app
diff --git a/backend/Dockerfile b/backend/Dockerfile
new file mode 100644
index 0000000..80cfa8c
--- /dev/null
+++ b/backend/Dockerfile
@@ -0,0 +1,16 @@
+FROM alpine:3.22.4
+RUN apk update
+
+# Installing zig
+# RUN apk add zig=0.15.2-r0
+
+# Installing redis
+RUN apk add redis redis-openrc git build-base make
+# RUN rc-update add redis default
+RUN git clone https://github.com/redis/hiredis.git && cd hiredis && make install
+
+WORKDIR /app
+COPY ./app /app/app
+# RUN make build
+EXPOSE 6969
+CMD ["./app"]
diff --git a/backend/Makefile b/backend/Makefile
index e0f60e8..a592749 100644
--- a/backend/Makefile
+++ b/backend/Makefile
@@ -1,6 +1,9 @@
all:
zig build test && zig build run
+build:
+ zig build && cp zig-out/bin/backend app
+
test:
zig build test
diff --git a/backend/Settings.json b/backend/Settings.json
new file mode 100644
index 0000000..c403505
--- /dev/null
+++ b/backend/Settings.json
@@ -0,0 +1,17 @@
+{
+ "Port": 6969,
+ "CorsParams": {
+ "Origin": "*",
+ "Methods": "GET,POST,PUT,DELETE,OPTIONS",
+ "Headers": "authorization,content-type"
+ },
+ "RedisParams": {
+ "Address": "127.0.0.1",
+ "Port": 6379
+ },
+ "DbParams": {
+ "Address": "localhost",
+ "Port": 5432
+ }
+}
+
diff --git a/backend/src/Redis/Connection.zig b/backend/src/Redis/Connection.zig
index a69b9ec..360ae44 100644
--- a/backend/src/Redis/Connection.zig
+++ b/backend/src/Redis/Connection.zig
@@ -1,4 +1,5 @@
const std = @import("std");
+const settings = @import("../Settings.zig");
const redis = @cImport(
@cInclude("hiredis/hiredis.h")
);
@@ -8,7 +9,10 @@ const addr: [*]const u8 = "127.0.0.1";
const port: u16 = 6379;
pub fn Connect() !void {
- connection = redis.redisConnect(addr, port);
+ connection = redis.redisConnect(
+ settings.Current.RedisParams.Address.ptr,
+ settings.Current.RedisParams.Port);
+
if (connection == null or connection.?.err != 0) {
if (connection != null) {
@panic(&connection.?.errstr);
@@ -57,6 +61,11 @@ pub fn ReadFromTopic(allocator: std.mem.Allocator, topic: []const u8, Key: []con
}
test "Redis connection" {
+ const alloc = std.testing.allocator;
+
+ try settings.ReadConfig(alloc);
+ defer settings.Deinit(alloc);
+
try Connect();
defer Disconnect();
@@ -66,7 +75,6 @@ test "Redis connection" {
.SecondsToLive = 1,
});
- const alloc = std.testing.allocator;
var resp = try ReadFromTopic(alloc, "test", "key");
try std.testing.expectEqualStrings("value", resp orelse "not found");
diff --git a/backend/src/Settings.zig b/backend/src/Settings.zig
new file mode 100644
index 0000000..142a662
--- /dev/null
+++ b/backend/src/Settings.zig
@@ -0,0 +1,78 @@
+const std = @import("std");
+
+const SettingsFile = "Settings.json";
+
+pub const errors = error {
+ Uninitialized
+};
+
+pub const SettingsStruct = struct {
+ Port: u16,
+
+ CorsParams: struct {
+ Origin: []const u8,
+ Methods: []const u8,
+ Headers: []const u8,
+ },
+
+ RedisParams: struct {
+ Address: []const u8,
+ Port: u16,
+ },
+
+ DbParams: struct {
+ Address: []const u8,
+ Port: u16,
+ },
+};
+
+pub var Current: *SettingsStruct = undefined;
+
+pub fn ReadConfig(allocator: std.mem.Allocator) !void {
+ const file = try std.fs.cwd().openFile(SettingsFile, .{});
+ defer file.close();
+
+ const contents = try file.readToEndAlloc(allocator, std.math.maxInt(usize));
+ defer allocator.free(contents);
+
+ var parsed = std.json.parseFromSlice(
+ SettingsStruct,
+ allocator,
+ contents, .{}) catch |err| {
+ std.debug.print("Failed to parse settings: {any}\n", .{err});
+ return err;
+ };
+
+ defer parsed.deinit();
+
+ // There is no reason to free this value, it will be freed by OS at the end of the application
+ Current = try allocator.create(SettingsStruct);
+ Current.* = .{
+ .Port = parsed.value.Port,
+
+ .CorsParams = .{
+ .Origin = try allocator.dupe(u8, parsed.value.CorsParams.Origin),
+ .Methods = try allocator.dupe(u8, parsed.value.CorsParams.Methods),
+ .Headers = try allocator.dupe(u8, parsed.value.CorsParams.Headers),
+ },
+
+ .RedisParams = .{
+ .Address = try allocator.dupe(u8, parsed.value.RedisParams.Address),
+ .Port = parsed.value.RedisParams.Port,
+ },
+
+ .DbParams = .{
+ .Address = try allocator.dupe(u8, parsed.value.DbParams.Address),
+ .Port = parsed.value.DbParams.Port,
+ },
+ };
+}
+
+pub fn Deinit(allocator: std.mem.Allocator) void {
+ allocator.free(Current.CorsParams.Origin);
+ allocator.free(Current.CorsParams.Methods);
+ allocator.free(Current.CorsParams.Headers);
+ allocator.free(Current.RedisParams.Address);
+ allocator.free(Current.DbParams.Address);
+ allocator.destroy(Current);
+}
diff --git a/backend/src/main.zig b/backend/src/main.zig
index 86e84cb..6d5f96c 100644
--- a/backend/src/main.zig
+++ b/backend/src/main.zig
@@ -5,11 +5,15 @@ const db = @import("Database/Connection.zig");
const handler = @import("Handler.zig");
const tokens = @import("Authentication/Tokens.zig");
const redis = @import("Redis/Connection.zig");
+const settings = @import("Settings.zig");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
+ try settings.ReadConfig(allocator);
+ defer settings.Deinit(allocator);
+
try tokens.Init();
// PSQL connection
@@ -23,13 +27,13 @@ pub fn main() !void {
};
var server = try httpz.Server(*handler.Handler).init(allocator, .{
- .address = .all(6969),
+ .address = .all(settings.Current.Port),
}, &httpHandler);
const cors = try server.middleware(httpz.middleware.Cors, .{
- .origin = "*",
- .methods = "GET,POST,PUT,DELETE,OPTIONS",
- .headers = "authorization,content-type",
+ .origin = settings.Current.CorsParams.Origin,
+ .methods = settings.Current.CorsParams.Methods,
+ .headers = settings.Current.CorsParams.Headers,
});
defer {