more stuff (sorry)

This commit is contained in:
Emil Lerch 2023-07-31 16:55:01 -07:00
parent d149ec0d52
commit 202cf9c27e
Signed by: lobo
GPG Key ID: A7B62D657EF764F8
5 changed files with 114 additions and 48 deletions

View File

@ -1,26 +1,30 @@
const builtin = @import("builtin"); const builtin = @import("builtin");
const std = @import("std"); const std = @import("std");
const pkgs = @import("deps.zig").pkgs; const GitRepoStep = @import("GitRepoStep.zig");
const CopyStep = @import("CopyStep.zig");
pub fn build(b: *std.build.Builder) !void { pub fn build(b: *std.build.Builder) !void {
// Standard target options allows the person running `zig build` to choose const zfetch_repo = GitRepoStep.create(b, .{
// what target to build for. Here we do not override the defaults, which .url = "https://github.com/truemedian/zfetch",
// means any target is allowed, and the default is native. Other options // .branch = "0.1.10", // branch also takes tags. Tag 0.1.10 isn't quite new enough
// for restricting supported target set are available. .sha = "271cab5da4d12c8f08e67aa0cd5268da100e52f1",
// We want the target to be aarch64-linux for deploys });
const target = std.zig.CrossTarget{
.cpu_arch = .aarch64,
.os_tag = .linux,
};
const copy_deps = CopyStep.create(
b,
"zfetch_deps.zig",
"libs/zfetch/deps.zig",
);
copy_deps.step.dependOn(&zfetch_repo.step);
// Standard release options allow the person running `zig build` to select // Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
// const mode = b.standardReleaseOptions(); // const mode = b.standardReleaseOptions();
const exe = b.addExecutable("bootstrap", "src/main.zig"); const exe = b.addExecutable("bootstrap", "src/main.zig");
pkgs.addAllTo(exe); exe.step.dependOn(&copy_deps.step);
exe.setTarget(target); exe.addPackage(getZfetchPackage(b, "libs/zfetch") catch unreachable);
exe.setBuildMode(.ReleaseSafe); exe.setBuildMode(.ReleaseSafe);
const debug = b.option(bool, "debug", "Debug mode (do not strip executable)") orelse false; const debug = b.option(bool, "debug", "Debug mode (do not strip executable)") orelse false;
exe.strip = !debug; exe.strip = !debug;
@ -28,7 +32,7 @@ pub fn build(b: *std.build.Builder) !void {
// TODO: We can cross-compile of course, but stripping and zip commands // TODO: We can cross-compile of course, but stripping and zip commands
// may vary // may vary
if (std.builtin.os.tag == .linux) { if (builtin.os.tag == .linux) {
// Package step // Package step
const package_step = b.step("package", "Package the function"); const package_step = b.step("package", "Package the function");
package_step.dependOn(b.getInstallStep()); package_step.dependOn(b.getInstallStep());
@ -165,10 +169,46 @@ fn fileExists(file_name: []const u8) bool {
defer file.close(); defer file.close();
return true; return true;
} }
fn addArgs(allocator: *std.mem.Allocator, original: []const u8, args: [][]const u8) ![]const u8 { fn addArgs(allocator: std.mem.Allocator, original: []const u8, args: [][]const u8) ![]const u8 {
var rc = original; var rc = original;
for (args) |arg| { for (args) |arg| {
rc = try std.mem.concat(allocator, u8, &.{ rc, " ", arg }); rc = try std.mem.concat(allocator, u8, &.{ rc, " ", arg });
} }
return rc; return rc;
} }
fn getDependency(comptime lib_prefix: []const u8, comptime name: []const u8, comptime root: []const u8) !std.build.Pkg {
const path = lib_prefix ++ "/libs/" ++ name ++ "/" ++ root;
// We don't actually care if the dependency has been checked out, as
// GitRepoStep will handle that for us
// Make sure that the dependency has been checked out.
// std.fs.cwd().access(path, .{}) catch |err| switch (err) {
// error.FileNotFound => {
// std.log.err("zfetch: dependency '{s}' not checked out", .{name});
//
// return err;
// },
// else => return err,
// };
return std.build.Pkg{
.name = name,
.path = .{ .path = path },
};
}
pub fn getZfetchPackage(b: *std.build.Builder, comptime lib_prefix: []const u8) !std.build.Pkg {
var dependencies = b.allocator.alloc(std.build.Pkg, 4) catch unreachable;
dependencies[0] = try getDependency(lib_prefix, "iguanaTLS", "src/main.zig");
dependencies[1] = try getDependency(lib_prefix, "network", "network.zig");
dependencies[2] = try getDependency(lib_prefix, "uri", "uri.zig");
dependencies[3] = try getDependency(lib_prefix, "hzzp", "src/main.zig");
return std.build.Pkg{
.name = "zfetch",
.path = .{ .path = lib_prefix ++ "/src/main.zig" },
.dependencies = dependencies,
};
}

View File

@ -1,5 +0,0 @@
pkg default ducdetronquito http 0.1.3
pkg default ducdetronquito h11 0.1.1
github nektro iguanaTLS 953ad821fae6c920fb82399493663668cd91bde7 src/main.zig 953ad821fae6c920fb82399493663668cd91bde7
github MasterQ32 zig-network 15b88658809cac9022ec7d59449b0cd3ebfd0361 network.zig 15b88658809cac9022ec7d59449b0cd3ebfd0361
github elerch requestz 1fa8157641300805b9503f98cd201d0959d19631 src/main.zig 1fa8157641300805b9503f98cd201d0959d19631

View File

@ -1,7 +0,0 @@
deps:
requestz:
src:
github:
user: elerch
repo: requestz
ref: 1fa8157641300805b9503f98cd201d0959d19631

View File

@ -1,29 +1,34 @@
const std = @import("std"); const std = @import("std");
const requestz = @import("requestz"); const zfetch = @import("zfetch");
pub fn run(event_handler: fn (*std.mem.Allocator, []const u8) anyerror![]const u8) !void { // TODO: remove inferred error set? pub fn run(event_handler: fn (std.mem.Allocator, []const u8) anyerror![]const u8) !void { // TODO: remove inferred error set?
const prefix = "http://"; const prefix = "http://";
const postfix = "/2018-06-01/runtime/invocation"; const postfix = "/2018-06-01/runtime/invocation";
const lambda_runtime_uri = std.os.getenv("AWS_LAMBDA_RUNTIME_API"); const lambda_runtime_uri = std.os.getenv("AWS_LAMBDA_RUNTIME_API");
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();
const allocator = &gpa.allocator; const allocator = gpa.allocator();
const url = try std.fmt.allocPrint(allocator, "{s}{s}{s}/next", .{ prefix, lambda_runtime_uri, postfix }); const url = try std.fmt.allocPrint(allocator, "{s}{s}{s}/next", .{ prefix, lambda_runtime_uri, postfix });
defer allocator.free(url); defer allocator.free(url);
std.log.notice("Bootstrap initializing with event url: {s}", .{url}); try zfetch.init();
defer zfetch.deinit();
std.log.info("Bootstrap initializing with event url: {s}", .{url});
var headers = zfetch.Headers.init(allocator);
defer headers.deinit();
while (true) { while (true) {
var req_alloc = std.heap.ArenaAllocator.init(allocator); var req_alloc = std.heap.ArenaAllocator.init(allocator);
defer req_alloc.deinit(); defer req_alloc.deinit();
const req_allocator = &req_alloc.allocator; const req_allocator = req_alloc.allocator();
var client = try requestz.Client.init(req_allocator); var req = try zfetch.Request.init(req_allocator, url, null);
// defer client.deinit(); defer req.deinit();
// Lambda freezes the process at this line of code. During warm start, // Lambda freezes the process at this line of code. During warm start,
// the process will unfreeze and data will be sent in response to client.get // the process will unfreeze and data will be sent in response to client.get
var response = client.get(url, .{}) catch |err| { req.do(.GET, headers, null) catch |err| {
std.log.err("Get fail: {}", .{err}); std.log.err("Get fail: {}", .{err});
// Documentation says something about "exit immediately". The // Documentation says something about "exit immediately". The
// Lambda infrastrucutre restarts, so it's unclear if that's necessary. // Lambda infrastrucutre restarts, so it's unclear if that's necessary.
@ -31,12 +36,17 @@ pub fn run(event_handler: fn (*std.mem.Allocator, []const u8) anyerror![]const u
// std.os.exit(1); // std.os.exit(1);
continue; continue;
}; };
defer response.deinit();
var request_id: ?[]const u8 = null; var request_id: ?[]const u8 = null;
for (response.headers.items()) |h| { var content_length: ?usize = null;
if (std.mem.indexOf(u8, h.name.value, "Lambda-Runtime-Aws-Request-Id")) |_| for (req.headers.list.items) |h| {
if (std.ascii.eqlIgnoreCase(h.name, "Lambda-Runtime-Aws-Request-Id"))
request_id = h.value; request_id = h.value;
if (std.ascii.eqlIgnoreCase(h.name, "Content-Length")) {
content_length = std.fmt.parseUnsigned(usize, h.value, 10) catch null;
if (content_length == null)
std.log.warn("Error parsing content length value: '{s}'", .{h.value});
}
// TODO: XRay uses an environment variable to do its magic. It's our // TODO: XRay uses an environment variable to do its magic. It's our
// responsibility to set this, but no zig-native setenv(3)/putenv(3) // responsibility to set this, but no zig-native setenv(3)/putenv(3)
// exists. I would kind of rather not link in libc for this, // exists. I would kind of rather not link in libc for this,
@ -54,7 +64,25 @@ pub fn run(event_handler: fn (*std.mem.Allocator, []const u8) anyerror![]const u
} }
const req_id = request_id.?; const req_id = request_id.?;
const event_response = event_handler(req_allocator, response.body) catch |err| { const reader = req.reader();
var buf: [65535]u8 = undefined;
var resp_payload = std.ArrayList(u8).init(req_allocator);
if (content_length) |len| {
resp_payload.ensureTotalCapacity(len) catch {
std.log.err("Could not allocate memory for body of request id: {s}", .{request_id.?});
continue;
};
}
defer resp_payload.deinit();
while (true) {
const read = try reader.read(&buf);
try resp_payload.appendSlice(buf[0..read]);
if (read == 0) break;
}
const event_response = event_handler(req_allocator, resp_payload.items) catch |err| {
// Stack trace will return null if stripped // Stack trace will return null if stripped
const return_trace = @errorReturnTrace(); const return_trace = @errorReturnTrace();
std.log.err("Caught error: {}. Return Trace: {}", .{ err, return_trace }); std.log.err("Caught error: {}. Return Trace: {}", .{ err, return_trace });
@ -70,27 +98,37 @@ pub fn run(event_handler: fn (*std.mem.Allocator, []const u8) anyerror![]const u
const content_fmt = try std.fmt.allocPrint(req_allocator, content, .{ "{", @errorName(err), return_trace, "}" }); const content_fmt = try std.fmt.allocPrint(req_allocator, content, .{ "{", @errorName(err), return_trace, "}" });
defer req_allocator.free(content_fmt); defer req_allocator.free(content_fmt);
std.log.err("Posting to {s}: Data {s}", .{ err_url, content_fmt }); std.log.err("Posting to {s}: Data {s}", .{ err_url, content_fmt });
var headers = .{.{ "Lambda-Runtime-Function-Error-Type", "HandlerReturned" }};
var err_req = try zfetch.Request.init(req_allocator, err_url, null);
defer err_req.deinit();
var err_headers = zfetch.Headers.init(req_allocator);
defer err_headers.deinit();
err_headers.append(.{
.name = "Lambda-Runtime-Function-Error-Type",
.value = "HandlerReturned",
}) catch |append_err| {
std.log.err("Error appending error header to post response for request id {s}: {}", .{ req_id, append_err });
std.os.exit(0);
continue;
};
// TODO: Determine why this post is not returning // TODO: Determine why this post is not returning
var err_resp = client.post(err_url, .{ err_req.do(.POST, err_headers, content_fmt) catch |post_err| { // Well, at this point all we can do is shout at the void
.content = content_fmt,
.headers = headers,
}) catch |post_err| { // Well, at this point all we can do is shout at the void
std.log.err("Error posting response for request id {s}: {}", .{ req_id, post_err }); std.log.err("Error posting response for request id {s}: {}", .{ req_id, post_err });
std.os.exit(0); std.os.exit(0);
continue; continue;
}; };
std.log.err("Post complete", .{}); std.log.err("Post complete", .{});
defer err_resp.deinit();
continue; continue;
}; };
// We should catch these potential alloc errors too
const response_url = try std.fmt.allocPrint(req_allocator, "{s}{s}{s}/{s}/response", .{ prefix, lambda_runtime_uri, postfix, req_id }); const response_url = try std.fmt.allocPrint(req_allocator, "{s}{s}{s}/{s}/response", .{ prefix, lambda_runtime_uri, postfix, req_id });
// defer req_allocator.free(response_url); const response_content = try std.fmt.allocPrint(req_allocator, "{s} \"content\": \"{s}\" {s}", .{ "{", event_response, "}" });
var resp_resp = client.post(response_url, .{ .content = event_response }) catch |err| { var resp_req = try zfetch.Request.init(req_allocator, response_url, null);
defer resp_req.deinit();
resp_req.do(.POST, headers, response_content) catch |err| {
// TODO: report error // TODO: report error
std.log.err("Error posting response for request id {s}: {}", .{ req_id, err }); std.log.err("Error posting response for request id {s}: {}", .{ req_id, err });
continue; continue;
}; };
defer resp_resp.deinit();
} }
} }

View File

@ -5,7 +5,7 @@ pub fn main() anyerror!void {
try lambda.run(handler); try lambda.run(handler);
} }
fn handler(allocator: *std.mem.Allocator, event_data: []const u8) ![]const u8 { fn handler(allocator: std.mem.Allocator, event_data: []const u8) ![]const u8 {
_ = allocator; _ = allocator;
return event_data; return event_data;
} }