mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-16 00:08:59 +00:00
Share the HTTP client globally
This commit is contained in:
40
src/app.zig
40
src/app.zig
@@ -2,24 +2,29 @@ const std = @import("std");
|
||||
|
||||
const Loop = @import("jsruntime").Loop;
|
||||
const Allocator = std.mem.Allocator;
|
||||
const HttpClient = @import("http/Client.zig");
|
||||
const Telemetry = @import("telemetry/telemetry.zig").Telemetry;
|
||||
|
||||
const log = std.log.scoped(.app);
|
||||
|
||||
pub const RunMode = enum {
|
||||
serve,
|
||||
fetch,
|
||||
};
|
||||
|
||||
// Container for global state / objects that various parts of the system
|
||||
// might need.
|
||||
pub const App = struct {
|
||||
loop: *Loop,
|
||||
app_dir_path: ?[]const u8,
|
||||
allocator: Allocator,
|
||||
telemetry: Telemetry,
|
||||
http_client: HttpClient,
|
||||
app_dir_path: ?[]const u8,
|
||||
|
||||
pub const RunMode = enum {
|
||||
serve,
|
||||
fetch,
|
||||
};
|
||||
|
||||
pub fn init(allocator: Allocator, run_mode: RunMode) !*App {
|
||||
const app = try allocator.create(App);
|
||||
errdefer allocator.destroy(app);
|
||||
|
||||
pub fn init(allocator: Allocator, run_mode: RunMode) !App {
|
||||
const loop = try allocator.create(Loop);
|
||||
errdefer allocator.destroy(loop);
|
||||
|
||||
@@ -27,29 +32,36 @@ pub const App = struct {
|
||||
errdefer loop.deinit();
|
||||
|
||||
const app_dir_path = getAndMakeAppDir(allocator);
|
||||
const telemetry = Telemetry.init(allocator, run_mode, app_dir_path);
|
||||
errdefer telemetry.deinit();
|
||||
|
||||
return .{
|
||||
app.* = .{
|
||||
.loop = loop,
|
||||
.allocator = allocator,
|
||||
.telemetry = telemetry,
|
||||
.telemetry = undefined,
|
||||
.app_dir_path = app_dir_path,
|
||||
.http_client = .{ .allocator = allocator },
|
||||
};
|
||||
app.telemetry = Telemetry.init(app, run_mode);
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
pub fn deinit(self: *App) void {
|
||||
const allocator = self.allocator;
|
||||
if (self.app_dir_path) |app_dir_path| {
|
||||
self.allocator.free(app_dir_path);
|
||||
allocator.free(app_dir_path);
|
||||
}
|
||||
|
||||
self.telemetry.deinit();
|
||||
self.loop.deinit();
|
||||
self.allocator.destroy(self.loop);
|
||||
allocator.destroy(self.loop);
|
||||
self.http_client.deinit();
|
||||
allocator.destroy(self);
|
||||
}
|
||||
};
|
||||
|
||||
fn getAndMakeAppDir(allocator: Allocator) ?[]const u8 {
|
||||
if (@import("builtin").is_test) {
|
||||
return allocator.dupe(u8, "/tmp") catch unreachable;
|
||||
}
|
||||
const app_dir_path = std.fs.getAppDataDir(allocator, "lightpanda") catch |err| {
|
||||
log.warn("failed to get lightpanda data dir: {}", .{err});
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user