Skip to content

Commit

Permalink
WIP: use client in proxy example
Browse files Browse the repository at this point in the history
  • Loading branch information
mookums committed Nov 19, 2024
1 parent 7319d9c commit a6a68cc
Show file tree
Hide file tree
Showing 8 changed files with 394 additions and 92 deletions.
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn build(b: *std.Build) void {
add_example(b, "benchmark", false, target, optimize, zzz, tardy);
add_example(b, "valgrind", true, target, optimize, zzz, tardy);

add_example(b, "client", false, target, optimize, zzz, tardy);
add_example(b, "proxy", false, target, optimize, zzz, tardy);

const tests = b.addTest(.{
.name = "tests",
Expand Down
52 changes: 0 additions & 52 deletions examples/client/main.zig

This file was deleted.

104 changes: 104 additions & 0 deletions examples/proxy/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const std = @import("std");
const log = std.log.scoped(.@"examples/proxy");

const zzz = @import("zzz");
const http = zzz.HTTP;

const tardy = @import("tardy");
const Tardy = tardy.Tardy(.auto);
const Runtime = tardy.Runtime;

const Client = http.Client;

const Server = http.Server(.plain);
const Router = Server.Router;
const Context = Server.Context;
const Route = Server.Route;

// this uses the new zzz HTTP client implementation as a proxy, firing off the requests
// and returning the retrieved data.
fn fetch_task(_: *Runtime, response: ?*const http.Response, ctx: *Context) !void {
if (response) |resp| {
try ctx.respond(.{
.status = .OK,
.mime = resp.mime.?,
// You need to dupe it out since the client
// response data is cleaned up after this callback runs.
.body = try ctx.allocator.dupe(u8, resp.body.?),
});
} else {
// If our fetch errors while running, we will get a null here.
try ctx.respond(.{
.status = .@"Internal Server Error",
.mime = http.Mime.HTML,
.body = "NOT FOUND!",
});
}
}

pub fn main() !void {
const host: []const u8 = "0.0.0.0";
const port: u16 = 9862;
//const proxy_path = "http://httpforever.com";
const proxy_path = "http://http.badssl.com";

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
defer _ = gpa.deinit();

// Creating our Tardy instance that
// will spawn our runtimes.
var t = try Tardy.init(.{
.allocator = allocator,
.threading = .single,
});
defer t.deinit();

var client = Client.init(allocator, .{});
defer client.deinit();

var router = Router.init(allocator);
defer router.deinit();

// TODO: we need a better solution for a general reroute on
// all routes.

try router.serve_route("/", Route.init().get(&client, struct {
fn handler_fn(ctx: *Context, c: *Client) !void {
var req = try c.get(ctx.runtime, proxy_path);
try req.fetch(ctx, fetch_task);
}
}.handler_fn));

try router.serve_route("/%r", Route.init().get(&client, struct {
fn handler_fn(ctx: *Context, c: *Client) !void {
const path = try std.fmt.allocPrint(ctx.allocator, "{s}/{s}", .{ proxy_path, ctx.path });
var req = try c.get(ctx.runtime, path);
try req.fetch(ctx, fetch_task);
}
}.handler_fn));

try t.entry(
&router,
struct {
fn entry(rt: *Runtime, r: *const Router) !void {
var server = Server.init(.{ .allocator = rt.allocator });
try server.bind(host, port);
try server.serve(r, rt);

// this kills it after a set delay, allowing the GPA to report any leaks.
//try rt.spawn_delay(void, {}, struct {
// fn kill_task(runtime: *Runtime, _: void, _: void) !void {
// runtime.stop();
// }
//}.kill_task, .{ .seconds = 30 });
}
}.entry,
{},
struct {
fn exit(rt: *Runtime, _: void) !void {
try Server.clean(rt);
}
}.exit,
);
}
Loading

0 comments on commit a6a68cc

Please sign in to comment.