Emil Lerch
dcf00d8460
All checks were successful
AWS-Zig Build / build-zig-0.11.0-amd64-host (push) Successful in 1m50s
This commit is a significant refactor that fixes a number of things. 1. Replaces the optional helpers import (which was always weird) with a mandatory interface import on behalf of the application. This is actually a good thing as it enables all things below. 2. Removes the severely awkward union that was the lambda context. Now, no matter how your handler runs, a single object with everything you need is fully populated and (nearly always) works as you would expect. There is a slight exception to this with AWS Lambda that is related to the service itself. It is also possible that not everything is passed in correctly for Cloudflare, which, if true, will be addressed later. 3. Allows writes to the context object. These will be added to the output, but is implementation dependent, and I'm not 100% sure I've got it right yet, but the infrastructure is there. 4. Allows proper tests throughout this project. 5. Allows proper tests in the application too. 6. Removes the need for the handler to be public under flexlib. Flexilib handler registration now works just like everything else. Note, however, that flexilib is unique in that your handler registration function will return before the program ends. If this is important for resource cleanup, @import("build_options").build_type is your friend. 7. Request method can now be passed into console applications using -m or --method
70 lines
2.3 KiB
Zig
70 lines
2.3 KiB
Zig
const std = @import("std");
|
|
|
|
pub const HandlerFn = *const fn (std.mem.Allocator, []const u8, Context) anyerror![]const u8;
|
|
|
|
pub const Response = struct {
|
|
allocator: std.mem.Allocator,
|
|
headers: std.http.Headers,
|
|
headers_owned: bool = true,
|
|
status: std.http.Status = .ok,
|
|
reason: ?[]const u8 = null,
|
|
/// client request. Note that in AWS lambda, all these are left at default.
|
|
/// It is currently up to you to work through a) if API Gateway is set up,
|
|
/// and b) how that gets parsed into event data. API Gateway has the ability
|
|
/// to severely muck with inbound data and we are unprepared to deal with
|
|
/// that here
|
|
request: struct {
|
|
target: []const u8 = "/",
|
|
headers: std.http.Headers,
|
|
headers_owned: bool = true,
|
|
method: std.http.Method = .GET,
|
|
},
|
|
body: std.ArrayList(u8),
|
|
|
|
// The problem we face is this:
|
|
//
|
|
// exe_run, cloudflare (wasi) are basically console apps
|
|
// flexilib is a web server (wierd one)
|
|
// standalone web server is a web server
|
|
// aws lambda is a web client
|
|
//
|
|
// void will work for exe_run/cloudflare
|
|
// ZigResponse works out of the box for flexilib - the lifecycle problem is
|
|
// handled in the interface
|
|
//
|
|
// aws lambda - need to investigate
|
|
// standalone web server...needs to spool
|
|
pub fn init(allocator: std.mem.Allocator) Response {
|
|
return .{
|
|
.allocator = allocator,
|
|
.headers = .{ .allocator = allocator },
|
|
.request = .{
|
|
.headers = .{ .allocator = allocator },
|
|
},
|
|
.body = std.ArrayList(u8).init(allocator),
|
|
};
|
|
}
|
|
pub fn write(res: *Response, bytes: []const u8) !usize {
|
|
return res.body.writer().write(bytes);
|
|
}
|
|
|
|
pub fn writeAll(res: *Response, bytes: []const u8) !void {
|
|
return res.body.writer().writeAll(bytes);
|
|
}
|
|
|
|
pub fn writer(res: *Response) std.io.Writer(*Response, error{OutOfMemory}, writeFn) {
|
|
return .{ .context = res };
|
|
}
|
|
|
|
fn writeFn(context: *Response, data: []const u8) error{OutOfMemory}!usize {
|
|
return try context.write(data);
|
|
}
|
|
pub fn deinit(res: *Response) void {
|
|
res.body.deinit();
|
|
if (res.headers_owned) res.headers.deinit();
|
|
if (res.request.headers_owned) res.request.headers.deinit();
|
|
}
|
|
};
|
|
|
|
pub const Context = *Response;
|