Also under construction! But this has extra text :)
-2024-11-12: Initial commit.
-2024-11-12: screen command tutorial
- about -diff --git a/src/main.zig b/src/main.zig index b3fc9af..41fd550 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,63 +2,71 @@ const std = @import("std"); const zap = @import("zap"); pub const WebRouter = struct { -const Self = @This(); -allocator: std.mem.Allocator, + const Self = @This(); + allocator: std.mem.Allocator, -pub fn init(allocator: std.mem.Allocator) Self { - return .{ .allocator = allocator }; -} - -pub fn index(self: *Self, req: zap.Request) void { - const string = std.fmt.allocPrint( - self.allocator, - "Test", - .{}, -) catch return; - defer self.allocator.free(string); - req.sendFile("src/public/index.html") catch return; -} - -pub fn home(self: *Self, req: zap.Request) void { - const string = std.fmt.allocPrint( - self.allocator, - "HOME!!!", - .{}, -) catch return; - defer self.allocator.free(string); - req.sendBody(string) catch return; -} - -pub fn blog(self: *Self, req: zap.Request) void { - req.parseBody() catch |err| { - std.log.err("parse error: {any}", .{err}); - }; - req.parseQuery(); - // looking for /blog?post=post_name - if(req.getParamSlice("post")) |value| { - // TODO: This will need to be updated to look at absolute - // filepaths instead, it's a happy accident that this is safe now. - const filepath = std.fmt.allocPrint(self.allocator, "./src/public/blog/{s}", .{value}) catch return; - const dir = std.fs.cwd().openDir("./src/public/blog", .{ .iterate = true }) catch return; - var walker = dir.walk(self.allocator) catch return; - defer walker.deinit(); - // I believe that this is safe, since now the post_name now has to be - // equal to one of the entries within the specified directory - while (walker.next() catch return) |entry| { - if(std.mem.eql(u8,entry.path,value)) { - const file_content = std.fs.cwd().readFileAlloc(self.allocator, filepath, std.math.maxInt(usize)) catch return; - defer self.allocator.free(file_content); - req.sendBody(file_content) catch return; - } - } - - req.sendBody("ERROR: You shouldn't be looking here.") catch return; + pub fn init(allocator: std.mem.Allocator) Self { + return .{ .allocator = allocator }; + } + + pub fn index(self: *Self, req: zap.Request) void { + const file_content = std.fs.cwd().readFileAlloc(self.allocator, "src/public/index.html", std.math.maxInt(usize)) catch return; + defer self.allocator.free(file_content); + req.sendBody(file_content) catch return; + } + + pub fn home(self: *Self, req: zap.Request) void { + const file_content = std.fs.cwd().readFileAlloc(self.allocator, "src/public/home.html", std.math.maxInt(usize)) catch return; + defer self.allocator.free(file_content); + req.sendBody(file_content) catch return; + } + + pub fn about(self: *Self, req: zap.Request) void { + const file_content = std.fs.cwd().readFileAlloc(self.allocator, "src/public/about.html", std.math.maxInt(usize)) catch return; + defer self.allocator.free(file_content); + req.sendBody(file_content) catch return; + } + + pub fn contact(self: *Self, req: zap.Request) void { + const file_content = std.fs.cwd().readFileAlloc(self.allocator, "src/public/contact.html", std.math.maxInt(usize)) catch return; + defer self.allocator.free(file_content); + req.sendBody(file_content) catch return; + } + + pub fn blog(self: *Self, req: zap.Request) void { + req.parseBody() catch |err| { + std.log.err("parse error: {any}", .{err}); + }; + req.parseQuery(); + // looking for /blog?post=post_name + if (req.getParamSlice("post")) |value| { + // TODO: This will need to be updated to look at absolute + // filepaths instead, it's a happy accident that this is safe now. + const filepath = std.fmt.allocPrint(self.allocator, "./src/public/blog/{s}", .{value}) catch return; + const dir = std.fs.cwd().openDir("./src/public/blog", .{ .iterate = true }) catch return; + var walker = dir.walk(self.allocator) catch return; + defer walker.deinit(); + // I believe that this is safe, since now the post_name now has to be + // equal to one of the entries within the specified directory + while (walker.next() catch return) |entry| { + if (std.mem.eql(u8, entry.path, value)) { + const file_content = std.fs.cwd().readFileAlloc(self.allocator, filepath, std.math.maxInt(usize)) catch return; + defer self.allocator.free(file_content); + std.log.info("sending file contents",.{}); + req.sendBody(file_content) catch return; + } + } + + req.sendBody("ERROR: You shouldn't be looking here.") catch return; + } + else { + req.sendFile("src/public/blog.html") catch return; + } + req.sendBody("ERROR: Request for post is invalid.") catch return; } - req.sendBody("ERROR: Request for post is invalid.") catch return; -} }; -fn not_found(req: zap.Request) void { +fn notFound(req: zap.Request) void { req.sendBody("404 - Not Found") catch return; } @@ -68,12 +76,14 @@ pub fn main() !void { }){}; const allocator = gpa.allocator(); var router = zap.Router.init(allocator, .{ - .not_found = not_found, + .not_found = notFound, }); defer router.deinit(); var web_router = WebRouter.init(allocator); try router.handle_func("/home", &web_router, &WebRouter.home); + try router.handle_func("/about", &web_router, &WebRouter.about); try router.handle_func("/index", &web_router, &WebRouter.index); + try router.handle_func("/contact", &web_router, &WebRouter.contact); try router.handle_func("/blog", &web_router, &WebRouter.blog); try router.handle_func("/", &web_router, &WebRouter.index); var listener = zap.HttpListener.init(.{ .port = 4000, .on_request = router.on_request_handler(), .log = true, .max_clients = 100000, .public_folder = "src/public" }); diff --git a/src/public/about.html b/src/public/about.html new file mode 100644 index 0000000..31e7acc --- /dev/null +++ b/src/public/about.html @@ -0,0 +1,2 @@ +
Also under construction! But this has extra text :)
-2024-11-12: Initial commit.
-2024-11-12: screen command tutorial
- about -