Compare commits

..

No commits in common. "265fefb035cfaa45eb3b0954777cc03bc5082537" and "d1fba53891e0647dbd4e8da7249a31e5a5fe5193" have entirely different histories.

12 changed files with 152 additions and 201 deletions

View file

@ -2,44 +2,40 @@ 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 file_content = std.fs.cwd().readFileAlloc(self.allocator, "src/public/index.html", std.math.maxInt(usize)) catch return; const string = std.fmt.allocPrint(
defer self.allocator.free(file_content); self.allocator,
req.sendBody(file_content) catch return; "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 { 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; const string = std.fmt.allocPrint(
defer self.allocator.free(file_content); self.allocator,
req.sendBody(file_content) catch return; "HOME!!!",
} .{},
) catch return;
defer self.allocator.free(string);
req.sendBody(string) catch return;
}
pub fn about(self: *Self, req: zap.Request) void { pub fn blog(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;
@ -49,24 +45,20 @@ pub const WebRouter = struct {
// 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 notFound(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;
} }
@ -76,14 +68,12 @@ 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 = notFound, .not_found = not_found,
}); });
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" });

View file

@ -1,15 +0,0 @@
<h3>about:</h3>
<p>Hello, I am Michal Skorczak, a Polish software engineer based in the UK.</p>
<p>Fan of all things free (as in beer) and free (as in freedom);
<b>especially</b> when it comes to computers.</p>
<p>Currently working on
<a href="https://mskor.xyz/git/explore/repos">various projects</a> but
primarily on integrating <a href="https://github.com/zigzap/zap">zap</a>
with <a href="https://github.com/vrischmann/zig-sqlite">sqlite</a> for a
simple to use API that I can use in my other projects.</p>
<p>Outside of my software (and soon hardware) projects, I can be found spending
my time playing guitar, painting Orks, reading and trying to run a
Minecraft server.</p>

View file

@ -1,3 +1,30 @@
<h3>blog posts:</h3> <head>
<p><a href="" hx-target="#content" hx-swap="innerHTML" hx-get="/blog?post=first_blog_post.html">first blog post</a></p> <meta charset="utf-8">
<p><a href="" hx-target="#content" hx-swap="innerHTML" hx-get="/blog?post=todoweb.html">creating todoweb</a></p> <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

@ -0,0 +1,3 @@
<h3>blog 1</h3>
<a href="" hx-get="/blog" hx-target="#content" hx-swap="innerHTML">back</a>
heres some test text for blog 1

View file

@ -0,0 +1,3 @@
<h3>blog 2</h3>
<a href="" hx-get="/blog" hx-target="#content" hx-swap="innerHTML">back</a>
heres some test text for blog 2

View file

@ -0,0 +1,3 @@
<h3>blog title</h3>
<a href="" hx-get="/blog" hx-target="#content" hx-swap="innerHTML">back</a>
heres some test text

View file

@ -1,5 +0,0 @@
<h3>blog 1</h3>
<p>This is the first blog post on mskor.xyz!</p>
<p>Although this is the first, I'll mostly be testing out formatting and
functionality here, so expect frequent and wild changes!</p>
<a href="" hx-get="/blog" hx-target="#content" hx-swap="innerHTML">back</a>

41
src/public/blog/test.txt Normal file
View file

@ -0,0 +1,41 @@
<h3>blog 1</h3>
<a href="" hx-get="/blog" hx-target="#content" hx-swap="innerHTML">back</a>
heres some test text for blog 1
khjsdfg
;'sdfgkl;hjdfgskl'
;gsdf
jkl;'sdfgl;
jfgsdjl;
sdfgl;'
jgsdfjl'
sdfgjl;
sdfgjl;
sdfg;
jlfgasd
jl;sdfgAJL;
ASFGD
JL;AGFSDJL;
ASGDFJL;
GASDFJL;
ADFGJL;
AFGDJL;
AFGL;
JAGFJL;
AGFD
JL;AGF
JL;AFGD
JL;AGFSJL;
GA
SD;FLKGJAS
;LGKASJL;
A;
LGJ
JL;AJ
LGJ
LAJL;
FGJL;ADFJL;G
AJ
LDSFJL;
JL;
JLL;
J

View file

