feat: add index.json

This commit is contained in:
LordMZTE 2023-03-16 16:40:40 +01:00
parent 0ee49c97df
commit 5e3f832d3e
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
6 changed files with 75 additions and 3 deletions

View file

@ -101,7 +101,7 @@ const BuildFrontendStep = struct {
const outpath = try std.fs.path.join(
self.b.allocator,
&.{ self.b.cache_root, "options", "index.js" },
&.{ self.b.cache_root.path.?, "options", "index.js" },
);
const argv = [_][]const u8{
"haxe",

View file

@ -1,4 +1,5 @@
{
"bind": "127.0.0.1:8080",
"data_dir": "zig-cache/data"
"data_dir": "zig-cache/data",
"base_url": "http://127.0.0.1:8080"
}

View file

@ -2,6 +2,7 @@ const std = @import("std");
bind: []const u8,
data_dir: []const u8,
base_url: []const u8,
const Self = @This();

View file

@ -3,3 +3,4 @@ const DownloadQueue = @import("DownloadQueue.zig");
downloads: *DownloadQueue,
data_dir: []const u8,
vids_dir: []const u8,
base_url: []const u8,

View file

@ -53,6 +53,7 @@ pub fn main() !void {
.downloads = undefined,
.data_dir = conf.data_dir,
.vids_dir = vids_path,
.base_url = std.mem.trimRight(u8, conf.base_url, "/"),
};
const downloads = try DownloadQueue.spawn(&state);
@ -65,11 +66,18 @@ pub fn main() !void {
const urls = c.onion_root_url(onion);
_ = c.onion_url_add_with_data(
urls,
"",
"^$|^index\\.html$",
routes.routeCb(routes.indexRoute, true),
&state,
null,
);
_ = c.onion_url_add_with_data(
urls,
"^index\\.json$",
routes.routeCb(routes.indexJsonRoute, true),
&state,
null,
);
_ = c.onion_url_add_with_data(
urls,
"^vids/",

View file

@ -163,6 +163,67 @@ pub fn indexRoute(
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 {
url: []const u8,
outname: ?[]const u8,