srctree

Gregory Mullen parent b077040d d06318ac
wire up api/repo with a naive impl

inlinesplit
src/api.zig added: 72, removed: 29, total 43
@@ -1,10 +1,12 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const routes = @import("routes.zig");
const ROUTE = routes.ROUTE;
const Context = @import("context.zig");
pub const Routes = @import("routes.zig");
const ROUTE = Routes.ROUTE;
pub const Context = @import("context.zig");
 
const endpoints = [_]routes.MatchRouter{
const Repo = @import("api/repo.zig");
 
const endpoints = [_]Routes.Match{
ROUTE("v0", router),
ROUTE("v1", router),
 
@@ -13,7 +15,7 @@ const endpoints = [_]routes.MatchRouter{
ROUTE("issue", issue),
ROUTE("network", router),
ROUTE("patch", diff),
ROUTE("repo", repo),
ROUTE("repo", Repo.repo),
ROUTE("user", user),
};
 
@@ -30,18 +32,20 @@ const APIRouteData = struct {
}
};
 
pub fn router(ctx: *Context) routes.Error!routes.Callable {
pub fn router(ctx: *Context) Routes.Error!Routes.Callable {
const uri_api = ctx.uri.next() orelse return heartbeat;
if (!std.mem.eql(u8, uri_api, "api")) return heartbeat;
const rd = try APIRouteData.init(ctx.alloc);
ctx.route_ctx = rd;
 
return heartbeat;
return Routes.router(ctx, &endpoints);
}
 
const Diff = struct {
sha: []const u8,
};
 
fn diff(ctx: *Context) routes.Error!void {
fn diff(ctx: *Context) Routes.Error!void {
return try ctx.sendJSON([0]Diff{});
}
 
@@ -49,7 +53,7 @@ const HeartBeat = struct {
nice: usize = 0,
};
 
fn heartbeat(ctx: *Context) routes.Error!void {
fn heartbeat(ctx: *Context) Routes.Error!void {
return try ctx.sendJSON(HeartBeat{ .nice = 69 });
}
 
@@ -57,7 +61,7 @@ const Issue = struct {
index: usize,
};
 
fn issue(ctx: *Context) routes.Error!void {
fn issue(ctx: *Context) Routes.Error!void {
return try ctx.sendJSON([0]Issue{});
}
 
@@ -76,7 +80,7 @@ const Network = struct {
networks: []RemotePeer,
};
 
fn network(ctx: *Context) routes.Error!void {
fn network(ctx: *Context) Routes.Error!void {
return try ctx.sendJSON(Network{ .networks = [0].{} });
}
 
@@ -84,20 +88,10 @@ const Patch = struct {
patch: []const u8,
};
 
fn patch(ctx: *Context) routes.Error!void {
fn patch(ctx: *Context) Routes.Error!void {
return try ctx.sendJSON(Patch{ .patch = [0].{} });
}
 
const Repo = struct {
name: []const u8,
head: []const u8,
updated: []const u8,
};
 
fn repo(ctx: *Context) routes.Error!void {
return try ctx.sendJSON([0]Repo{});
}
 
const Flex = struct {
days: []const Day,
 
@@ -106,7 +100,7 @@ const Flex = struct {
};
};
 
fn flex(ctx: *Context) routes.Error!void {
fn flex(ctx: *Context) Routes.Error!void {
return try ctx.sendJSON([0]Flex{});
}
 
@@ -115,6 +109,6 @@ const User = struct {
email: []const u8,
};
 
fn user(ctx: *Context) routes.Error!void {
return try ctx.sendJSON([0]user{});
fn user(ctx: *Context) Routes.Error!void {
return try ctx.sendJSON([0]User{});
}
 
filename was Deleted added: 72, removed: 29, total 43
@@ -0,0 +1,47 @@
const std = @import("std");
const API = @import("../api.zig");
const Bleach = @import("../bleach.zig");
const Git = @import("../git.zig");
 
pub const Repo = struct {
name: []const u8,
head: []const u8,
updated: []const u8,
};
 
pub const RepoRequest = struct {
name: []const u8,
};
 
pub fn repo(ctx: *API.Context) API.Routes.Error!void {
const req = try ctx.reqdata.validate(RepoRequest);
 
const dname = try ctx.alloc.alloc(u8, req.name.len);
const rname = Bleach.sanitize(req.name, dname, .{ .rules = .filename }) catch return error.BadData;
if (!std.mem.eql(u8, req.name, rname)) return error.Abusive;
 
var cwd = std.fs.cwd();
const filename = try std.fmt.allocPrint(ctx.alloc, "./repos/{s}", .{rname});
const dir = cwd.openDir(filename, .{}) catch {
ctx.response.status = .not_found;
return try ctx.sendJSON([0]Repo{});
};
var gitrepo = Git.Repo.init(dir) catch {
ctx.response.status = .service_unavailable;
return try ctx.sendJSON([0]Repo{});
};
gitrepo.loadData(ctx.alloc) catch return error.Unknown;
defer gitrepo.raze(ctx.alloc);
 
const head = switch (gitrepo.HEAD(ctx.alloc) catch return error.Unknown) {
.branch => |b| b.sha,
.sha => |s| s,
else => return error.NotImplemented,
};
 
return try ctx.sendJSON([1]Repo{.{
.name = rname,
.head = head,
.updated = "undefined",
}});
}
 
src/context.zig added: 72, removed: 29, total 43
@@ -133,7 +133,9 @@ pub fn sendJSON(ctx: *Context, json: anytype) Error!void {
else => unreachable,
};
 
const data = std.json.stringifyAlloc(ctx.alloc, json, .{}) catch |err| {
const data = std.json.stringifyAlloc(ctx.alloc, json, .{
.emit_null_optional_fields = false,
}) catch |err| {
std.debug.print("Error trying to print json {}\n", .{err});
return error.Unknown;
};