122 lines
3.5 KiB
Zig
122 lines
3.5 KiB
Zig
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.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 {
|
|
const Self = @This();
|
|
allocator: Allocator,
|
|
|
|
pub fn init(allocator: Allocator) Self {
|
|
return .{ .allocator = allocator };
|
|
}
|
|
|
|
pub fn index(self: *Self, req: zap.Request) void {
|
|
std.log.warn("index", .{});
|
|
|
|
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 {
|
|
std.log.warn("home", .{});
|
|
|
|
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();
|
|
const param_count = req.getParamCount();
|
|
std.log.info("param_count: {}", .{param_count});
|
|
// looking for /blog?post=post_name
|
|
if(req.getParamSlice("post")) |value| {
|
|
std.log.info("post name: {s}", .{value});
|
|
// THIS IS SO DANGEROUS WHY DID I LET THIS THROUGH AHAHAHAHA
|
|
const filepath = std.fmt.allocPrint(self.allocator, "src/public/blog/{s}", .{value}) catch return;
|
|
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);
|
|
req.sendBody(file_content) catch return;
|
|
}
|
|
req.sendBody("ERROR: !") 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;
|
|
}
|
|
|
|
pub fn main() !void {
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{
|
|
.thread_safe = true,
|
|
}){};
|
|
const allocator = gpa.allocator();
|
|
var router = zap.Router.init(allocator, .{
|
|
.not_found = not_found,
|
|
});
|
|
defer router.deinit();
|
|
var web_router = WebRouter.init(allocator);
|
|
try router.handle_func("/home", &web_router, &WebRouter.home);
|
|
try router.handle_func("/index", &web_router, &WebRouter.index);
|
|
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" });
|
|
try listener.listen();
|
|
|
|
std.debug.print("Listening on 0.0.0.0:4000\n", .{});
|
|
|
|
zap.start(.{
|
|
.threads = 2,
|
|
.workers = 1, // 1 worker enables sharing state between threads
|
|
});
|
|
}
|