Remake basic main.zig handler
This commit is contained in:
parent
c1c5e3014d
commit
a93039534c
1 changed files with 18 additions and 34 deletions
|
@ -1,43 +1,27 @@
|
|||
const std = @import("std");
|
||||
const util = @import("util");
|
||||
const http = @import("http");
|
||||
|
||||
pub const db = @import("./db.zig");
|
||||
pub const http = @import("./http.zig");
|
||||
pub const routing = @import("./routing.zig");
|
||||
|
||||
pub const Uuid = util.Uuid;
|
||||
pub const ciutf8 = util.ciutf8;
|
||||
|
||||
fn testHandler(ctx: *const http.Context) !void {
|
||||
const stream = try ctx.response.open(.ok);
|
||||
defer stream.close();
|
||||
|
||||
try stream.writer().writeAll("Index page");
|
||||
}
|
||||
//const db = @import("./db.zig");
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
var srv = std.net.StreamServer.init(.{ .reuse_address = true });
|
||||
defer srv.deinit();
|
||||
|
||||
const uuid = try Uuid.parse("f75f5160-12d3-42c2-a81d-ad2245b7a74b");
|
||||
std.log.debug("{}", .{uuid});
|
||||
|
||||
try srv.listen(std.net.Address.parseIp("0.0.0.0", 8080) catch unreachable);
|
||||
var srv = try http.Server.listen(std.net.Address.parseIp("0.0.0.0", 8080) catch unreachable);
|
||||
defer srv.shutdown();
|
||||
|
||||
while (true) {
|
||||
const conn = try srv.accept();
|
||||
var buf: [1 << 20]u8 = undefined;
|
||||
var fba = std.heap.FixedBufferAllocator.init(&buf);
|
||||
|
||||
// todo: keep track of connections
|
||||
_ = async http.handleConnection(std.heap.page_allocator, conn, struct {
|
||||
fn func(ctx: *const http.Context) !void {
|
||||
return testHandler(ctx);
|
||||
}
|
||||
}.func);
|
||||
const alloc = fba.allocator();
|
||||
var ctx = try srv.accept(alloc);
|
||||
defer ctx.close();
|
||||
|
||||
const headers = http.Headers.init(alloc);
|
||||
|
||||
var stream = try ctx.openResponse(&headers, .ok);
|
||||
const writer = stream.writer();
|
||||
defer stream.close();
|
||||
try writer.print("Page for {s}", .{ctx.request.path});
|
||||
|
||||
try stream.finish();
|
||||
}
|
||||
}
|
||||
|
||||
test {
|
||||
_ = http;
|
||||
_ = util;
|
||||
_ = routing;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue