Skip to content

Commit

Permalink
refactor: project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
sectasy0 committed May 10, 2024
1 parent 9fefb2c commit b37c9d4
Show file tree
Hide file tree
Showing 26 changed files with 320 additions and 397 deletions.
21 changes: 11 additions & 10 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const std = @import("std");
const builtin = @import("builtin");

const server = @import("server/listener.zig");
const Config = @import("server/config.zig");
const MemoryStorage = @import("server/storage.zig");
const cli = @import("server/cli.zig");
const Logger = @import("server/logger.zig");
const log = @import("server/logger.zig");

const TracingAllocator = @import("server/tracing.zig");
const persistance = @import("server/persistance.zig");

const Employer = @import("server/employer.zig");
const persistance = @import("server/storage/persistance.zig");
const Memory = @import("server/storage/memory.zig");

const server = @import("server/network/listener.zig");

const Employer = @import("server/processing/employer.zig");

pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, ret_addr: ?usize) noreturn {
@setCold(true);
Expand Down Expand Up @@ -70,7 +71,7 @@ pub fn main() void {

handle_arguments(result.args) orelse return;

var logger = Logger.init(
var logger = log.Logger.init(
allocator,
result.args.@"log-path",
result.args.sout,
Expand Down Expand Up @@ -113,14 +114,14 @@ pub fn main() void {
defer _ = dbgpa.deinit();

var tracing = TracingAllocator.init(dbgpa.allocator());
var mem_storage = MemoryStorage.init(
var memory = Memory.init(
tracing.allocator(),
config,
&persister,
);
defer mem_storage.deinit();
defer memory.deinit();

persister.load(&mem_storage) catch |err| {
persister.load(&memory) catch |err| {
if (err != error.FileNotFound) {
logger.log(
.Warning,
Expand All @@ -133,7 +134,7 @@ pub fn main() void {
const context = Employer.Context{
.config = &config,
.logger = &logger,
.storage = &mem_storage,
.memory = &memory,
};

run_supervisor(allocator, context);
Expand Down
4 changes: 2 additions & 2 deletions src/protocol/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub const ZType = union(enum) {
array: array,
null: void,
// ClientError only for compatibility with ProtocolHandler
// and it will not be stored in MemoryStorage but will be returned
// and it will not be stored in Memory but will be returned
err: ClientError,

pub const array = std.ArrayList(ZType);
Expand Down Expand Up @@ -51,7 +51,7 @@ pub fn ztype_copy(value: ZType, allocator: std.mem.Allocator) anyerror!ZType {

return .{ .map = result };
},
// we do not implement for err because errors wont be stored in MemoryStorage
// we do not implement for err because errors wont be stored in Memory
else => return error.UnsuportedType,
}

Expand Down
39 changes: 0 additions & 39 deletions src/server/access_control.zig

This file was deleted.

2 changes: 1 addition & 1 deletion src/server/logger.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const std = @import("std");
const utils = @import("utils.zig");

const Logger = @This();
pub const Logger = @This();

pub const DEFAULT_PATH: []const u8 = "./logs/zcached.log";
const MAX_FILE_SIZE: usize = 30 * 1048576; // 30Mb in binary.
Expand Down
47 changes: 47 additions & 0 deletions src/server/middleware/access.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const std = @import("std");

const Logger = @import("../logger.zig");
const Config = @import("../config.zig");
const utils = @import("../utils.zig");

pub const AccessMiddleware = @This();

logger: *Logger,
config: *const Config,

pub fn init(config: *const Config, logger: *Logger) AccessMiddleware {
return AccessMiddleware{ .logger = logger, .config = config };
}

// Check whether a new connection, represented by the provided network address, can be established.
pub fn verify(self: AccessMiddleware, address: std.net.Address) !void {
try self.check_whitelist(address);
}

// Checks whether the provided network address is whitelisted.
// If whitelisting is enabled (whitelist capacity is greater than 0) and the given address is not whitelisted.
fn check_whitelist(
self: AccessMiddleware,
address: std.net.Address,
) !void {
if (self.config.whitelist.capacity > 0 and !is_whitelisted(
self.config.whitelist,
address,
)) {
self.logger.log(
.Info,
"* connection from {any} is not whitelisted, rejected",
.{address},
);

return error.NotPermitted;
}
}

// Checks if the provided network address is present in the given whitelist.
fn is_whitelisted(whitelist: std.ArrayList(std.net.Address), addr: std.net.Address) bool {
for (whitelist.items) |whitelisted| {
if (std.meta.eql(whitelisted.any.data[2..].*, addr.any.data[2..].*)) return true;
}
return false;
}
File renamed without changes.
24 changes: 13 additions & 11 deletions src/server/listener.zig → src/server/network/listener.zig
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
const std = @import("std");

const Worker = @import("worker.zig");
const StreamServer = @import("stream_server.zig");
const Connection = @import("connection.zig");
const MemoryStorage = @import("storage.zig");
const Context = @import("employer.zig").Context;
const StreamServer = @import("../network/stream_server.zig");
const Connection = @import("../network/connection.zig");

const AccessControl = @import("access_control.zig");
const RequestProcessor = @import("request_processor.zig");
const Worker = @import("../processing/worker.zig");
const Context = @import("../processing/employer.zig").Context;
const requests = @import("../processing/requests.zig");

const utils = @import("utils.zig");
const Logger = @import("logger.zig");
const Memory = @import("../storage/memory.zig");

const AccessMiddleware = @import("../middleware/access.zig");

const utils = @import("../utils.zig");
const Logger = @import("../logger.zig");

const DEFAULT_CLIENT_BUFFER: usize = 4096;

Expand Down Expand Up @@ -144,7 +146,7 @@ fn handle_incoming(self: *Listener, worker: *Worker) AcceptResult {
},
};

const access_control = AccessControl.init(self.context.config, self.context.logger);
const access_control = AccessMiddleware.init(self.context.config, self.context.logger);
access_control.verify(incoming.address) catch return .{
.err = .{
.etype = error.NotPermitted,
Expand Down Expand Up @@ -285,7 +287,7 @@ fn handle_request(self: *const Listener, worker: *Worker, connection: *Connectio

connection.position = 0;

var processor = RequestProcessor.init(
var processor = requests.Processor.init(
worker.allocator,
self.context,
);
Expand Down
File renamed without changes.
Loading

0 comments on commit b37c9d4

Please sign in to comment.