add global_allocator

This commit is contained in:
Luna 2021-04-13 21:31:58 -03:00
parent 11079f2c4d
commit 6a1f9a7a34
1 changed files with 9 additions and 6 deletions

View File

@ -7,7 +7,8 @@ const fmt = std.fmt;
const images_dir_path = "./images";
var registry: mimetypes.Registry = undefined;
var registry: ?mimetypes.Registry = null;
var global_allocator: ?*std.mem.Allocator = null;
pub fn main() anyerror!void {
std.log.info("welcome to webscale", .{});
@ -16,8 +17,8 @@ pub fn main() anyerror!void {
defer _ = gpa.deinit();
registry = mimetypes.Registry.init(std.heap.page_allocator);
defer registry.deinit();
try registry.load();
defer registry.?.deinit();
try registry.?.load();
// TODO: configurable addr via env var
const bind_addr = try std.net.Address.parseIp("0.0.0.0", 8080);
@ -26,6 +27,8 @@ pub fn main() anyerror!void {
// TODO: configurable path via env var
try std.fs.cwd().makePath(images_dir_path);
global_allocator = &gpa.allocator;
try http.listenAndServe(
&gpa.allocator,
bind_addr,
@ -66,7 +69,7 @@ const ContentDisposition = struct {
const Self = @This();
pub fn deinit(self: *Self) void {
pub fn deinit(self: *const Self) void {
self.allocator.free(self.name);
self.allocator.free(self.filename);
}
@ -92,7 +95,7 @@ const Part = struct {
const Self = @This();
pub fn deinit(self: *Self) void {
pub fn deinit(self: *const Self) void {
self.disposition.deinit();
self.allocator.free(self.content_type);
}
@ -168,7 +171,7 @@ const Multipart = struct {
var content_disposition: ?ContentDisposition = null;
var content_type: ?[]const u8 = null;
std.log.debug("next bytes: {any}", .{self.stream.buffer[self.stream.pos..(self.stream.pos + 50)]});
std.log.debug("next bytes: {s}", .{self.stream.buffer[self.stream.pos..(self.stream.pos + 50)]});
while (try parser.next()) |event| {
std.log.debug("got event: {}", .{event});