Remake basic main.zig handler

This commit is contained in:
jaina heartles 2022-07-09 15:07:51 -07:00
parent c1c5e3014d
commit a93039534c

View file

@ -1,43 +1,27 @@
const std = @import("std"); const std = @import("std");
const util = @import("util"); const http = @import("http");
pub const db = @import("./db.zig"); //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");
}
pub fn main() anyerror!void { pub fn main() anyerror!void {
var srv = std.net.StreamServer.init(.{ .reuse_address = true }); var srv = try http.Server.listen(std.net.Address.parseIp("0.0.0.0", 8080) catch unreachable);
defer srv.deinit(); defer srv.shutdown();
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);
while (true) { 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 const alloc = fba.allocator();
_ = async http.handleConnection(std.heap.page_allocator, conn, struct { var ctx = try srv.accept(alloc);
fn func(ctx: *const http.Context) !void { defer ctx.close();
return testHandler(ctx);
} const headers = http.Headers.init(alloc);
}.func);
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;
}