Updated: all of the functionality is working, content is the next thing to update
This commit is contained in:
parent
d1fba53891
commit
94c92cf910
6 changed files with 90 additions and 107 deletions
66
src/main.zig
66
src/main.zig
|
@ -2,40 +2,44 @@ const std = @import("std");
|
|||
const zap = @import("zap");
|
||||
|
||||
pub const WebRouter = struct {
|
||||
const Self = @This();
|
||||
allocator: std.mem.Allocator,
|
||||
const Self = @This();
|
||||
allocator: std.mem.Allocator,
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator) Self {
|
||||
pub fn init(allocator: std.mem.Allocator) Self {
|
||||
return .{ .allocator = allocator };
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
pub fn index(self: *Self, req: zap.Request) void {
|
||||
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;
|
||||
}
|
||||
pub fn home(self: *Self, req: zap.Request) void {
|
||||
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 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| {
|
||||
std.log.err("parse error: {any}", .{err});
|
||||
};
|
||||
req.parseQuery();
|
||||
// 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
|
||||
// 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;
|
||||
|
@ -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
|
||||
// equal to one of the entries within the specified directory
|
||||
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;
|
||||
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
2
src/public/about.html
Normal file
|
@ -0,0 +1,2 @@
|
|||
<h2>About me:</h2>
|
||||
Here is some information about me!
|
|
@ -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>
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
<h1>HOME</h1>
|
||||
<h2>Home page</h2>
|
||||
Here is the home page!
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in a new issue