Compare commits
4 Commits
d149ec0d52
...
fa13a08c4d
Author | SHA1 | Date | |
---|---|---|---|
fa13a08c4d | |||
7b890d2458 | |||
3f33693d7a | |||
202cf9c27e |
119
build.zig
119
build.zig
|
@ -1,47 +1,85 @@
|
||||||
const builtin = @import("builtin");
|
const builtin = @import("builtin");
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const pkgs = @import("deps.zig").pkgs;
|
|
||||||
|
|
||||||
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
|
|
||||||
// what target to build for. Here we do not override the defaults, which
|
|
||||||
// means any target is allowed, and the default is native. Other options
|
|
||||||
// for restricting supported target set are available.
|
|
||||||
// We want the target to be aarch64-linux for deploys
|
|
||||||
const target = std.zig.CrossTarget{
|
|
||||||
.cpu_arch = .aarch64,
|
|
||||||
.os_tag = .linux,
|
|
||||||
};
|
|
||||||
|
|
||||||
// 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 target = b.standardTargetOptions(.{});
|
||||||
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
||||||
const exe = b.addExecutable("bootstrap", "src/main.zig");
|
var exe = b.addExecutable(.{
|
||||||
|
.name = "bootstrap",
|
||||||
|
.root_source_file = .{ .path = "src/main.zig" },
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
pkgs.addAllTo(exe);
|
try lambdaBuildOptions(b, exe);
|
||||||
exe.setTarget(target);
|
|
||||||
exe.setBuildMode(.ReleaseSafe);
|
b.installArtifact(exe);
|
||||||
const debug = b.option(bool, "debug", "Debug mode (do not strip executable)") orelse false;
|
|
||||||
exe.strip = !debug;
|
|
||||||
exe.install();
|
|
||||||
|
|
||||||
// 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) {
|
// TODO: Add test
|
||||||
|
}
|
||||||
|
fn fileExists(file_name: []const u8) bool {
|
||||||
|
const file = std.fs.openFileAbsolute(file_name, .{}) catch return false;
|
||||||
|
defer file.close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
fn addArgs(allocator: std.mem.Allocator, original: []const u8, args: [][]const u8) ![]const u8 {
|
||||||
|
var rc = original;
|
||||||
|
for (args) |arg| {
|
||||||
|
rc = try std.mem.concat(allocator, u8, &.{ rc, " ", arg });
|
||||||
|
}
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// lambdaBuildOptions will add three build options to the build (if compiling
|
||||||
|
/// the code on a Linux host):
|
||||||
|
///
|
||||||
|
/// * package: Packages the function for deployment to Lambda
|
||||||
|
/// (dependencies are the zip executable and a shell)
|
||||||
|
/// * iam: Gets an IAM role for the Lambda function, and creates it if it does not exist
|
||||||
|
/// (dependencies are the AWS CLI, grep and a shell)
|
||||||
|
/// * deploy: Deploys the lambda function to a live AWS environment
|
||||||
|
/// (dependencies are the AWS CLI, and a shell)
|
||||||
|
/// * remoterun: Runs the lambda function in a live AWS environment
|
||||||
|
/// (dependencies are the AWS CLI, and a shell)
|
||||||
|
///
|
||||||
|
/// remoterun depends on deploy
|
||||||
|
/// deploy depends on iam and package
|
||||||
|
///
|
||||||
|
/// iam and package do not have any dependencies
|
||||||
|
///
|
||||||
|
/// At the moment, there I do not see a way utilize the zig package manager to
|
||||||
|
/// add modules for build.zig itself. There is some thinking in this area:
|
||||||
|
/// https://github.com/ziglang/zig/issues/8070
|
||||||
|
///
|
||||||
|
/// But for now I think this is a copy/paste job
|
||||||
|
/// TODO: Move this code into another file (temporary)
|
||||||
|
/// TODO: Determine a way to use packages within build.zig (permanent)
|
||||||
|
pub fn lambdaBuildOptions(b: *std.build.Builder, exe: *std.Build.Step.Compile) !void {
|
||||||
|
// The rest of this function is currently reliant on the use of Linux
|
||||||
|
// system being used to build the lambda function
|
||||||
|
//
|
||||||
|
// It is likely that much of this will work on other Unix-like OSs, but
|
||||||
|
// we will work this out later
|
||||||
|
//
|
||||||
|
// TODO: support other host OSs
|
||||||
|
if (builtin.os.tag != .linux) return;
|
||||||
|
|
||||||
// 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());
|
||||||
// strip may not be installed or work for the target arch
|
// const function_zip = b.getInstallPath(exe.installed_path.?, "function.zip");
|
||||||
// TODO: make this much less fragile
|
const function_zip = b.getInstallPath(.bin, "function.zip");
|
||||||
const strip = if (debug)
|
|
||||||
try std.fmt.allocPrint(b.allocator, "true", .{})
|
// TODO: allow use of user-specified exe names
|
||||||
else
|
// TODO: Avoid use of system-installed zip, maybe using something like
|
||||||
try std.fmt.allocPrint(b.allocator, "[ -x /usr/aarch64-linux-gnu/bin/strip ] && /usr/aarch64-linux-gnu/bin/strip {s}", .{b.getInstallPath(exe.install_step.?.dest_dir, exe.install_step.?.artifact.out_filename)});
|
// https://github.com/hdorio/hwzip.zig/blob/master/src/hwzip.zig
|
||||||
defer b.allocator.free(strip);
|
const zip = try std.fmt.allocPrint(b.allocator, "zip -qj9 {s} {s}", .{ function_zip, b.getInstallPath(.bin, exe.out_filename) });
|
||||||
package_step.dependOn(&b.addSystemCommand(&.{ "/bin/sh", "-c", strip }).step);
|
|
||||||
const function_zip = b.getInstallPath(exe.install_step.?.dest_dir, "function.zip");
|
|
||||||
const zip = try std.fmt.allocPrint(b.allocator, "zip -qj9 {s} {s}", .{ function_zip, b.getInstallPath(exe.install_step.?.dest_dir, exe.install_step.?.artifact.out_filename) });
|
|
||||||
defer b.allocator.free(zip);
|
defer b.allocator.free(zip);
|
||||||
package_step.dependOn(&b.addSystemCommand(&.{ "/bin/sh", "-c", zip }).step);
|
package_step.dependOn(&b.addSystemCommand(&.{ "/bin/sh", "-c", zip }).step);
|
||||||
|
|
||||||
|
@ -56,6 +94,8 @@ pub fn build(b: *std.build.Builder) !void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Allow custom lambda role names
|
||||||
var iam_role: []u8 = &.{};
|
var iam_role: []u8 = &.{};
|
||||||
const iam_step = b.step("iam", "Create/Get IAM role for function");
|
const iam_step = b.step("iam", "Create/Get IAM role for function");
|
||||||
deploy_step.dependOn(iam_step); // iam_step will either be a noop or all the stuff below
|
deploy_step.dependOn(iam_step); // iam_step will either be a noop or all the stuff below
|
||||||
|
@ -64,7 +104,7 @@ pub fn build(b: *std.build.Builder) !void {
|
||||||
// need to do anything with the iam role. Otherwise, we'll create/
|
// need to do anything with the iam role. Otherwise, we'll create/
|
||||||
// get the IAM role and stick the name in a file in our destination
|
// get the IAM role and stick the name in a file in our destination
|
||||||
// directory to be used later
|
// directory to be used later
|
||||||
const iam_role_name_file = b.getInstallPath(exe.install_step.?.dest_dir, "iam_role_name");
|
const iam_role_name_file = b.getInstallPath(.bin, "iam_role_name");
|
||||||
iam_role = try std.fmt.allocPrint(b.allocator, "--role $(cat {s})", .{iam_role_name_file});
|
iam_role = try std.fmt.allocPrint(b.allocator, "--role $(cat {s})", .{iam_role_name_file});
|
||||||
// defer b.allocator.free(iam_role);
|
// defer b.allocator.free(iam_role);
|
||||||
if (!fileExists(iam_role_name_file)) {
|
if (!fileExists(iam_role_name_file)) {
|
||||||
|
@ -92,7 +132,7 @@ pub fn build(b: *std.build.Builder) !void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const function_name = b.option([]const u8, "function-name", "Function name for Lambda [zig-fn]") orelse "zig-fn";
|
const function_name = b.option([]const u8, "function-name", "Function name for Lambda [zig-fn]") orelse "zig-fn";
|
||||||
const function_name_file = b.getInstallPath(exe.install_step.?.dest_dir, function_name);
|
const function_name_file = b.getInstallPath(.bin, function_name);
|
||||||
const ifstatement = "if [ ! -f {s} ] || [ {s} -nt {s} ]; then if aws lambda get-function --function-name {s} 2>&1 |grep -q ResourceNotFoundException; then echo not found > /dev/null; {s}; else echo found > /dev/null; {s}; fi; fi";
|
const ifstatement = "if [ ! -f {s} ] || [ {s} -nt {s} ]; then if aws lambda get-function --function-name {s} 2>&1 |grep -q ResourceNotFoundException; then echo not found > /dev/null; {s}; else echo found > /dev/null; {s}; fi; fi";
|
||||||
// The architectures option was introduced in 2.2.43 released 2021-10-01
|
// The architectures option was introduced in 2.2.43 released 2021-10-01
|
||||||
// We want to use arm64 here because it is both faster and cheaper for most
|
// We want to use arm64 here because it is both faster and cheaper for most
|
||||||
|
@ -114,7 +154,7 @@ pub fn build(b: *std.build.Builder) !void {
|
||||||
}
|
}
|
||||||
const cmd = try std.fmt.allocPrint(b.allocator, ifstatement, .{
|
const cmd = try std.fmt.allocPrint(b.allocator, ifstatement, .{
|
||||||
function_name_file,
|
function_name_file,
|
||||||
std.fs.path.dirname(exe.root_src.?.path),
|
std.fs.path.dirname(exe.root_src.?.path).?,
|
||||||
function_name_file,
|
function_name_file,
|
||||||
function_name,
|
function_name,
|
||||||
not_found_fmt,
|
not_found_fmt,
|
||||||
|
@ -156,19 +196,6 @@ pub fn build(b: *std.build.Builder) !void {
|
||||||
run_cmd.addArgs(args);
|
run_cmd.addArgs(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
const run_step = b.step("run", "Run the app");
|
const run_step = b.step("remoterun", "Run the app in AWS lambda");
|
||||||
run_step.dependOn(&run_cmd.step);
|
run_step.dependOn(&run_cmd.step);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
fn fileExists(file_name: []const u8) bool {
|
|
||||||
const file = std.fs.openFileAbsolute(file_name, .{}) catch return false;
|
|
||||||
defer file.close();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
fn addArgs(allocator: *std.mem.Allocator, original: []const u8, args: [][]const u8) ![]const u8 {
|
|
||||||
var rc = original;
|
|
||||||
for (args) |arg| {
|
|
||||||
rc = try std.mem.concat(allocator, u8, &.{ rc, " ", arg });
|
|
||||||
}
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
|
@ -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
|
|
7
gyro.zzz
7
gyro.zzz
|
@ -1,7 +0,0 @@
|
||||||
deps:
|
|
||||||
requestz:
|
|
||||||
src:
|
|
||||||
github:
|
|
||||||
user: elerch
|
|
||||||
repo: requestz
|
|
||||||
ref: 1fa8157641300805b9503f98cd201d0959d19631
|
|
142
src/lambda.zig
142
src/lambda.zig
|
@ -1,42 +1,69 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const requestz = @import("requestz");
|
|
||||||
|
|
||||||
pub fn run(event_handler: fn (*std.mem.Allocator, []const u8) anyerror![]const u8) !void { // TODO: remove inferred error set?
|
const HandlerFn = *const fn (std.mem.Allocator, []const u8) anyerror![]const u8;
|
||||||
|
|
||||||
|
pub fn run(event_handler: HandlerFn) !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").?;
|
||||||
|
// TODO: If this is null, go into single use command line mode
|
||||||
|
|
||||||
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);
|
||||||
|
const uri = try std.Uri.parse(url);
|
||||||
|
|
||||||
std.log.notice("Bootstrap initializing with event url: {s}", .{url});
|
var client: std.http.Client = .{ .allocator = allocator };
|
||||||
|
defer client.deinit();
|
||||||
|
var empty_headers = std.http.Headers.init(allocator);
|
||||||
|
defer empty_headers.deinit();
|
||||||
|
std.log.info("Bootstrap initializing with event url: {s}", .{url});
|
||||||
|
|
||||||
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 client.request(.GET, uri, empty_headers, .{});
|
||||||
// defer client.deinit();
|
defer req.deinit();
|
||||||
|
|
||||||
|
req.start() catch |err| { // Well, at this point all we can do is shout at the void
|
||||||
|
std.log.err("Get fail (start): {}", .{err});
|
||||||
|
std.os.exit(0);
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
// 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.wait() catch |err| { // Well, at this point all we can do is shout at the void
|
||||||
std.log.err("Get fail: {}", .{err});
|
std.log.err("Get fail (wait): {}", .{err});
|
||||||
|
std.os.exit(0);
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
if (req.response.status != .ok) {
|
||||||
// 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.
|
||||||
// It seems as though a continue should be fine, and slightly faster
|
// It seems as though a continue should be fine, and slightly faster
|
||||||
// std.os.exit(1);
|
// std.os.exit(1);
|
||||||
|
std.log.err("Get fail: {} {s}", .{
|
||||||
|
@intFromEnum(req.response.status),
|
||||||
|
req.response.status.phrase() orelse "",
|
||||||
|
});
|
||||||
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,43 +81,100 @@ 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 });
|
if (return_trace) |rt|
|
||||||
|
std.log.err("Caught error: {}. Return Trace: {any}", .{ err, rt })
|
||||||
|
else
|
||||||
|
std.log.err("Caught error: {}. No return trace available", .{err});
|
||||||
const err_url = try std.fmt.allocPrint(req_allocator, "{s}{s}/runtime/invocation/{s}/error", .{ prefix, lambda_runtime_uri, req_id });
|
const err_url = try std.fmt.allocPrint(req_allocator, "{s}{s}/runtime/invocation/{s}/error", .{ prefix, lambda_runtime_uri, req_id });
|
||||||
defer req_allocator.free(err_url);
|
defer req_allocator.free(err_url);
|
||||||
|
const err_uri = try std.Uri.parse(err_url);
|
||||||
const content =
|
const content =
|
||||||
\\ {s}
|
\\ {s}
|
||||||
\\ "errorMessage": "{s}",
|
\\ "errorMessage": "{s}",
|
||||||
\\ "errorType": "HandlerReturnedError",
|
\\ "errorType": "HandlerReturnedError",
|
||||||
\\ "stackTrace": [ "{}" ]
|
\\ "stackTrace": [ "{any}" ]
|
||||||
\\ {s}
|
\\ {s}
|
||||||
;
|
;
|
||||||
const content_fmt = try std.fmt.allocPrint(req_allocator, content, .{ "{", @errorName(err), return_trace, "}" });
|
const content_fmt = if (return_trace) |rt|
|
||||||
|
try std.fmt.allocPrint(req_allocator, content, .{ "{", @errorName(err), rt, "}" })
|
||||||
|
else
|
||||||
|
try std.fmt.allocPrint(req_allocator, content, .{ "{", @errorName(err), "no return trace available", "}" });
|
||||||
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" }};
|
|
||||||
// TODO: Determine why this post is not returning
|
var err_headers = std.http.Headers.init(req_allocator);
|
||||||
var err_resp = client.post(err_url, .{
|
defer err_headers.deinit();
|
||||||
.content = content_fmt,
|
err_headers.append(
|
||||||
.headers = headers,
|
"Lambda-Runtime-Function-Error-Type",
|
||||||
}) catch |post_err| { // Well, at this point all we can do is shout at the void
|
"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;
|
||||||
|
};
|
||||||
|
var err_req = try client.request(.POST, err_uri, empty_headers, .{});
|
||||||
|
defer err_req.deinit();
|
||||||
|
err_req.start() 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", .{});
|
|
||||||
defer err_resp.deinit();
|
err_req.wait() 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.os.exit(0);
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
// TODO: Determine why this post is not returning
|
||||||
|
if (err_req.response.status != .ok) {
|
||||||
|
// Documentation says something about "exit immediately". The
|
||||||
|
// Lambda infrastrucutre restarts, so it's unclear if that's necessary.
|
||||||
|
// It seems as though a continue should be fine, and slightly faster
|
||||||
|
// std.os.exit(1);
|
||||||
|
std.log.err("Get fail: {} {s}", .{
|
||||||
|
@intFromEnum(err_req.response.status),
|
||||||
|
err_req.response.status.phrase() orelse "",
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
std.log.err("Post complete", .{});
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
// TODO: We should catch these potential alloc errors too
|
||||||
|
// TODO: This whole loop should be in another function so we can catch everything at once
|
||||||
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);
|
defer allocator.free(response_url);
|
||||||
var resp_resp = client.post(response_url, .{ .content = event_response }) catch |err| {
|
const response_uri = try std.Uri.parse(response_url);
|
||||||
|
const response_content = try std.fmt.allocPrint(req_allocator, "{s} \"content\": \"{s}\" {s}", .{ "{", event_response, "}" });
|
||||||
|
var resp_req = try client.request(.POST, response_uri, empty_headers, .{});
|
||||||
|
defer resp_req.deinit();
|
||||||
|
try resp_req.start();
|
||||||
|
try resp_req.writeAll(response_content); // TODO: AllocPrint + writeAll makes no sense
|
||||||
|
resp_req.wait() 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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user