Updated: all of the functionality is working, content is the next thing to update

This commit is contained in:
Michal Skorczak 2024-12-06 17:42:34 +00:00
parent d1fba53891
commit 94c92cf910
6 changed files with 90 additions and 107 deletions

View file

@ -2,40 +2,44 @@ const std = @import("std");
const zap = @import("zap"); const zap = @import("zap");
pub const WebRouter = struct { pub const WebRouter = struct {
const Self = @This(); const Self = @This();
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
pub fn init(allocator: std.mem.Allocator) Self { pub fn init(allocator: std.mem.Allocator) Self {
return .{ .allocator = allocator }; return .{ .allocator = allocator };
} }
pub fn index(self: *Self, req: zap.Request) void { pub fn index(self: *Self, req: zap.Request) void {
const string = std.fmt.allocPrint( const file_content = std.fs.cwd().readFileAlloc(self.allocator, "src/public/index.html", std.math.maxInt(usize)) catch return;
self.allocator, defer self.allocator.free(file_content);
"Test", req.sendBody(file_content) catch return;
.{}, }
) catch return;
defer self.allocator.free(string);
req.sendFile("src/public/index.html") catch return;
}
pub fn home(self: *Self, req: zap.Request) void { pub fn home(self: *Self, req: zap.Request) void {
const string = std.fmt.allocPrint( const file_content = std.fs.cwd().readFileAlloc(self.allocator, "src/public/home.html", std.math.maxInt(usize)) catch return;
self.allocator, defer self.allocator.free(file_content);
"HOME!!!", req.sendBody(file_content) catch return;
.{}, }
) catch return;
defer self.allocator.free(string);
req.sendBody(string) catch return;
}
pub fn blog(self: *Self, req: zap.Request) void { 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| { req.parseBody() catch |err| {
std.log.err("parse error: {any}", .{err}); std.log.err("parse error: {any}", .{err});
}; };
req.parseQuery(); req.parseQuery();
// looking for /blog?post=post_name // looking for /blog?post=post_name
if(req.getParamSlice("post")) |value| { if (req.getParamSlice("post")) |value| {
// TODO: This will need to be updated to look at absolute // TODO: This will need to be updated to look at absolute
// filepaths instead, it's a happy accident that this is safe now. // 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 filepath = std.fmt.allocPrint(self.allocator, "./src/public/blog/{s}", .{value}) catch return;
@ -45,20 +49,24 @@ pub fn blog(self: *Self, req: zap.Request) void {
// I believe that this is safe, since now the post_name now has to be // 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 // equal to one of the entries within the specified directory
while (walker.next() catch return) |entry| { while (walker.next() catch return) |entry| {
if(std.mem.eql(u8,entry.path,value)) { if (std.mem.eql(u8, entry.path, value)) {
const file_content = std.fs.cwd().readFileAlloc(self.allocator, filepath, std.math.maxInt(usize)) catch return; const file_content = std.fs.cwd().readFileAlloc(self.allocator, filepath, std.math.maxInt(usize)) catch return;
defer self.allocator.free(file_content); defer self.allocator.free(file_content);
std.log.info("sending file contents",.{});
req.sendBody(file_content) 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;
} }
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; req.sendBody("404 - Not Found") catch return;
} }
@ -68,12 +76,14 @@ pub fn main() !void {
}){}; }){};
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var router = zap.Router.init(allocator, .{ var router = zap.Router.init(allocator, .{
.not_found = not_found, .not_found = notFound,
}); });
defer router.deinit(); defer router.deinit();
var web_router = WebRouter.init(allocator); var web_router = WebRouter.init(allocator);
try router.handle_func("/home", &web_router, &WebRouter.home); 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("/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("/blog", &web_router, &WebRouter.blog);
try router.handle_func("/", &web_router, &WebRouter.index); 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" }); var listener = zap.HttpListener.init(.{ .port = 4000, .on_request = router.on_request_handler(), .log = true, .max_clients = 100000, .public_folder = "src/public" });

2
src/public/about.html Normal file
View file

@ -0,0 +1,2 @@
<h2>About me:</h2>
Here is some information about me!

View file

@ -1,30 +1,2 @@
<head> <h2>Blog posts:</h2>
<meta charset="utf-8"> <a href="" hx-target="#content" hx-swap="innerHTML" hx-get="/blog?post=first_blog_post.html">first blog post</a>
<meta name="viewport" content="width=device-width, inital-scale=1">
<meta http-equiv="X-UR-Compatible" content='ie=edge'>
<title>mskor.xyz</title>
<meta name="keywords" content="">
<link rel="stylesheet" id="css" href="style.css">
<script src="https://unpkg.com/htmx.org@2.0.3" integrity="sha384-0895/pl2MU10Hqc6jd4RvrthNlDiE9U1tWmX7WRESftEDRosgxNsQG/Ze9YMRzHq" crossorigin="anonymous"></script>
</head>
<body>
<div id="header">
<a href="/" hx-target="#content" hx-swap="innerHTML" hx-get="/home">
<h1>mskor.xyz</h1>
</a>
<nav>
<!--<a href="" hx-target="#content" hx-swap="innerHTML" hx-get="/home" hx-trigger="load">-->
<!--<a href="/" hx-target="#content" hx-swap="innerHTML" hx-get="/about">about</a>-->
<a href="/git/explore/repos">git</a>
<a href="/blog.html">blog</a>
</nav>
</div>
<div id="content">
<p>Also under construction! But this has extra text :)</p>
<p>2024-11-12: Initial commit.</p>
<p>2024-11-12: <a href="https://www.howtogeek.com/662422/how-to-use-linuxs-screen-command/">screen command tutorial</a></p>
<a href="/" hx-target="#content" hx-swap="innerHTML" hx-get="/blog?post=test.txt">about</a>
</div>
</body>
</html>

View file

@ -1 +1,2 @@
<h1>HOME</h1> <h2>Home page</h2>
Here is the home page!

View file

@ -1,3 +1,4 @@
<html>
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, inital-scale=1"> <meta name="viewport" content="width=device-width, inital-scale=1">
@ -9,19 +10,16 @@
</head> </head>
<body> <body>
<div id="header"> <div id="header">
<a href="/" hx-target="#content" hx-swap="innerHTML" hx-get="/home"> <a href="/" hx-target="#content" hx-swap="innerHTML" hx-get="/home" hx-trigger="load">
<h1>mskor.xyz</h1> <h1>mskor.xyz</h1>
</a> </a>
<nav> <nav>
<a href="" hx-target="#content" hx-swap="innerHTML" hx-get="/about">about</a>
<!--<a href="" hx-target="#content" hx-swap="innerHTML" hx-get="/home" hx-trigger="load">--!> <a href="https://mskor.xyz/git/explore/repos">git</a>
<!--<a href="/" hx-target="#content" hx-swap="innerHTML" hx-get="/about">about</a>--> <a href="" hx-target="#content" hx-swap="innerHTML" hx-get="/blog">blog</a>
<a href="/git/explore/repos">git</a> <a href="" hx-target="#content" hx-swap="innerHTML" hx-get="/contact">contact</a>
<a href="/blog.html">blog</a>
</nav> </nav>
</div> </div>
<div id="content"> <div id="content"></div>
Under construction!
</div>
</body> </body>
</html> </html>