srctree

Gregory Mullen parent fb224d73 09d9a863
update srctree to the newest verse API

src/api.zig added: 120, removed: 101, total 19
@@ -38,7 +38,7 @@ pub fn router(vrs: *Verse) Router.Error!Router.BuildFn {
const uri_api = vrs.uri.next() orelse return heartbeat;
if (!std.mem.eql(u8, uri_api, "api")) return heartbeat;
const rd = try APIRouteData.init(vrs.alloc);
vrs.route_ctx = rd;
vrs.route_data.add("api", rd) catch unreachable;
 
return Router.router(vrs, &endpoints);
}
 
src/endpoints/commit-flex.zig added: 120, removed: 101, total 19
@@ -403,10 +403,12 @@ pub fn commitFlex(ctx: *Verse) Error!void {
});
}
 
const btns = [1]Template.Structs.NavButtons{.{ .name = "inbox", .extra = 0, .url = "/inbox" }};
var page = UserCommitsPage.init(.{
.meta_head = .{ .open_graph = .{} },
.body_header = .{ .nav = .{ .nav_auth = undefined, .nav_buttons = &btns } },
.body_header = (ctx.route_data.get(
"body_header",
*const S.BodyHeaderHtml,
) catch return error.Unknown).*,
.total_hits = try allocPrint(ctx.alloc, "{}", .{tcount}),
.flexes = flexes,
.checked_repos = try allocPrint(ctx.alloc, "{}", .{repo_count}),
 
src/endpoints/gist.zig added: 120, removed: 101, total 19
@@ -3,6 +3,7 @@ const allocPrint = std.fmt.allocPrint;
 
const Verse = @import("verse");
const Template = Verse.Template;
const S = Template.Structs;
const RequestData = Verse.RequestData.RequestData;
const Bleach = @import("../bleach.zig");
const Allocator = std.mem.Allocator;
@@ -95,28 +96,21 @@ fn new(ctx: *Verse) Error!void {
return edit(ctx, &files);
}
 
fn edit(ctx: *Verse, files: []const Template.Structs.GistFiles) Error!void {
// TODO move this back into context somehow
var btns = [1]Template.Structs.NavButtons{
.{ .name = "inbox", .extra = 0, .url = "/inbox" },
};
 
fn edit(vrs: *Verse, files: []const Template.Structs.GistFiles) Error!void {
var page = GistNewPage.init(.{
.meta_head = .{
.open_graph = .{
.title = "Create A New Gist",
},
},
.body_header = .{
.nav = .{
.nav_auth = undefined,
.nav_buttons = &btns,
},
},
.body_header = (vrs.route_data.get(
"body_header",
*const S.BodyHeaderHtml,
) catch return error.Unknown).*,
.gist_files = files,
});
 
return ctx.sendPage(&page);
return vrs.sendPage(&page);
}
 
fn toTemplate(a: Allocator, files: []const Gist.File) ![]Template.Structs.GistFiles {
@@ -130,16 +124,16 @@ fn toTemplate(a: Allocator, files: []const Gist.File) ![]Template.Structs.GistFi
return out;
}
 
fn view(ctx: *Verse) Error!void {
fn view(vrs: *Verse) Error!void {
// TODO move this back into context somehow
var btns = [1]Template.Structs.NavButtons{.{ .name = "inbox", .extra = 0, .url = "/inbox" }};
 
if (ctx.uri.next()) |hash| {
if (vrs.uri.next()) |hash| {
if (hash.len != 64) return error.BadData;
 
const gist = Gist.open(ctx.alloc, hash[0..64].*) catch return error.Unknown;
const files = toTemplate(ctx.alloc, gist.files) catch return error.Unknown;
const og = try std.fmt.allocPrint(ctx.alloc, "A perfect paste from {}", .{Bleach.Html{ .text = gist.owner }});
const gist = Gist.open(vrs.alloc, hash[0..64].*) catch return error.Unknown;
const files = toTemplate(vrs.alloc, gist.files) catch return error.Unknown;
const og = try std.fmt.allocPrint(vrs.alloc, "A perfect paste from {}", .{Bleach.Html{ .text = gist.owner }});
var page = GistPage.init(.{
.meta_head = .{
.open_graph = .{
@@ -156,6 +150,6 @@ fn view(ctx: *Verse) Error!void {
.gist_files = files,
});
 
return ctx.sendPage(&page);
return vrs.sendPage(&page);
} else return error.Unrouteable;
}
 
src/endpoints/repos.zig added: 120, removed: 101, total 19
@@ -290,20 +290,19 @@ fn list(ctx: *Verse) Error!void {
};
}
 
var btns = [1]Template.Structs.NavButtons{.{
.name = "inbox",
.extra = 0,
.url = "/inbox",
}};
//var btns = [1]Template.Structs.NavButtons{.{
// .name = "inbox",
// .extra = 0,
// .url = "/inbox",
//}};
 
var page = ReposPage.init(.{
.meta_head = .{ .open_graph = .{} },
.body_header = .{
.nav = .{
.nav_auth = undefined,
.nav_buttons = &btns,
},
},
.body_header = (ctx.route_data.get(
"body_header",
*const S.BodyHeaderHtml,
) catch return error.Unknown).*,
 
.buttons = .{ .buttons = repo_buttons },
.repo_list = repos_compiled,
});
@@ -492,11 +491,14 @@ fn blame(ctx: *Verse) Error!void {
}
 
const wrapped_blames = try wrapLineNumbersBlame(ctx.alloc, parsed.lines, parsed.map, rd.name);
var btns = navButtons(ctx) catch return error.Unknown;
//var btns = navButtons(ctx) catch return error.Unknown;
 
var page = BlamePage.init(.{
.meta_head = .{ .open_graph = .{} },
.body_header = .{ .nav = .{ .nav_auth = undefined, .nav_buttons = &btns } },
.body_header = (ctx.route_data.get(
"body_header",
*const S.BodyHeaderHtml,
) catch return error.Unknown).*,
.filename = Bleach.Html.sanitizeAlloc(ctx.alloc, blame_file) catch unreachable,
.blame_lines = wrapped_blames,
});
@@ -561,59 +563,64 @@ fn excludedExt(name: []const u8) bool {
 
const BlobPage = Template.PageData("blob.html");
 
fn blob(ctx: *Verse, repo: *Git.Repo, pfiles: Git.Tree) Error!void {
fn blob(vrs: *Verse, repo: *Git.Repo, pfiles: Git.Tree) Error!void {
var blb: Git.Blob = undefined;
 
var files = pfiles;
search: while (ctx.uri.next()) |bname| {
search: while (vrs.uri.next()) |bname| {
for (files.blobs) |obj| {
if (std.mem.eql(u8, bname, obj.name)) {
blb = obj;
if (obj.isFile()) {
if (ctx.uri.next()) |_| return error.InvalidURI;
if (vrs.uri.next()) |_| return error.InvalidURI;
break :search;
}
const treeobj = repo.loadObject(ctx.alloc, obj.sha) catch return error.Unknown;
files = Git.Tree.initOwned(obj.sha, ctx.alloc, treeobj) catch return error.Unknown;
const treeobj = repo.loadObject(vrs.alloc, obj.sha) catch return error.Unknown;
files = Git.Tree.initOwned(obj.sha, vrs.alloc, treeobj) catch return error.Unknown;
continue :search;
}
} else return error.InvalidURI;
}
 
var resolve = repo.loadBlob(ctx.alloc, blb.sha) catch return error.Unknown;
var resolve = repo.loadBlob(vrs.alloc, blb.sha) catch return error.Unknown;
if (!resolve.isFile()) return error.Unknown;
var formatted: []const u8 = undefined;
if (Highlight.Language.guessFromFilename(blb.name)) |lang| {
const pre = try Highlight.highlight(ctx.alloc, lang, resolve.data.?);
const pre = try Highlight.highlight(vrs.alloc, lang, resolve.data.?);
formatted = pre[28..][0 .. pre.len - 38];
} else if (excludedExt(blb.name)) {
formatted = "This file type is currently unsupported";
} else {
formatted = Bleach.Html.sanitizeAlloc(ctx.alloc, resolve.data.?) catch return error.Unknown;
formatted = Bleach.Html.sanitizeAlloc(vrs.alloc, resolve.data.?) catch return error.Unknown;
}
 
const wrapped = try wrapLineNumbers(ctx.alloc, formatted);
const wrapped = try wrapLineNumbers(vrs.alloc, formatted);
 
ctx.uri.reset();
_ = ctx.uri.next();
const uri_repo = ctx.uri.next() orelse return error.Unrouteable;
_ = ctx.uri.next();
const uri_filename = Bleach.Html.sanitizeAlloc(ctx.alloc, ctx.uri.rest()) catch return error.Unknown;
vrs.uri.reset();
_ = vrs.uri.next();
const uri_repo = vrs.uri.next() orelse return error.Unrouteable;
_ = vrs.uri.next();
const uri_filename = Bleach.Html.sanitizeAlloc(vrs.alloc, vrs.uri.rest()) catch return error.Unknown;
 
ctx.status = .ok;
vrs.status = .ok;
 
var btns = navButtons(ctx) catch return error.Unknown;
var btns = navButtons(vrs) catch return error.Unknown;
// TODO fixme
_ = &btns;
 
var page = BlobPage.init(.{
.meta_head = .{ .open_graph = .{} },
.body_header = .{ .nav = .{ .nav_auth = undefined, .nav_buttons = &btns } },
.body_header = (vrs.route_data.get(
"body_header",
*const S.BodyHeaderHtml,
) catch return error.Unknown).*,
.repo = uri_repo,
.uri_filename = uri_filename,
.filename = blb.name,
.blob_lines = wrapped,
});
 
try ctx.sendPage(&page);
try vrs.sendPage(&page);
}
 
fn mkTree(a: Allocator, repo: *const Git.Repo, uri: *UriIter, pfiles: Git.Tree) !Git.Tree {
@@ -785,11 +792,14 @@ fn tree(ctx: *Verse, repo: *Git.Repo, files: *Git.Tree) Error!void {
}
}
 
var btns = navButtons(ctx) catch return error.Unknown;
//var btns = navButtons(ctx) catch return error.Unknown;
 
var page = TreePage.init(.{
.meta_head = .{ .open_graph = .{} },
.body_header = .{ .nav = .{ .nav_auth = undefined, .nav_buttons = &btns } },
.body_header = (ctx.route_data.get(
"body_header",
*const S.BodyHeaderHtml,
) catch return error.Unknown).*,
.upstream = null,
.repo_name = rd.name,
.repo = try allocPrint(ctx.alloc, "{s}", .{repo_data[0]}),
@@ -819,10 +829,13 @@ fn tagsList(ctx: *Verse) Error!void {
html.name = tag.name;
}
 
var btns = navButtons(ctx) catch return error.Unknown;
//var btns = navButtons(ctx) catch return error.Unknown;
var page = TagPage.init(.{
.meta_head = .{ .open_graph = .{} },
.body_header = .{ .nav = .{ .nav_auth = undefined, .nav_buttons = &btns } },
.body_header = (ctx.route_data.get(
"body_header",
*const S.BodyHeaderHtml,
) catch return error.Unknown).*,
.upstream = null,
.tags = tstack,
});
 
src/endpoints/settings.zig added: 120, removed: 101, total 19
@@ -13,19 +13,19 @@ pub const endpoints = [_]Router.Match{
 
const SettingsPage = Template.PageData("settings.html");
 
fn default(ctx: *Verse) Router.Error!void {
try ctx.auth.requireValid();
fn default(vrs: *Verse) Router.Error!void {
try vrs.auth.requireValid();
 
var blocks: []S.ConfigBlocks = &[0]S.ConfigBlocks{};
 
if (Ini.global_config) |cfg| {
blocks = try ctx.alloc.alloc(Template.Structs.ConfigBlocks, cfg.ns.len);
blocks = try vrs.alloc.alloc(Template.Structs.ConfigBlocks, cfg.ns.len);
for (cfg.ns, 0..) |ns, i| {
blocks[i] = .{
.config_name = ns.name,
.config_text = ns.block,
.count = try std.fmt.allocPrint(
ctx.alloc,
vrs.alloc,
"{}",
.{std.mem.count(u8, ns.block, "\n") + 2},
),
@@ -33,14 +33,13 @@ fn default(ctx: *Verse) Router.Error!void {
}
}
 
const btns = [1]Template.Structs.NavButtons{.{ .name = "inbox", .extra = 0, .url = "/inbox" }};
var page = SettingsPage.init(.{
.meta_head = .{ .open_graph = .{} },
.body_header = .{ .nav = .{ .nav_auth = undefined, .nav_buttons = &btns } },
.body_header = (vrs.route_data.get("body_header", *const S.BodyHeaderHtml) catch return error.Unknown).*,
.config_blocks = blocks[0..],
});
 
try ctx.sendPage(&page);
try vrs.sendPage(&page);
}
 
const SettingsReq = struct {
@@ -48,14 +47,14 @@ const SettingsReq = struct {
block_text: [][]const u8,
};
 
fn post(ctx: *Verse) Router.Error!void {
try ctx.auth.requireValid();
fn post(vrs: *Verse) Router.Error!void {
try vrs.auth.requireValid();
 
const udata = RequestData(SettingsReq).initMap(ctx.alloc, ctx.reqdata) catch return error.BadData;
const udata = RequestData(SettingsReq).initMap(vrs.alloc, vrs.reqdata) catch return error.BadData;
 
for (udata.block_name, udata.block_text) |name, text| {
std.debug.print("block data:\nname '{s}'\ntext '''{s}'''\n", .{ name, text });
}
 
return ctx.redirect("/settings", true) catch unreachable;
return vrs.redirect("/settings", true) catch unreachable;
}
 
src/ini.zig added: 120, removed: 101, total 19
@@ -167,15 +167,14 @@ pub fn init(a: Allocator, data: []const u8) !Config {
/// I'm not happy with this API. I think I deleted it once already... deleted
/// twice incoming!
pub fn initOwned(a: Allocator, data: []u8) !Config {
var c =
try init(a, data);
var c = try init(a, data);
c.owned = data;
return c;
}
 
pub fn fromFile(a: Allocator, file: std.fs.File) !Config {
const data = try file.readToEndAlloc(a, 1 <<| 18);
return try init(a, data);
return try initOwned(a, data);
}
 
pub var global_config: ?*const Config = null;
 
src/main.zig added: 120, removed: 101, total 19
@@ -133,4 +133,5 @@ pub fn main() !void {
}
std.posix.exit(1);
};
agent_config.running = false;
}
 
src/repos.zig added: 120, removed: 101, total 19
@@ -1,5 +1,6 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const sleep = std.time.sleep;
 
const Git = @import("git.zig");
const Ini = @import("ini.zig");
@@ -28,8 +29,9 @@ pub fn containsName(name: []const u8) bool {
}
 
pub const AgentConfig = struct {
const SECONDS = 1000 * 1000 * 1000;
running: bool = true,
sleep_for: usize = 60 * 60 * 1000 * 1000 * 1000,
sleep_for: usize = 60 * 60 * SECONDS,
g_config: *Ini.Config,
};
 
@@ -76,9 +78,8 @@ pub fn updateThread(cfg: *AgentConfig) void {
}
}
 
std.time.sleep(cfg.sleep_for);
//std.time.sleep(1000 * 1000 * 1000);
while (cfg.running) {
sleep(cfg.sleep_for / 60 / 6);
while (cfg.running) running: {
for (names) |rname| {
const dirname = std.fmt.bufPrint(&name_buffer, "repos/{s}", .{rname}) catch return;
const dir = std.fs.cwd().openDir(dirname, .{}) catch continue;
@@ -134,6 +135,13 @@ pub fn updateThread(cfg: *AgentConfig) void {
}
}
}
std.time.sleep(cfg.sleep_for);
 
var qi: usize = 60 * 60;
while (qi > 0) {
qi -|= 1;
sleep(cfg.sleep_for / 60 / 60);
if (!cfg.running) break :running;
}
}
std.debug.print("update thread done!\n", .{});
}
 
src/srctree.zig added: 120, removed: 101, total 19
@@ -1,4 +1,5 @@
const std = @import("std");
const eql = std.mem.eql;
const Verse = @import("verse");
const Router = Verse.Router;
const Template = Verse.Template;
@@ -40,28 +41,30 @@ pub const routes = [_]Match{
 
const E404Page = Template.PageData("4XX.html");
 
fn notFound(ctx: *Verse) Router.Error!void {
// TODO fix this
@import("std").debug.print("404 for route\n", .{});
ctx.status = .not_found;
fn notFound(vrs: *Verse) Router.Error!void {
std.debug.print("404 for route\n", .{});
vrs.status = .not_found;
var page = E404Page.init(.{});
ctx.sendPage(&page) catch unreachable;
vrs.sendPage(&page) catch unreachable;
}
 
pub fn router(ctx: *Verse) Router.Error!BuildFn {
pub fn router(vrs: *Verse) Router.Error!BuildFn {
// var i_count: usize = 0;
// var itr = Types.Delta.iterator(ctx.alloc, "");
// var itr = Types.Delta.iterator(vrs.alloc, "");
// while (itr.next()) |it| {
// i_count += 1;
// it.raze(ctx.alloc);
// it.raze(vrs.alloc);
// }
 
return Router.router(ctx, &routes);
return Router.router(vrs, &routes);
}
 
pub fn builder(ctx: *Verse, call: BuildFn) void {
return call(ctx) catch |err| switch (err) {
error.InvalidURI => builder(ctx, notFound), // TODO catch inline
pub fn builder(vrs: *Verse, call: BuildFn) void {
const bh = vrs.alloc.create(Template.Structs.BodyHeaderHtml) catch unreachable;
const btns = [1]Template.Structs.NavButtons{.{ .name = "inbox", .extra = 0, .url = "/inbox" }};
bh.* = .{ .nav = .{ .nav_auth = "Public2", .nav_buttons = &btns } };
vrs.route_data.add("body_header", bh) catch {};
return call(vrs) catch |err| switch (err) {
error.InvalidURI => builder(vrs, notFound), // TODO catch inline
error.BrokenPipe => std.debug.print("client disconnect", .{}),
error.Unrouteable => {
std.debug.print("Unrouteable", .{});
@@ -82,11 +85,11 @@ pub fn builder(ctx: *Verse, call: BuildFn) void {
error.BadData,
error.DataMissing,
=> {
std.debug.print("Abusive {} because {}", .{ ctx.request, err });
for (ctx.request.raw.zwsgi.vars) |vars| {
std.debug.print("Abusive {} because {}", .{ vrs.request, err });
for (vrs.request.raw.zwsgi.vars) |vars| {
std.debug.print("Abusive var '{s}' => '''{s}'''", .{ vars.key, vars.val });
}
if (ctx.reqdata.post) |post_data| {
if (vrs.reqdata.post) |post_data| {
std.debug.print("post data => '''{s}'''", .{post_data.rawpost});
}
},