Added: can now send file contents back as the body with http parameters
This commit is contained in:
parent
95a4beca29
commit
f6408bb16c
3 changed files with 106 additions and 42 deletions
66
src/main.zig
66
src/main.zig
|
@ -20,51 +20,72 @@ const Allocator = std.mem.Allocator;
|
||||||
//}
|
//}
|
||||||
|
|
||||||
pub const WebRouter = struct {
|
pub const WebRouter = struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
allocator: Allocator,
|
allocator: Allocator,
|
||||||
|
|
||||||
pub fn init(allocator: Allocator) Self {
|
pub fn init(allocator: 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 {
|
||||||
std.log.warn("index", .{});
|
std.log.warn("index", .{});
|
||||||
|
|
||||||
const string = std.fmt.allocPrint(
|
const string = std.fmt.allocPrint(
|
||||||
self.allocator,
|
self.allocator,
|
||||||
"Test",
|
"Test",
|
||||||
.{},
|
.{},
|
||||||
) catch return;
|
) catch return;
|
||||||
defer self.allocator.free(string);
|
defer self.allocator.free(string);
|
||||||
req.sendFile("src/public/index.html") catch return;
|
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 {
|
||||||
std.log.warn("home", .{});
|
std.log.warn("home", .{});
|
||||||
|
|
||||||
const string = std.fmt.allocPrint(
|
const string = std.fmt.allocPrint(
|
||||||
self.allocator,
|
self.allocator,
|
||||||
"HOME!!!",
|
"HOME!!!",
|
||||||
.{},
|
.{},
|
||||||
) catch return;
|
) catch return;
|
||||||
defer self.allocator.free(string);
|
defer self.allocator.free(string);
|
||||||
req.sendBody(string) catch return;
|
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});
|
||||||
|
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();
|
|
||||||
//}
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//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_git() void {}
|
||||||
//fn route_blog() void {}
|
//fn route_blog() void {}
|
||||||
//fn route_resume() void {}
|
//fn route_resume() void {}
|
||||||
|
@ -86,6 +107,7 @@ pub fn main() !void {
|
||||||
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("/index", &web_router, &WebRouter.index);
|
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);
|
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" });
|
||||||
try listener.listen();
|
try listener.listen();
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
</a>
|
</a>
|
||||||
<nav>
|
<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="/home" hx-trigger="load">-->
|
||||||
<!--<a href="/" hx-target="#content" hx-swap="innerHTML" hx-get="/about">about</a>-->
|
<!--<a href="/" hx-target="#content" hx-swap="innerHTML" hx-get="/about">about</a>-->
|
||||||
<a href="/git/explore/repos">git</a>
|
<a href="/git/explore/repos">git</a>
|
||||||
<a href="/blog.html">blog</a>
|
<a href="/blog.html">blog</a>
|
||||||
|
@ -24,6 +24,7 @@
|
||||||
<p>Also under construction! But this has extra text :)</p>
|
<p>Also under construction! But this has extra text :)</p>
|
||||||
<p>2024-11-12: Initial commit.</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>
|
<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>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
41
src/public/blog/test.txt
Normal file
41
src/public/blog/test.txt
Normal 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
|
Loading…
Reference in a new issue