2022-07-13 07:57:21 +00:00
|
|
|
const std = @import("std");
|
|
|
|
const root = @import("root");
|
|
|
|
const builtin = @import("builtin");
|
|
|
|
const http = @import("http");
|
2022-10-08 20:47:54 +00:00
|
|
|
const api = @import("api");
|
2022-10-08 07:51:22 +00:00
|
|
|
const util = @import("util");
|
2022-10-09 09:05:01 +00:00
|
|
|
const query_utils = @import("./query.zig");
|
2022-10-10 02:06:11 +00:00
|
|
|
const json_utils = @import("./json.zig");
|
2022-07-13 07:57:21 +00:00
|
|
|
|
2022-07-22 06:53:05 +00:00
|
|
|
pub const auth = @import("./controllers/auth.zig");
|
2022-09-08 02:19:46 +00:00
|
|
|
pub const communities = @import("./controllers/communities.zig");
|
2022-09-08 05:10:58 +00:00
|
|
|
pub const invites = @import("./controllers/invites.zig");
|
2022-09-08 06:56:29 +00:00
|
|
|
pub const users = @import("./controllers/users.zig");
|
2022-09-08 07:52:23 +00:00
|
|
|
pub const notes = @import("./controllers/notes.zig");
|
2022-07-22 06:53:05 +00:00
|
|
|
|
2022-10-10 02:06:11 +00:00
|
|
|
pub fn routeRequest(api_source: anytype, request: http.Request, response: http.Response, alloc: std.mem.Allocator) void {
|
|
|
|
// TODO: hashmaps?
|
|
|
|
inline for (routes) |route| {
|
|
|
|
if (Context(route).matchAndHandle(api_source, request, response, alloc)) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo 404
|
|
|
|
}
|
|
|
|
|
2022-10-10 02:31:15 +00:00
|
|
|
const routes = .{ sample_api, invites.create };
|
2022-10-10 02:06:11 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
pub const sample_api = struct {
|
|
|
|
const Self = @This();
|
2022-07-13 07:57:21 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
pub const method = .POST;
|
|
|
|
pub const path = "/notes/:id/reacts";
|
|
|
|
pub const content_type = "application/json";
|
2022-07-13 07:57:21 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
pub const Args = struct {
|
|
|
|
id: []const u8,
|
|
|
|
};
|
2022-07-13 07:57:21 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
pub const Body = struct {
|
2022-10-10 02:06:11 +00:00
|
|
|
content: util.Uuid,
|
2022-10-09 09:05:01 +00:00
|
|
|
};
|
2022-07-13 07:57:21 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
pub const Query = struct {
|
|
|
|
arg: []const u8 = "",
|
|
|
|
};
|
2022-07-13 07:57:21 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
pub fn handler(ctx: Context(Self), response: *Response, _: api.ApiSource.Conn) !void {
|
2022-10-10 02:06:11 +00:00
|
|
|
std.log.debug("{}", .{ctx.body.content});
|
2022-10-09 09:05:01 +00:00
|
|
|
try response.writeJson(.created, ctx.query);
|
2022-07-13 07:57:21 +00:00
|
|
|
}
|
2022-10-09 09:05:01 +00:00
|
|
|
};
|
2022-07-13 07:57:21 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
pub fn Context(comptime Route: type) type {
|
|
|
|
return struct {
|
|
|
|
const Self = @This();
|
|
|
|
|
|
|
|
pub const Args = if (@hasDecl(Route, "Args")) Route.Args else void;
|
2022-10-10 02:31:15 +00:00
|
|
|
|
|
|
|
// TODO: if controller does not provide a body type, maybe we should
|
|
|
|
// leave it as a simple reader instead of void
|
2022-10-09 09:05:01 +00:00
|
|
|
pub const Body = if (@hasDecl(Route, "Body")) Route.Body else void;
|
2022-10-10 02:31:15 +00:00
|
|
|
|
|
|
|
// TODO: if controller does not provide a query type, maybe we should
|
|
|
|
// leave it as a simple string instead of void
|
2022-10-09 09:05:01 +00:00
|
|
|
pub const Query = if (@hasDecl(Route, "Query")) Route.Query else void;
|
|
|
|
|
|
|
|
allocator: std.mem.Allocator,
|
|
|
|
|
|
|
|
method: http.Method,
|
|
|
|
request_line: []const u8,
|
|
|
|
headers: http.Headers,
|
|
|
|
|
|
|
|
args: Args,
|
|
|
|
body: Body,
|
|
|
|
query: Query,
|
|
|
|
|
|
|
|
fn parseArgs(path: []const u8) ?Args {
|
|
|
|
var args: Route.Args = undefined;
|
|
|
|
var path_iter = util.PathIter.from(path);
|
|
|
|
comptime var route_iter = util.PathIter.from(Route.path);
|
|
|
|
inline while (comptime route_iter.next()) |route_segment| {
|
|
|
|
const path_segment = path_iter.next() orelse return null;
|
|
|
|
if (route_segment[0] == ':') {
|
|
|
|
@field(args, route_segment[1..]) = path_segment;
|
|
|
|
} else {
|
|
|
|
if (!std.ascii.eqlIgnoreCase(route_segment, path_segment)) return null;
|
|
|
|
}
|
|
|
|
}
|
2022-07-21 03:39:43 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
return args;
|
|
|
|
}
|
2022-07-13 07:57:21 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
pub fn matchAndHandle(api_source: *api.ApiSource, ctx: http.server.Context, alloc: std.mem.Allocator) bool {
|
|
|
|
const req = ctx.request;
|
|
|
|
if (req.method != Route.method) return false;
|
|
|
|
var path = std.mem.sliceTo(std.mem.sliceTo(req.path, '#'), '?');
|
|
|
|
var args: Route.Args = parseArgs(path) orelse return false;
|
2022-07-13 07:57:21 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
var response = Response{ .headers = http.Headers.init(alloc), .ctx = ctx };
|
|
|
|
defer response.headers.deinit();
|
|
|
|
|
|
|
|
var self = Self{
|
|
|
|
.allocator = alloc,
|
|
|
|
|
|
|
|
.method = req.method,
|
|
|
|
.request_line = req.path,
|
|
|
|
.headers = req.headers,
|
|
|
|
|
|
|
|
.args = args,
|
|
|
|
.body = undefined,
|
|
|
|
.query = undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.prepareAndHandle(api_source, req, &response);
|
|
|
|
|
|
|
|
return true;
|
2022-10-08 07:51:22 +00:00
|
|
|
}
|
2022-10-09 09:05:01 +00:00
|
|
|
|
|
|
|
fn errorHandler(response: *Response, status: http.Status) void {
|
|
|
|
response.writeStatus(status) catch unreachable;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prepareAndHandle(self: *Self, api_source: anytype, req: http.Request, response: *Response) void {
|
|
|
|
self.parseBody(req) catch return errorHandler(response, .bad_request);
|
|
|
|
defer self.freeBody();
|
|
|
|
|
|
|
|
self.parseQuery() catch return errorHandler(response, .bad_request);
|
|
|
|
|
|
|
|
var api_conn = self.getApiConn(api_source) catch return errorHandler(response, .internal_server_error); // TODO
|
|
|
|
defer api_conn.close();
|
|
|
|
|
|
|
|
self.handle(response, api_conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parseBody(self: *Self, req: http.Request) !void {
|
|
|
|
if (Body != void) {
|
|
|
|
const body = req.body orelse return error.NoBody;
|
2022-10-10 02:06:11 +00:00
|
|
|
self.body = try json_utils.parse(Body, body, self.allocator);
|
2022-10-08 07:51:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
fn freeBody(self: *Self) void {
|
|
|
|
if (Body != void) {
|
2022-10-10 02:06:11 +00:00
|
|
|
json_utils.parseFree(self.body, self.allocator);
|
2022-10-09 09:05:01 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-08 07:51:22 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
fn parseQuery(self: *Self) !void {
|
|
|
|
if (Query != void) {
|
|
|
|
const path = std.mem.sliceTo(self.request_line, '?');
|
|
|
|
const q = std.mem.sliceTo(self.request_line[path.len..], '#');
|
2022-10-08 07:51:22 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
self.query = try query_utils.parseQuery(Query, q);
|
2022-10-08 07:51:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
fn handle(self: Self, response: *Response, api_conn: anytype) void {
|
|
|
|
Route.handler(self, response, api_conn) catch |err| std.log.err("{}", .{err});
|
|
|
|
}
|
2022-07-18 06:11:42 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
fn getApiConn(self: *Self, api_source: anytype) !api.ApiSource.Conn {
|
|
|
|
const host = self.headers.get("Host") orelse return error.NoHost;
|
|
|
|
const auth_header = self.headers.get("Authorization");
|
|
|
|
const token = if (auth_header) |header| blk: {
|
|
|
|
const prefix = "bearer ";
|
|
|
|
if (header.len < prefix.len) break :blk null;
|
|
|
|
if (!std.ascii.eqlIgnoreCase(prefix, header[0..prefix.len])) break :blk null;
|
|
|
|
break :blk header[prefix.len..];
|
|
|
|
} else null;
|
2022-09-05 08:52:49 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
if (token) |t| return try api_source.connectToken(host, t, self.allocator);
|
2022-07-26 02:07:05 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
return try api_source.connectUnauthorized(host, self.allocator);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2022-07-18 06:11:42 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
pub const Response = struct {
|
|
|
|
const Self = @This();
|
|
|
|
headers: http.Headers,
|
|
|
|
ctx: http.server.Context,
|
2022-07-18 06:11:42 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
pub fn writeStatus(self: *Self, status: http.Status) !void {
|
|
|
|
var stream = try self.ctx.openResponse(&self.headers, status);
|
|
|
|
defer stream.close();
|
|
|
|
try stream.finish();
|
2022-07-18 06:11:42 +00:00
|
|
|
}
|
2022-07-13 07:57:21 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
pub fn writeJson(self: *Self, status: http.Status, response_body: anytype) !void {
|
|
|
|
try self.headers.put("Content-Type", "application/json");
|
2022-07-13 07:57:21 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
var stream = try self.ctx.openResponse(&self.headers, status);
|
|
|
|
defer stream.close();
|
2022-07-13 07:57:21 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
const writer = stream.writer();
|
|
|
|
try std.json.stringify(response_body, json_options, writer);
|
2022-08-02 06:24:16 +00:00
|
|
|
|
2022-10-09 09:05:01 +00:00
|
|
|
try stream.finish();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const json_options = if (builtin.mode == .Debug)
|
|
|
|
.{
|
|
|
|
.whitespace = .{
|
|
|
|
.indent = .{ .Space = 2 },
|
|
|
|
.separator = true,
|
|
|
|
},
|
|
|
|
.string = .{ .String = .{} },
|
|
|
|
} else .{
|
|
|
|
.whitespace = .{
|
|
|
|
.indent = .None,
|
|
|
|
.separator = false,
|
|
|
|
},
|
|
|
|
.string = .{ .String = .{} },
|
|
|
|
};
|