Add basic webpage
This commit is contained in:
parent
721bf7e61a
commit
76b9018297
11 changed files with 73 additions and 26 deletions
|
@ -7,14 +7,18 @@ const util = @import("util");
|
|||
const query_utils = @import("./query.zig");
|
||||
const json_utils = @import("./json.zig");
|
||||
|
||||
pub const auth = @import("./controllers/auth.zig");
|
||||
pub const communities = @import("./controllers/communities.zig");
|
||||
pub const invites = @import("./controllers/invites.zig");
|
||||
pub const users = @import("./controllers/users.zig");
|
||||
pub const follows = @import("./controllers/users/follows.zig");
|
||||
pub const notes = @import("./controllers/notes.zig");
|
||||
pub const streaming = @import("./controllers/streaming.zig");
|
||||
pub const timelines = @import("./controllers/timelines.zig");
|
||||
pub const auth = @import("./controllers/api/auth.zig");
|
||||
pub const communities = @import("./controllers/api/communities.zig");
|
||||
pub const invites = @import("./controllers/api/invites.zig");
|
||||
pub const users = @import("./controllers/api/users.zig");
|
||||
pub const follows = @import("./controllers/api/users/follows.zig");
|
||||
pub const notes = @import("./controllers/api/notes.zig");
|
||||
pub const streaming = @import("./controllers/api/streaming.zig");
|
||||
pub const timelines = @import("./controllers/api/timelines.zig");
|
||||
|
||||
const web = struct {
|
||||
const index = @import("./controllers/web/index.zig");
|
||||
};
|
||||
|
||||
pub fn routeRequest(api_source: anytype, req: *http.Request, res: *http.Response, alloc: std.mem.Allocator) void {
|
||||
// TODO: hashmaps?
|
||||
|
@ -50,6 +54,8 @@ const routes = .{
|
|||
follows.create,
|
||||
follows.query_followers,
|
||||
follows.query_following,
|
||||
|
||||
web.index,
|
||||
};
|
||||
|
||||
pub fn Context(comptime Route: type) type {
|
||||
|
@ -87,7 +93,7 @@ pub fn Context(comptime Route: type) type {
|
|||
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] == ':') {
|
||||
if (route_segment.len > 0 and route_segment[0] == ':') {
|
||||
const A = @TypeOf(@field(args, route_segment[1..]));
|
||||
@field(args, route_segment[1..]) = parseArg(A, path_segment) catch return null;
|
||||
} else {
|
||||
|
@ -216,22 +222,16 @@ pub const Response = struct {
|
|||
|
||||
/// Write a response with no body, only a given status
|
||||
pub fn status(self: *Self, status_code: http.Status) !void {
|
||||
std.debug.assert(!self.opened);
|
||||
self.opened = true;
|
||||
|
||||
var stream = try self.res.open(status_code, &self.headers);
|
||||
var stream = try self.open(status_code);
|
||||
defer stream.close();
|
||||
try stream.finish();
|
||||
}
|
||||
|
||||
/// Write a request body as json
|
||||
pub fn json(self: *Self, status_code: http.Status, response_body: anytype) !void {
|
||||
std.debug.assert(!self.opened);
|
||||
self.opened = true;
|
||||
|
||||
try self.headers.put("Content-Type", "application/json");
|
||||
|
||||
var stream = try self.res.open(status_code, &self.headers);
|
||||
var stream = try self.open(status_code);
|
||||
defer stream.close();
|
||||
|
||||
const writer = stream.writer();
|
||||
|
@ -240,6 +240,13 @@ pub const Response = struct {
|
|||
try stream.finish();
|
||||
}
|
||||
|
||||
pub fn open(self: *Self, status_code: http.Status) !http.Response.Stream {
|
||||
std.debug.assert(!self.opened);
|
||||
self.opened = true;
|
||||
|
||||
return try self.res.open(status_code, &self.headers);
|
||||
}
|
||||
|
||||
/// Prints the given error as json
|
||||
pub fn err(self: *Self, status_code: http.Status, message: []const u8, details: anytype) !void {
|
||||
return self.json(status_code, .{
|
||||
|
@ -251,6 +258,7 @@ pub const Response = struct {
|
|||
/// Signals that the HTTP connection should be hijacked without writing a
|
||||
/// response beforehand.
|
||||
pub fn hijack(self: *Self) *http.Response {
|
||||
std.debug.assert(!self.opened);
|
||||
self.opened = true;
|
||||
return self.res;
|
||||
}
|
||||
|
|
|
@ -1,12 +1,7 @@
|
|||
const std = @import("std");
|
||||
const api = @import("api");
|
||||
const util = @import("util");
|
||||
const query_utils = @import("../query.zig");
|
||||
const controller_utils = @import("../controllers.zig").helpers;
|
||||
const controller_utils = @import("../../controllers.zig").helpers;
|
||||
|
||||
const QueryArgs = api.CommunityQueryArgs;
|
||||
const Uuid = util.Uuid;
|
||||
const DateTime = util.DateTime;
|
||||
|
||||
pub const create = struct {
|
||||
pub const method = .POST;
|
|
@ -1,7 +1,6 @@
|
|||
const std = @import("std");
|
||||
const api = @import("api");
|
||||
const query_utils = @import("../query.zig");
|
||||
const controller_utils = @import("../controllers.zig").helpers;
|
||||
const controller_utils = @import("../../controllers.zig").helpers;
|
||||
|
||||
pub const global = struct {
|
||||
pub const method = .GET;
|
|
@ -1,6 +1,6 @@
|
|||
const api = @import("api");
|
||||
const util = @import("util");
|
||||
const controller_utils = @import("../../controllers.zig").helpers;
|
||||
const controller_utils = @import("../../../controllers.zig").helpers;
|
||||
|
||||
const Uuid = util.Uuid;
|
||||
|
25
src/main/controllers/web/index.fmt.html
Normal file
25
src/main/controllers/web/index.fmt.html
Normal file
|
@ -0,0 +1,25 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>{[community_name]s}</title>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>{[community_name]s}</h1>
|
||||
Cluster Admin pseudocommunity
|
||||
</header>
|
||||
<form action="/login" method="post">
|
||||
<h2>Login</h2>
|
||||
<label>
|
||||
Username
|
||||
<div>@<input type="text" name="username" placeholder="xion" />@{[community_host]s}</div>
|
||||
</label>
|
||||
<label>
|
||||
Password
|
||||
<div><input type="password" name="password" placeholder="hunter2" /></div>
|
||||
</label>
|
||||
<button type="submit">Login</button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
20
src/main/controllers/web/index.zig
Normal file
20
src/main/controllers/web/index.zig
Normal file
|
@ -0,0 +1,20 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub const path = "/";
|
||||
pub const method = .GET;
|
||||
|
||||
pub fn handler(_: anytype, res: anytype, srv: anytype) !void {
|
||||
try res.headers.put("Content-Type", "text/html");
|
||||
|
||||
var stream = try res.open(.ok);
|
||||
defer stream.close();
|
||||
|
||||
try std.fmt.format(stream.writer(), template, .{
|
||||
.community_name = srv.community.name,
|
||||
.community_host = srv.community.host,
|
||||
});
|
||||
|
||||
try stream.finish();
|
||||
}
|
||||
|
||||
const template = @embedFile("./index.fmt.html");
|
Loading…
Reference in a new issue