From 4d6b86ea3841de7606f69d01eefebaeab6113540 Mon Sep 17 00:00:00 2001 From: jxnshi Date: Sat, 14 Dec 2024 13:14:52 +0000 Subject: [PATCH] Add repo tab --- public/404.html | 2 +- public/layout.html | 3 +- src/main.zig | 130 +++++++++++++++++++++++++++++---------------- 3 files changed, 87 insertions(+), 48 deletions(-) diff --git a/public/404.html b/public/404.html index 8d03dc1..1d70c63 100644 --- a/public/404.html +++ b/public/404.html @@ -1 +1 @@ -

Lost in outter space.

+

On dirait que vous vous êtes perdu.

diff --git a/public/layout.html b/public/layout.html index f28b7f1..614c137 100644 --- a/public/layout.html +++ b/public/layout.html @@ -13,8 +13,9 @@

Projets

diff --git a/src/main.zig b/src/main.zig index ca1c9bf..d6f66fb 100644 --- a/src/main.zig +++ b/src/main.zig @@ -30,6 +30,8 @@ fn handleClient(parent_allocator: std.mem.Allocator, connection: std.net.Server. defer layout_buffer.deinit(); defer temp_buffer.deinit(); + const temp_buffer_writer = temp_buffer.writer(); + var server = std.http.Server.init(connection, &read_buffer); const cwd = std.fs.cwd(); @@ -62,6 +64,7 @@ fn handleClient(parent_allocator: std.mem.Allocator, connection: std.net.Server. break :blk stat.kind == .file; }; + // Serve file. if (is_file) blk: { const file = public_dir.openFile(route, .{}) catch |err| { std.log.err("Could not open file \"{s}\" with error {}", .{ route, err }); @@ -72,7 +75,10 @@ fn handleClient(parent_allocator: std.mem.Allocator, connection: std.net.Server. const file_reader = file.reader(); try file_reader.readAllArrayList(&send_buffer, std.math.maxInt(usize)); - } else blk: { + } + + // Serve route. + else blk: { { // Layout file. var layout_file = public_dir.openFile("layout.html", .{}) catch { std.log.err("Could not open layout file.", .{}); @@ -87,74 +93,106 @@ fn handleClient(parent_allocator: std.mem.Allocator, connection: std.net.Server. var maybe_index_file: ?std.fs.File = null; - index_file: { - route_dir: { - var is_public: bool = undefined; - var route_dir: std.fs.Dir = undefined; - - if (route.len == 0) { - route_dir = public_dir; - is_public = true; - } else { - route_dir = public_dir.openDir(route, .{}) catch break :route_dir; - is_public = false; - } + // Handle repos. + if (std.mem.startsWith(u8, route, "repos")) { + const sub_route = route["repos".len..]; - defer { - if (!is_public) { - route_dir.close(); - } - } + // Show all repos. + if (sub_route.len == 0) { + var git_dir = try std.fs.openDirAbsolute("/srv/git/", .{ + .iterate = true, + }); - maybe_index_file = route_dir.openFile("index.html", .{}) catch break :route_dir; + defer git_dir.close(); - break :index_file; - } + var git_iter = git_dir.iterate(); + + try temp_buffer_writer.writeAll("\n"); + } - if (public_dir.openFile(path_ext.items, .{})) |index_file| { - maybe_index_file = index_file; - } else |_| { + // Show specific repo. + else { // } } - if (maybe_index_file) |index_file| { - defer index_file.close(); + // Handle file index. + else { + index_file: { + route_dir: { + var route_dir: std.fs.Dir = undefined; + var is_public: bool = undefined; + + if (route.len == 0) { + route_dir = public_dir; + is_public = true; + } else { + route_dir = public_dir.openDir(route, .{}) catch break :route_dir; + is_public = false; + } + + defer { + if (!is_public) { + route_dir.close(); + } + } + + maybe_index_file = route_dir.openFile("index.html", .{}) catch break :route_dir; + + break :index_file; + } - const index_file_reader = index_file.reader(); - try index_file_reader.readAllArrayList(&temp_buffer, std.math.maxInt(usize)); - } else no_index_file: { - md_file: { var path_ext = std.ArrayList(u8).init(allocator); defer path_ext.deinit(); try path_ext.appendSlice(route); - try path_ext.appendSlice(".md"); + try path_ext.appendSlice(".html"); + + if (public_dir.openFile(path_ext.items, .{})) |index_file| { + maybe_index_file = index_file; + } else |_| { + // + } + } - var md_file = public_dir.openFile(path_ext.items, .{}) catch break :md_file; - defer md_file.close(); + if (maybe_index_file) |index_file| { + defer index_file.close(); - const md_file_reader = md_file.reader(); + const index_file_reader = index_file.reader(); + try index_file_reader.readAllArrayList(&temp_buffer, std.math.maxInt(usize)); + } else no_index_file: { + md_file: { + var path_ext = std.ArrayList(u8).init(allocator); + defer path_ext.deinit(); - const temp_buffer_writer = temp_buffer.writer(); + try path_ext.appendSlice(route); + try path_ext.appendSlice(".md"); - try markdownToHtml(temp_buffer_writer.any(), md_file_reader.any()); + var md_file = public_dir.openFile(path_ext.items, .{}) catch break :md_file; + defer md_file.close(); - break :no_index_file; - } + const md_file_reader = md_file.reader(); + + try markdownToHtml(temp_buffer_writer.any(), md_file_reader.any()); - var not_found_file = public_dir.openFile("404.html", .{}) catch break :blk; - defer not_found_file.close(); + break :no_index_file; + } + + // 404 Not Found. + var not_found_file = public_dir.openFile("404.html", .{}) catch break :blk; + defer not_found_file.close(); - const not_found_file_reader = not_found_file.reader(); + const not_found_file_reader = not_found_file.reader(); - try not_found_file_reader.readAllArrayList(&temp_buffer, std.math.maxInt(usize)); + try not_found_file_reader.readAllArrayList(&temp_buffer, std.math.maxInt(usize)); + } } try send_buffer.resize(layout_buffer.items.len + temp_buffer.items.len - 3); -- 2.49.0