Compare commits
3 commits
72e0ad13a2
...
80b93b7437
Author | SHA1 | Date | |
---|---|---|---|
|
80b93b7437 | ||
|
c28a0c9853 | ||
|
575fd30835 |
1 changed files with 22 additions and 48 deletions
70
src/main.zig
70
src/main.zig
|
@ -1,29 +1,11 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const zap = @import("zap");
|
const zap = @import("zap");
|
||||||
const Allocator = std.mem.Allocator;
|
|
||||||
//fn dispatch_routes(r: zap.Request) void {
|
|
||||||
// if (r.path) |the_path| {
|
|
||||||
// std.debug.print("PATH: {s}\n", .{the_path});
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if (r.query) |the_query| {
|
|
||||||
// std.debug.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 {
|
pub const WebRouter = struct {
|
||||||
const Self = @This();
|
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 };
|
return .{ .allocator = allocator };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,37 +43,29 @@ pub fn blog(self: *Self, req: zap.Request) void {
|
||||||
// looking for /blog?post=post_name
|
// looking for /blog?post=post_name
|
||||||
if(req.getParamSlice("post")) |value| {
|
if(req.getParamSlice("post")) |value| {
|
||||||
std.log.info("post name: {s}", .{value});
|
std.log.info("post name: {s}", .{value});
|
||||||
// THIS IS SO DANGEROUS WHY DID I LET THIS THROUGH AHAHAHAHA
|
// TODO: This will need to be updated to look at absolute
|
||||||
const filepath = std.fmt.allocPrint(self.allocator, "src/public/blog/{s}", .{value}) catch return;
|
// filepaths instead, it's a happy accident that this is safe now.
|
||||||
defer self.allocator.free(filepath);
|
const filepath = std.fmt.allocPrint(self.allocator, "./src/public/blog/{s}", .{value}) catch return;
|
||||||
const file_content = std.fs.cwd().readFileAlloc(self.allocator, filepath, std.math.maxInt(usize)) catch return;
|
const dir = std.fs.cwd().openDir("./src/public/blog", .{ .iterate = true }) catch return;
|
||||||
defer self.allocator.free(file_content);
|
var walker = dir.walk(self.allocator) catch return;
|
||||||
req.sendBody(file_content) catch return;
|
defer walker.deinit();
|
||||||
}
|
// I believe that this is safe, since now the post_name now has to be
|
||||||
req.sendBody("ERROR: !") catch return;
|
// 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)) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
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 {
|
fn not_found(req: zap.Request) void {
|
||||||
req.sendBody("404 - Not Found") catch return;
|
req.sendBody("404 - Not Found") catch return;
|
||||||
}
|
}
|
||||||
|
@ -113,7 +87,7 @@ pub fn main() !void {
|
||||||
var listener = zap.HttpListener.init(.{ .port = 4000, .on_request = router.on_request_handler(), .log = true, .max_clients = 100000, .public_folder = "src/public" });
|
var listener = zap.HttpListener.init(.{ .port = 4000, .on_request = router.on_request_handler(), .log = true, .max_clients = 100000, .public_folder = "src/public" });
|
||||||
try listener.listen();
|
try listener.listen();
|
||||||
|
|
||||||
std.debug.print("Listening on 0.0.0.0:4000\n", .{});
|
std.log.info("Listening on 0.0.0.0:4000\n", .{});
|
||||||
|
|
||||||
zap.start(.{
|
zap.start(.{
|
||||||
.threads = 2,
|
.threads = 2,
|
||||||
|
|
Loading…
Reference in a new issue