@ -1,87 +0,0 @@
<h3>creating todoweb</h3>
<p><i>06/12/2024 18:00</i></p>
<p>With an interview fast approaching and the prospect of having to talk about
a project I've created looming; it's best that I actually create something
and am able to talk about the design process.</p>
<p>So despite this site being able to one of those projects, something with
more <i>flair</i> would be helpful and by flair I mean, it does more than just
serve HTML back.</p>
<p>So the following will be the documented process of how I went about creating
<b>TODOWEB</b>.</p>
<p><i>What I hope to create:</i></p>
<p>I'm thinking, that having a web application that allows for users to login
and create updates to their own personal todo lists would be a good place
to start. Extensions from this should come later (such as giving them an
API to pull data from). Therefore, the following functionality needs to
exist:</p>
<ul>
<li>Allow the user to login with credentials.</li>
<li>Allow the user to read their previously made todo list(s)</li>
<li>Allow the user to create, delete and update todo list(s) and their
items</li>
</ul>
<p>From here I'm making the following assumptions/list of tasks:</p>
<ul>
<li>Create a database that I can send commands to.</li>
<li>Have a front end that can display todo list(s) and their items</li>
<li>Create a login page with sessions so that the user doesn't need to
constantly login to see their updates.</li>
</ul>
<p>This really doesn't seem to bad.</p>
<p>My best bet is the just start building at least one of these things
(it'll be the database as that's going to be the most annoying) and then
come back and update this page as the updates to the project progress.</p>
<p>However, the tech stack I intend to use for this (and how I've already set
up the project) will be writing the backend in Zig (using Zap) as a
framework, SQLITE as the database engine and then HTMX as the
front-end.</p>
<p>Hopefully, this will be <b><i>BLAZINGLY FAST</i></b></p>
<p><i>06/12/2024 18:37</i></p>
<p>I need to be on the most up to date version of Zig (master) in order to make
use of all of these fancy libraries, I'll just try compiling from source
and see how long that takes on this machine. <b>Surely</b>, nothing will
go wrong!</p>
<p><i>06/12/2024 18:45</i></p>
<p><b>I LOVE ZIG SO MUCH!!!!!!</b> Being able to just download the binary and
have everything work is <b>HOW IT SHOULD BE.</b> This also means that I can
go back and use the other version of the library, no compiling LLVM for me!
At least this time...</p>
<p><i>06/12/2024 18:55</i></p>
<p>They work! Both Zap and the zig-sqlite libraries load and their demo versions
are functional! This now means that I can start on trying to get some kind
of base plate down! I say that, but I must refer back to what exactly I want
to accomplish otherwise I'll lose sight. Right now, I think designing the
DB to handle user logins is going to be my best bet. However, once this is
done, a lot of the other parts should fall into place as if I can register,
login and show user data on a page; then having linked data to a user should
be a breeze!</p>
<p>This is really begining to sound and look like rambling, but I know that I'd
end up loving reading something like this, reminds me of The Martian (2015)
with the video logs, lame example and thing to reference but that's where
I've gone!</p>
<p><i>12/12/2024 22:36</i></p>
<p>I had worked on this last night but had forgotten to write an update, whilst
everything appears to be working (<i>zig build run</i> works), it seems that
I need to do a lot more reading on how Zig really works.</p>
<p>(This also reminds me that I need to add code blocks into this site, but for
now just making references to things will do)</p>
<p>With the way that `sqlite.Db` works, what I ideally want to do is wrap it
within my own struct that I can then call a `.get()` method on for the zap
user authentication. The Lookup for the Authenticator will need to mimic
a hashmap, (which is what it uses in the example code). So in theory, it
should just be a case of having that `.get()` method return a []const
u8.</p>
<p>But it is with this wrapping that I am encountering issues, probably best
that I read up, not on the syntax that would make it work but <i>HOW</i>
Zig wants me to think about it.</p>
<p>I'd encountered something similar when doing a technical test in React,
the syntax was just Javascript so that wasn't the issue, but the ideas and
philosophy of <i>HOW</i> these things work together (I've never used States
before) was the missing link!</p>
<<p><i>Sources:</i></p>
<ul>
<li><a href="https://github.com/vrischmann/zig-sqlite">zig-sqlite</a></li>
<li><a href="https://github.com/vrischmann/zig-sqlite-demo">zig-sqlite-demo</a></li>
<li><a href="https://github.com/zigzap/zap/">zigzap</a></li>
<li><a href="https://www.youtube.com/watch?v=YD1rlE5tp_0">The Martian (2015) reference</a></li>
</ul>

View file

@ -1,3 +0,0 @@
<h3>contact:</h3>
<p>Email: mskorczak1@gmail.com. (for now...)</p>
<p><a href="https://www.linkedin.com/in/mskorczak/">LinkedIn</a></p>

View file

@ -1,9 +1 @@
<p>Welcome to <b>mskor.xyz</b>!</p> <h1>HOME</h1>
<p>This page consists of:</p>
<ul>
<li><a href="" hx-target="#content" hx-swap="innerHTML" hx-get="/about">who I am</a></li>
<li><a href="https://mskor.xyz/git/explore/repos">my projects</a></li>
<li><a href="" hx-target="#content" hx-swap="innerHTML" hx-get="/blog">my writing (ramblings)</a></li>
<li><a href="" hx-target="#content" hx-swap="innerHTML" hx-get="/contact">ways to contact me!</a></li>
</ul>
<p>Enjoy your stay!</p>

View file

@ -1,4 +1,3 @@
<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">
@ -10,16 +9,19 @@
</head> </head>
<body> <body>
<div id="header"> <div id="header">
<a href="/" hx-target="#content" hx-swap="innerHTML" hx-get="/home" hx-trigger="load"> <a href="/" hx-target="#content" hx-swap="innerHTML" hx-get="/home">
<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="https://mskor.xyz/git/explore/repos">git</a> <!--<a href="" hx-target="#content" hx-swap="innerHTML" hx-get="/home" hx-trigger="load">--!>
<a href="" hx-target="#content" hx-swap="innerHTML" hx-get="/blog">blog</a> <!--<a href="/" hx-target="#content" hx-swap="innerHTML" hx-get="/about">about</a>-->
<a href="" hx-target="#content" hx-swap="innerHTML" hx-get="/contact">contact</a> <a href="/git/explore/repos">git</a>
<a href="/blog.html">blog</a>
</nav> </nav>
</div> </div>
<div id="content"></div> <div id="content">
Under construction!
</div>
</body> </body>
</html> </html>