-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
394 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} |
Oops, something went wrong.