Updated: general clean up and a warning for the future

This commit is contained in:
Michal Skorczak 2024-11-13 22:57:29 +00:00
parent 575fd30835
commit c28a0c9853

View file

@ -1,29 +1,11 @@
const std = @import("std");
const zap = @import("zap");
const Allocator = std.mem.Allocator;
//fn dispatch_routes(r: zap.Request) void {
// if (r.path) |the_path| {
// std.log.print("PATH: {s}\n", .{the_path});
// }
//
// if (r.query) |the_query| {
// std.log.print("QUERY: {s}\n", .{the_query});
// }
// if (r.path) |path| {
// if (routes.get(path)) |method| {
// method(r);
// return;
// }
// }
// r.setStatus(.not_found);
// r.sendBody("404 - File not found") catch return;
//}
pub const WebRouter = struct {
const Self = @This();
allocator: Allocator,
allocator: std.mem.Allocator,
pub fn init(allocator: Allocator) Self {
pub fn init(allocator: std.mem.Allocator) Self {
return .{ .allocator = allocator };
}
@ -61,11 +43,14 @@ pub fn blog(self: *Self, req: zap.Request) void {
// looking for /blog?post=post_name
if(req.getParamSlice("post")) |value| {
std.log.info("post name: {s}", .{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| {
std.log.info("entry: {s}", .{entry.path});
if(std.mem.eql(u8,entry.path,value)) {
@ -75,48 +60,13 @@ pub fn blog(self: *Self, req: zap.Request) void {
}
}
// defer self.allocator.free(filepath);
// const file_content = std.fs.cwd().readFileAlloc(self.allocator, filepath, std.math.maxInt(usize)) catch return;
// defer self.allocator.free(file_content);
// const absolute_filepath = std.fs.cwd().realpathAlloc(self.allocator, filepath) catch return;
// defer self.allocator.free(absolute_filepath);
// std.log.info("absolute_filepath: {s}\n", .{absolute_filepath});
// var walker = dir.walk(self.allocator) catch return;
// defer walker.deinit();
// while (walker.next() catch return) |entry| {
// const abs_p = std.fs.cwd().realpathAlloc(self.allocator, entry.path) catch return;
// defer self.allocator.free(abs_p);
// std.log.info("abs_entry: {s}", .{abs_p});
// std.log.info("entry: {s}", .{entry.path});
// }
// std.log.info("pwd: {s}", .{std.fs.cwd().realpathAlloc(self.allocator, ".") catch return});
// req.sendBody(file_content) catch return;
req.sendBody("ERROR: You shouldn't be looking here.") catch return;
}
req.sendBody("ERROR: You shouldn't be looking here.") catch return;
req.sendBody("ERROR: Request for post is invalid.") catch return;
}
};
//pub fn blog(self: *Self, req: zap.Request) void {
// std.log.warn("blog", .{});
// const template =
// \\ {{=<< >>=}}
// \\ * Files:
// \\ <<#files>>
// \\ <<<& name>> (<<name>>)
// \\ <</files>>
// ;
// var mustache = Mustache.fromData(template) catch return;
// defer mustache.deinit();
//}
//fn route_git() void {}
//fn route_blog() void {}
//fn route_resume() void {}
//fn route_contact() void {}
fn not_found(req: zap.Request) void {
req.sendBody("404 - Not Found") catch return;
}