Compare commits
3 Commits
fc0f9a0c76
...
54740a7022
Author | SHA1 | Date | |
---|---|---|---|
54740a7022 | |||
5edaacde07 | |||
3088af4219 |
|
@ -19,7 +19,7 @@ pub fn build(b: *std.Build) !void {
|
|||
.name = "universal-lambda-zig",
|
||||
// In this case the main source file is merely a path, however, in more
|
||||
// complicated build scripts, this could be a generated file.
|
||||
.root_source_file = .{ .path = "src/main.zig" },
|
||||
.root_source_file = .{ .path = "src/universal_lambda_build.zig" },
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
|
|
@ -17,6 +17,9 @@ pub const Response = extern struct {
|
|||
};
|
||||
|
||||
pub const Request = extern struct {
|
||||
target: [*]const u8,
|
||||
target_len: usize,
|
||||
|
||||
method: [*:0]u8,
|
||||
method_len: usize,
|
||||
|
||||
|
@ -33,6 +36,7 @@ threadlocal var allocator: ?*std.mem.Allocator = null;
|
|||
const log = std.log.scoped(.interface);
|
||||
|
||||
pub const ZigRequest = struct {
|
||||
target: []const u8,
|
||||
method: [:0]u8,
|
||||
content: []u8,
|
||||
headers: []Header,
|
||||
|
@ -104,6 +108,7 @@ pub fn handleRequest(request: *Request, zigRequestHandler: ZigRequestHandler) ?*
|
|||
zigRequestHandler(
|
||||
alloc,
|
||||
.{
|
||||
.target = request.target[0..request.target_len],
|
||||
.method = request.method[0..request.method_len :0],
|
||||
.content = request.content[0..request.content_len],
|
||||
.headers = request.headers[0..request.headers_len],
|
||||
|
|
|
@ -62,23 +62,11 @@ export fn handle_request(request: *interface.Request) callconv(.C) ?*interface.R
|
|||
fn handleRequest(allocator: std.mem.Allocator, request: interface.ZigRequest, response: interface.ZigResponse) !void {
|
||||
// setup
|
||||
var response_writer = response.body.writer();
|
||||
try response_writer.writeAll(try client_handler.handler(allocator, request.content, .{}));
|
||||
// real work
|
||||
for (request.headers) |h| {
|
||||
const header = interface.toZigHeader(h);
|
||||
// std.debug.print("\n{s}: {s}\n", .{ header.name, header.value });
|
||||
if (std.ascii.eqlIgnoreCase(header.name, "host") and std.mem.startsWith(u8, header.value, "iam")) {
|
||||
try response_writer.print("iam response", .{});
|
||||
return;
|
||||
}
|
||||
if (std.ascii.eqlIgnoreCase(header.name, "x-slow")) {
|
||||
std.time.sleep(std.time.ns_per_ms * (std.fmt.parseInt(usize, header.value, 10) catch 1000));
|
||||
try response_writer.print("i am slow\n\n", .{});
|
||||
return;
|
||||
}
|
||||
}
|
||||
try response.headers.put("X-custom-foo", "bar");
|
||||
log.info("handlerequest header count {d}", .{response.headers.count()});
|
||||
// dispatch to our actual handler
|
||||
try response_writer.writeAll(try client_handler.handler(allocator, request.content, .{ .flexilib = .{
|
||||
.request = request,
|
||||
.response = response,
|
||||
} }));
|
||||
}
|
||||
// Need to figure out how tests would work
|
||||
test "handle_request" {
|
||||
|
|
|
@ -67,7 +67,10 @@ pub fn run(allocator: ?std.mem.Allocator, event_handler: HandlerFn) !void { // T
|
|||
// reasonable to report back
|
||||
const event = ev.?;
|
||||
defer ev.?.deinit();
|
||||
const event_response = event_handler(req_allocator, event.event_data, .{}) catch |err| {
|
||||
// Lambda does not have context, just environment variables. API Gateway
|
||||
// might be configured to pass in lots of context, but this comes through
|
||||
// event data, not context.
|
||||
const event_response = event_handler(req_allocator, event.event_data, .{ .none = {} }) catch |err| {
|
||||
event.reportError(@errorReturnTrace(), err, lambda_runtime_uri) catch unreachable;
|
||||
continue;
|
||||
};
|
||||
|
|
|
@ -1,12 +1,19 @@
|
|||
const std = @import("std");
|
||||
const build_options = @import("build_options");
|
||||
|
||||
const flexilib = @import("flexilib-interface.zig"); // TODO: pull in flexilib directly
|
||||
pub const HandlerFn = *const fn (std.mem.Allocator, []const u8, Context) anyerror![]const u8;
|
||||
|
||||
const log = std.log.scoped(.universal_lambda);
|
||||
|
||||
// TODO: Should this be union?
|
||||
pub const Context = struct {};
|
||||
pub const Context = union(enum) {
|
||||
web_request: *std.http.Server.Response,
|
||||
flexilib: struct {
|
||||
request: flexilib.Request,
|
||||
response: flexilib.Response,
|
||||
},
|
||||
none: void,
|
||||
};
|
||||
|
||||
const runFn = blk: {
|
||||
switch (build_options.build_type) {
|
||||
|
@ -37,11 +44,12 @@ fn runExe(allocator: ?std.mem.Allocator, event_handler: HandlerFn) !void {
|
|||
|
||||
const aa = arena.allocator();
|
||||
|
||||
const data = try std.io.getStdIn().reader().readAllAlloc(aa, std.math.maxInt(usize));
|
||||
// We're setting up an arena allocator. While we could use a gpa and get
|
||||
// some additional safety, this is now "production" runtime, and those
|
||||
// things are better handled by unit tests
|
||||
const writer = std.io.getStdOut().writer();
|
||||
try writer.writeAll(try event_handler(aa, "", .{}));
|
||||
try writer.writeAll(try event_handler(aa, data, .{ .none = {} }));
|
||||
try writer.writeAll("\n");
|
||||
}
|
||||
|
||||
|
@ -105,7 +113,7 @@ fn processRequest(aa: std.mem.Allocator, server: *std.http.Server, event_handler
|
|||
try aa.dupe(u8, "");
|
||||
// no need to free - will be handled by arena
|
||||
|
||||
response_bytes = event_handler(aa, body, .{}) catch |e| brk: {
|
||||
response_bytes = event_handler(aa, body, .{ .web_request = &res }) catch |e| brk: {
|
||||
res.status = .internal_server_error;
|
||||
// TODO: more about this particular request
|
||||
log.err("Unexpected error from executor processing request: {any}", .{e});
|
||||
|
|
Loading…
Reference in New Issue
Block a user