Provide abstraction for headers

Basic abstraction wrapping existing std.http.Headers to provide a stable
interface.
This commit is contained in:
Bob Farrell 2024-02-15 20:01:07 +00:00
parent 3ba070a72a
commit 7958480ff7
7 changed files with 43 additions and 12 deletions

View File

@ -27,8 +27,8 @@ If you are interested in _Jetzig_ you will probably find these tools interesting
* :white_check_mark: Cookies. * :white_check_mark: Cookies.
* :white_check_mark: Error handling. * :white_check_mark: Error handling.
* :white_check_mark: Static content from /public directory. * :white_check_mark: Static content from /public directory.
* :x: Static content generation. * :white_check_mark: Headers (available but not yet wrapped).
* :x: Headers (available but not yet wrapped). * :x: Static content generation (nearly there!).
* :x: Param/JSON payload parsing/abstracting. * :x: Param/JSON payload parsing/abstracting.
* :x: Development-mode responses for debugging. * :x: Development-mode responses for debugging.
* :x: Environment configurations (develompent/production/etc.) * :x: Environment configurations (develompent/production/etc.)

View File

@ -4,7 +4,7 @@ pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
const template_path = b.option([]const u8, "zmpl_templates_path", "Path to templates") orelse "src/app/views/"; const template_path = b.option([]const u8, "zmpl_templates_path", "Path to templates") orelse "src/app/views/";
const manifest : []const u8 = b.pathJoin(&.{ template_path, "zmpl.manifest.zig" }); const manifest: []const u8 = b.pathJoin(&.{ template_path, "zmpl.manifest.zig" });
const lib = b.addStaticLibrary(.{ const lib = b.addStaticLibrary(.{
.name = "jetzig", .name = "jetzig",
@ -24,13 +24,13 @@ pub fn build(b: *std.Build) !void {
b.installArtifact(exe); b.installArtifact(exe);
const jetzig_module = b.addModule("jetzig",.{ .root_source_file = .{ .path = "src/jetzig.zig" } }); const jetzig_module = b.addModule("jetzig", .{ .root_source_file = .{ .path = "src/jetzig.zig" } });
exe.root_module.addImport("jetzig", jetzig_module); exe.root_module.addImport("jetzig", jetzig_module);
lib.root_module.addImport("jetzig", jetzig_module); lib.root_module.addImport("jetzig", jetzig_module);
const zmpl_dep = b.dependency( const zmpl_dep = b.dependency(
"zmpl", "zmpl",
.{ .target = target, .optimize = optimize, .zmpl_templates_path = template_path, .zmpl_manifest_path = manifest}, .{ .target = target, .optimize = optimize, .zmpl_templates_path = template_path, .zmpl_manifest_path = manifest },
); );
lib.root_module.addImport("zmpl", zmpl_dep.module("zmpl")); lib.root_module.addImport("zmpl", zmpl_dep.module("zmpl"));
@ -90,7 +90,10 @@ pub const CompileViewsStep = struct {
.makeFn = &make, .makeFn = &make,
}); });
const compile_step_view = owner.allocator.create(CompileViewsStep) catch @panic("Out of memory"); const compile_step_view = owner.allocator.create(CompileViewsStep) catch @panic("Out of memory");
compile_step_view.* = .{ .step = step, .template_path = options.template_path, }; compile_step_view.* = .{
.step = step,
.template_path = options.template_path,
};
return compile_step_view; return compile_step_view;
} }

View File

@ -162,5 +162,5 @@ pub fn base64Decode(allocator: std.mem.Allocator, string: []const u8) ![]const u
} }
test { test {
@import("std").testing.refAllDeclsRecursive(@This()); @import("std").testing.refAllDecls(@This());
} }

View File

@ -7,4 +7,5 @@ pub const Request = @import("http/Request.zig");
pub const Response = @import("http/Response.zig"); pub const Response = @import("http/Response.zig");
pub const Session = @import("http/Session.zig"); pub const Session = @import("http/Session.zig");
pub const Cookies = @import("http/Cookies.zig"); pub const Cookies = @import("http/Cookies.zig");
pub const Headers = @import("http/Headers.zig");
pub const status_codes = @import("http/status_codes.zig"); pub const status_codes = @import("http/status_codes.zig");

View File

@ -0,0 +1,30 @@
const std = @import("std");
allocator: std.mem.Allocator,
headers: std.http.Headers,
const Self = @This();
pub fn init(allocator: std.mem.Allocator, headers: std.http.Headers) Self {
return .{ .allocator = allocator, .headers = headers };
}
pub fn getFirstValue(self: *Self, key: []const u8) ?[]const u8 {
return self.headers.getFirstValue(key);
}
pub fn append(self: *Self, key: []const u8, value: []const u8) !void {
try self.headers.append(key, value);
}
test {
const allocator = std.testing.allocator;
var headers = std.http.Headers.init(allocator);
defer headers.deinit();
try headers.append("foo", "bar");
var jetzig_headers = Self.init(allocator, headers);
try std.testing.expectEqualStrings(
headers.getFirstValue("foo").?,
jetzig_headers.getFirstValue("foo").?,
);
}

View File

@ -12,7 +12,7 @@ pub const Format = enum { HTML, JSON, UNKNOWN };
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
path: []const u8, path: []const u8,
method: Method, method: Method,
headers: std.http.Headers, headers: jetzig.http.Headers,
segments: std.ArrayList([]const u8), segments: std.ArrayList([]const u8),
server: *jetzig.http.Server, server: *jetzig.http.Server,
session: *jetzig.http.Session, session: *jetzig.http.Session,
@ -68,7 +68,7 @@ pub fn init(
.allocator = allocator, .allocator = allocator,
.path = response.request.target, .path = response.request.target,
.method = method, .method = method,
.headers = response.request.headers, .headers = jetzig.http.Headers.init(allocator, response.request.headers),
.server = server, .server = server,
.segments = segments, .segments = segments,
.cookies = cookies, .cookies = cookies,

View File

@ -1,7 +1,4 @@
// const Cookies = @import("http/Cookies.zig");
test { test {
_ = @import("jetzig.zig"); _ = @import("jetzig.zig");
_ = @import("zmpl");
@import("std").testing.refAllDeclsRecursive(@This()); @import("std").testing.refAllDeclsRecursive(@This());
} }