feat: add index.json
This commit is contained in:
parent
0ee49c97df
commit
5e3f832d3e
|
@ -101,7 +101,7 @@ const BuildFrontendStep = struct {
|
||||||
|
|
||||||
const outpath = try std.fs.path.join(
|
const outpath = try std.fs.path.join(
|
||||||
self.b.allocator,
|
self.b.allocator,
|
||||||
&.{ self.b.cache_root, "options", "index.js" },
|
&.{ self.b.cache_root.path.?, "options", "index.js" },
|
||||||
);
|
);
|
||||||
const argv = [_][]const u8{
|
const argv = [_][]const u8{
|
||||||
"haxe",
|
"haxe",
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
{
|
{
|
||||||
"bind": "127.0.0.1:8080",
|
"bind": "127.0.0.1:8080",
|
||||||
"data_dir": "zig-cache/data"
|
"data_dir": "zig-cache/data",
|
||||||
|
"base_url": "http://127.0.0.1:8080"
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ const std = @import("std");
|
||||||
|
|
||||||
bind: []const u8,
|
bind: []const u8,
|
||||||
data_dir: []const u8,
|
data_dir: []const u8,
|
||||||
|
base_url: []const u8,
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
|
|
|
@ -3,3 +3,4 @@ const DownloadQueue = @import("DownloadQueue.zig");
|
||||||
downloads: *DownloadQueue,
|
downloads: *DownloadQueue,
|
||||||
data_dir: []const u8,
|
data_dir: []const u8,
|
||||||
vids_dir: []const u8,
|
vids_dir: []const u8,
|
||||||
|
base_url: []const u8,
|
||||||
|
|
10
src/main.zig
10
src/main.zig
|
@ -53,6 +53,7 @@ pub fn main() !void {
|
||||||
.downloads = undefined,
|
.downloads = undefined,
|
||||||
.data_dir = conf.data_dir,
|
.data_dir = conf.data_dir,
|
||||||
.vids_dir = vids_path,
|
.vids_dir = vids_path,
|
||||||
|
.base_url = std.mem.trimRight(u8, conf.base_url, "/"),
|
||||||
};
|
};
|
||||||
|
|
||||||
const downloads = try DownloadQueue.spawn(&state);
|
const downloads = try DownloadQueue.spawn(&state);
|
||||||
|
@ -65,11 +66,18 @@ pub fn main() !void {
|
||||||
const urls = c.onion_root_url(onion);
|
const urls = c.onion_root_url(onion);
|
||||||
_ = c.onion_url_add_with_data(
|
_ = c.onion_url_add_with_data(
|
||||||
urls,
|
urls,
|
||||||
"",
|
"^$|^index\\.html$",
|
||||||
routes.routeCb(routes.indexRoute, true),
|
routes.routeCb(routes.indexRoute, true),
|
||||||
&state,
|
&state,
|
||||||
null,
|
null,
|
||||||
);
|
);
|
||||||
|
_ = c.onion_url_add_with_data(
|
||||||
|
urls,
|
||||||
|
"^index\\.json$",
|
||||||
|
routes.routeCb(routes.indexJsonRoute, true),
|
||||||
|
&state,
|
||||||
|
null,
|
||||||
|
);
|
||||||
_ = c.onion_url_add_with_data(
|
_ = c.onion_url_add_with_data(
|
||||||
urls,
|
urls,
|
||||||
"^vids/",
|
"^vids/",
|
||||||
|
|
|
@ -163,6 +163,67 @@ pub fn indexRoute(
|
||||||
return c.OCS_PROCESSED;
|
return c.OCS_PROCESSED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn indexJsonRoute(
|
||||||
|
state: *State,
|
||||||
|
req: *c.onion_request,
|
||||||
|
res: *c.onion_response,
|
||||||
|
) !c_int {
|
||||||
|
_ = req;
|
||||||
|
|
||||||
|
const Response = struct {
|
||||||
|
active_task: ?DownloadQueue.Task,
|
||||||
|
tasks: []DownloadQueue.Task,
|
||||||
|
vids: [][]u8,
|
||||||
|
paused: bool,
|
||||||
|
};
|
||||||
|
|
||||||
|
var arena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
|
||||||
|
defer arena.deinit();
|
||||||
|
const alloc = arena.allocator();
|
||||||
|
|
||||||
|
const tasks = try alloc.alloc(DownloadQueue.Task, state.downloads.tasks.len);
|
||||||
|
var cur_task = state.downloads.tasks.last;
|
||||||
|
var i: usize = 0;
|
||||||
|
while (cur_task) |t| {
|
||||||
|
tasks[i] = t.data;
|
||||||
|
i += 1;
|
||||||
|
cur_task = t.prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
var vids_dir = try std.fs.cwd().openIterableDir(state.vids_dir, .{});
|
||||||
|
defer vids_dir.close();
|
||||||
|
|
||||||
|
var iter = vids_dir.iterate();
|
||||||
|
|
||||||
|
var vids = std.ArrayList([]u8).init(std.heap.c_allocator);
|
||||||
|
defer vids.deinit();
|
||||||
|
while (try iter.next()) |ent| {
|
||||||
|
if (ent.kind != .File)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
const path_z = try alloc.dupeZ(u8, ent.name);
|
||||||
|
|
||||||
|
const quote = c.onion_quote_new(path_z) orelse return error.OutOfMemory;
|
||||||
|
defer c.free(quote);
|
||||||
|
|
||||||
|
try vids.append(try std.fmt.allocPrint(alloc, "{s}/vids/{s}", .{ state.base_url, quote }));
|
||||||
|
}
|
||||||
|
|
||||||
|
c.onion_response_set_header(res, "Content-Type", "application/json");
|
||||||
|
|
||||||
|
const data = Response{
|
||||||
|
.active_task = state.downloads.active_task,
|
||||||
|
.tasks = tasks,
|
||||||
|
.vids = vids.items,
|
||||||
|
.paused = state.downloads.paused,
|
||||||
|
};
|
||||||
|
|
||||||
|
const writer = responseWriter(res);
|
||||||
|
try std.json.stringify(data, .{}, writer);
|
||||||
|
|
||||||
|
return c.OCS_PROCESSED;
|
||||||
|
}
|
||||||
|
|
||||||
pub const ApiTask = struct {
|
pub const ApiTask = struct {
|
||||||
url: []const u8,
|
url: []const u8,
|
||||||
outname: ?[]const u8,
|
outname: ?[]const u8,
|
||||||
|
|
Loading…
Reference in a new issue