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

@ -10,23 +10,27 @@ pub fn init(allocator: std.mem.Allocator) Self {
}
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;
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 string = std.fmt.allocPrint(
self.allocator,
"HOME!!!",
.{},
) catch return;
defer self.allocator.free(string);
req.sendBody(string) catch return;
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 {
@ -48,17 +52,21 @@ pub fn blog(self: *Self, req: zap.Request) void {
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;
}
};
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" });

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>
<meta charset="utf-8">
<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>
<h2>Blog posts:</h2>
<a href="" hx-target="#content" hx-swap="innerHTML" hx-get="/blog?post=first_blog_post.html">first blog post</a>

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