mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 15:14:08 +00:00
added serve example
This commit is contained in:
parent
21daa0a16a
commit
43cc8217ee
5 changed files with 58 additions and 11 deletions
|
@ -20,7 +20,7 @@ pub fn build(b: *std.build.Builder) !void {
|
||||||
}{
|
}{
|
||||||
.{ .name = "hello", .src = "examples/hello/hello.zig" },
|
.{ .name = "hello", .src = "examples/hello/hello.zig" },
|
||||||
.{ .name = "routes", .src = "examples/routes/routes.zig" },
|
.{ .name = "routes", .src = "examples/routes/routes.zig" },
|
||||||
// .{ .name = "serve", .src = "examples/serve/serve.zig" },
|
.{ .name = "serve", .src = "examples/serve/serve.zig" },
|
||||||
}) |excfg| {
|
}) |excfg| {
|
||||||
const ex_name = excfg.name;
|
const ex_name = excfg.name;
|
||||||
const ex_src = excfg.src;
|
const ex_src = excfg.src;
|
||||||
|
|
11
examples/serve/index.html
Normal file
11
examples/serve/index.html
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<h1>My First Heading</h1>
|
||||||
|
|
||||||
|
<p>My first <a href="/two.html">page</a>.</p>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
25
examples/serve/serve.zig
Normal file
25
examples/serve/serve.zig
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const zap = @import("zap");
|
||||||
|
|
||||||
|
fn on_request(r: zap.SimpleRequest) void {
|
||||||
|
// TODO: send 404 response
|
||||||
|
_ = r.sendBody("<html><body><h1>404 - File not found</h1></body></html>");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn main() !void {
|
||||||
|
var listener = zap.SimpleHttpListener.init(.{
|
||||||
|
.port = 3000,
|
||||||
|
.on_request = on_request,
|
||||||
|
.public_folder = std.mem.span("examples/serve"),
|
||||||
|
.log = true,
|
||||||
|
});
|
||||||
|
try listener.listen();
|
||||||
|
|
||||||
|
std.debug.print("Listening on 0.0.0.0:3000\n", .{});
|
||||||
|
|
||||||
|
// start worker threads
|
||||||
|
zap.start(.{
|
||||||
|
.threads = 2,
|
||||||
|
.workers = 2,
|
||||||
|
});
|
||||||
|
}
|
11
examples/serve/two.html
Normal file
11
examples/serve/two.html
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<h1>My SECOND Heading</h1>
|
||||||
|
|
||||||
|
<p>My second <a href="/index.html">page</a>.</p>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
|
@ -44,8 +44,8 @@ pub const SimpleHttpRequestFn = *const fn (SimpleRequest) void;
|
||||||
pub const SimpleHttpListenerSettings = struct {
|
pub const SimpleHttpListenerSettings = struct {
|
||||||
port: usize,
|
port: usize,
|
||||||
interface: [*c]const u8 = null,
|
interface: [*c]const u8 = null,
|
||||||
on_request: SimpleHttpRequestFn,
|
on_request: ?SimpleHttpRequestFn,
|
||||||
public_folder: ?[]u8 = null,
|
public_folder: ?[]const u8 = null,
|
||||||
max_clients: ?u8 = null,
|
max_clients: ?u8 = null,
|
||||||
timeout: ?u8 = null,
|
timeout: ?u8 = null,
|
||||||
log: bool = false,
|
log: bool = false,
|
||||||
|
@ -71,7 +71,7 @@ pub const SimpleHttpListener = struct {
|
||||||
.query = fio2str(r.*.query),
|
.query = fio2str(r.*.query),
|
||||||
.h = r,
|
.h = r,
|
||||||
};
|
};
|
||||||
l.settings.on_request(req);
|
l.settings.on_request.?(req);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,14 +82,14 @@ pub const SimpleHttpListener = struct {
|
||||||
if (self.settings.public_folder) |pf| {
|
if (self.settings.public_folder) |pf| {
|
||||||
pfolder_len = pf.len;
|
pfolder_len = pf.len;
|
||||||
// TODO: make sure it's 0-terminated!!!
|
// TODO: make sure it's 0-terminated!!!
|
||||||
if (pf[pf.len - 1] != 0) {
|
// if (pf[pf.len - 1] != 0) {
|
||||||
return error.ValueNotZeroTerminated;
|
// return error.ValueNotZeroTerminated;
|
||||||
}
|
// }
|
||||||
pfolder = pf.ptr;
|
pfolder = pf.ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
var x: C.http_settings_s = .{
|
var x: C.http_settings_s = .{
|
||||||
.on_request = Self.theOneAndOnlyRequestCallBack,
|
.on_request = if (self.settings.on_request) |_| Self.theOneAndOnlyRequestCallBack else null,
|
||||||
.on_upgrade = null,
|
.on_upgrade = null,
|
||||||
.on_response = null,
|
.on_response = null,
|
||||||
.on_finish = null,
|
.on_finish = null,
|
||||||
|
@ -164,9 +164,9 @@ pub fn listen(port: [*c]const u8, interface: [*c]const u8, settings: ListenSetti
|
||||||
if (settings.public_folder) |pf| {
|
if (settings.public_folder) |pf| {
|
||||||
pfolder_len = pf.len;
|
pfolder_len = pf.len;
|
||||||
// TODO: make sure it's 0-terminated!!!
|
// TODO: make sure it's 0-terminated!!!
|
||||||
if (pf[pf.len - 1] != 0) {
|
// if (pf[pf.len - 1] != 0) {
|
||||||
return error.ValueNotZeroTerminated;
|
// return error.ValueNotZeroTerminated;
|
||||||
}
|
// }
|
||||||
pfolder = pf.ptr;
|
pfolder = pf.ptr;
|
||||||
}
|
}
|
||||||
var x: C.http_settings_s = .{
|
var x: C.http_settings_s = .{
|
||||||
|
|
Loading…
Add table
Reference in a new issue