Mvoing controllers around

This commit is contained in:
jaina heartles 2022-09-07 19:19:46 -07:00
parent 99337b6429
commit 075b922ef5
5 changed files with 66 additions and 55 deletions

View file

@ -6,12 +6,7 @@ const api = @import("./api.zig");
const Uuid = @import("util").Uuid; const Uuid = @import("util").Uuid;
pub const auth = @import("./controllers/auth.zig"); pub const auth = @import("./controllers/auth.zig");
pub const notes = @import("./controllers/notes.zig"); pub const communities = @import("./controllers/communities.zig");
pub const actors = @import("./controllers/actors.zig");
pub const admin = struct {
pub const invites = @import("./controllers/admin/invites.zig");
pub const communities = @import("./controllers/admin/communities.zig");
};
pub const utils = struct { pub const utils = struct {
const json_options = if (builtin.mode == .Debug) .{ const json_options = if (builtin.mode == .Debug) .{

View file

@ -1,30 +0,0 @@
const root = @import("root");
const http = @import("http");
const Uuid = @import("util").Uuid;
const utils = @import("../../controllers.zig").utils;
const RequestServer = root.RequestServer;
const RouteArgs = http.RouteArgs;
pub fn create(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
const opt = try utils.parseRequestBody(struct { origin: []const u8 }, ctx);
defer utils.freeRequestBody(opt, ctx.alloc);
var api = try utils.getApiConn(srv, ctx);
defer api.close();
const invite = try api.createCommunity(opt.origin);
try utils.respondJson(ctx, .created, invite);
}
pub fn get(srv: *RequestServer, ctx: *http.server.Context, args: RouteArgs) !void {
const host = args.get("host") orelse return error.NotFound;
var api = try utils.getApiConn(srv, ctx);
defer api.close();
const invite = (try api.getCommunity(host)) orelse return utils.respondError(ctx, .not_found, "Community not found");
try utils.respondJson(ctx, .ok, invite);
}

View file

@ -10,25 +10,34 @@ const utils = @import("../controllers.zig").utils;
const RequestServer = root.RequestServer; const RequestServer = root.RequestServer;
const RouteArgs = http.RouteArgs; const RouteArgs = http.RouteArgs;
pub fn login(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void { pub const login = struct {
const credentials = try utils.parseRequestBody(struct { username: []const u8, password: []const u8 }, ctx); pub const path = "/auth/login";
defer utils.freeRequestBody(credentials, ctx.alloc); pub const method = .POST;
pub fn handler(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
const credentials = try utils.parseRequestBody(struct { username: []const u8, password: []const u8 }, ctx);
defer utils.freeRequestBody(credentials, ctx.alloc);
var api = try utils.getApiConn(srv, ctx); var api = try utils.getApiConn(srv, ctx);
defer api.close(); defer api.close();
const token = try api.login(credentials.username, credentials.password); const token = try api.login(credentials.username, credentials.password);
try utils.respondJson(ctx, .ok, token); try utils.respondJson(ctx, .ok, token);
} }
};
pub fn verifyLogin(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void { pub const verify_login = struct {
var api = try utils.getApiConn(srv, ctx); pub const path = "/auth/login";
defer api.close(); pub const method = .GET;
// The self-hosted compiler doesn't like inferring this error set. pub fn handler(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
// do this for now var api = try utils.getApiConn(srv, ctx);
const info = api.getTokenInfo() catch unreachable; defer api.close();
try utils.respondJson(ctx, .ok, info); // The self-hosted compiler doesn't like inferring this error set.
} // do this for now
const info = api.getTokenInfo() catch unreachable;
try utils.respondJson(ctx, .ok, info);
}
};

View file

@ -0,0 +1,33 @@
const root = @import("root");
const http = @import("http");
const utils = @import("../controllers.zig").utils;
const RequestServer = root.RequestServer;
const RouteArgs = http.RouteArgs;
pub const create = struct {
pub const method = .GET;
pub const path = "/communities";
pub fn handler(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
const opt = try utils.parseRequestBody(struct { origin: []const u8 }, ctx);
defer utils.freeRequestBody(opt, ctx.alloc);
var api = try utils.getApiConn(srv, ctx);
defer api.close();
const invite = try api.createCommunity(opt.origin);
try utils.respondJson(ctx, .created, invite);
}
};
pub fn get(srv: *RequestServer, ctx: *http.server.Context, args: RouteArgs) !void {
const host = args.get("host") orelse return error.NotFound;
var api = try utils.getApiConn(srv, ctx);
defer api.close();
const invite = (try api.getCommunity(host)) orelse return utils.respondError(ctx, .not_found, "Community not found");
try utils.respondJson(ctx, .ok, invite);
}

View file

@ -19,10 +19,10 @@ const router = Router{
//Route.new(.POST, "/users", c.users.create), //Route.new(.POST, "/users", c.users.create),
//Route.new(.POST, "/auth/register", &c.auth.register), //Route.new(.POST, "/auth/register", &c.auth.register),
Route.new(.POST, "/login", &c.auth.login), prepare(c.auth.login),
Route.new(.GET, "/login", &c.auth.verifyLogin), prepare(c.auth.verify_login),
Route.new(.POST, "/communities", &c.admin.communities.create), prepare(c.communities.create),
//Route.new(.POST, "/invites", &c.admin.invites.create), //Route.new(.POST, "/invites", &c.admin.invites.create),
@ -40,6 +40,10 @@ const router = Router{
}, },
}; };
fn prepare(comptime route_desc: type) Route {
return Route.new(route_desc.method, route_desc.path, &route_desc.handler);
}
pub const RequestServer = struct { pub const RequestServer = struct {
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
api: api.ApiSource, api: api.ApiSource,