Mvoing controllers around
This commit is contained in:
parent
99337b6429
commit
075b922ef5
5 changed files with 66 additions and 55 deletions
|
@ -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) .{
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -10,7 +10,10 @@ 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 {
|
||||||
|
pub const path = "/auth/login";
|
||||||
|
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);
|
const credentials = try utils.parseRequestBody(struct { username: []const u8, password: []const u8 }, ctx);
|
||||||
defer utils.freeRequestBody(credentials, ctx.alloc);
|
defer utils.freeRequestBody(credentials, ctx.alloc);
|
||||||
|
|
||||||
|
@ -20,9 +23,14 @@ pub fn login(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void
|
||||||
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 {
|
||||||
|
pub const path = "/auth/login";
|
||||||
|
pub const method = .GET;
|
||||||
|
|
||||||
|
pub fn handler(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
|
||||||
var api = try utils.getApiConn(srv, ctx);
|
var api = try utils.getApiConn(srv, ctx);
|
||||||
defer api.close();
|
defer api.close();
|
||||||
|
|
||||||
|
@ -31,4 +39,5 @@ pub fn verifyLogin(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs)
|
||||||
const info = api.getTokenInfo() catch unreachable;
|
const info = api.getTokenInfo() catch unreachable;
|
||||||
|
|
||||||
try utils.respondJson(ctx, .ok, info);
|
try utils.respondJson(ctx, .ok, info);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
33
src/main/controllers/communities.zig
Normal file
33
src/main/controllers/communities.zig
Normal 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);
|
||||||
|
}
|
|
@ -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,
|
||||||
|
|
Loading…
Reference in a new issue