mirror of
https://github.com/zigzap/zap.git
synced 2025-10-21 07:34:08 +00:00
Plow through all remaining zig-0.12-dev updates 😵
This commit is contained in:
parent
da420b4114
commit
885db6aacf
9 changed files with 42 additions and 46 deletions
|
@ -16,7 +16,7 @@ const Handler = struct {
|
||||||
// check for query params (for ?terminate=true)
|
// check for query params (for ?terminate=true)
|
||||||
r.parseQuery();
|
r.parseQuery();
|
||||||
|
|
||||||
var param_count = r.getParamCount();
|
const param_count = r.getParamCount();
|
||||||
std.log.info("param_count: {}", .{param_count});
|
std.log.info("param_count: {}", .{param_count});
|
||||||
|
|
||||||
// iterate over all params
|
// iterate over all params
|
||||||
|
@ -28,7 +28,8 @@ const Handler = struct {
|
||||||
for (params.items) |kv| {
|
for (params.items) |kv| {
|
||||||
if (kv.value) |v| {
|
if (kv.value) |v| {
|
||||||
std.debug.print("\n", .{});
|
std.debug.print("\n", .{});
|
||||||
std.log.info("Param `{s}` in owned list is {any}\n", .{ kv.key.str, v });
|
// TODO: Zig fails to format 'v':
|
||||||
|
// std.log.info("Param `{s}` in owned list is {any}\n", .{ kv.key.str, v });
|
||||||
switch (v) {
|
switch (v) {
|
||||||
// single-file upload
|
// single-file upload
|
||||||
zap.Request.HttpParam.Hash_Binfile => |*file| {
|
zap.Request.HttpParam.Hash_Binfile => |*file| {
|
||||||
|
@ -87,7 +88,7 @@ pub fn main() !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{
|
var gpa = std.heap.GeneralPurposeAllocator(.{
|
||||||
.thread_safe = true,
|
.thread_safe = true,
|
||||||
}){};
|
}){};
|
||||||
var allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
Handler.alloc = allocator;
|
Handler.alloc = allocator;
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ fn setupUserData(a: std.mem.Allocator) !void {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
var a = std.heap.page_allocator;
|
const a = std.heap.page_allocator;
|
||||||
try setupUserData(a);
|
try setupUserData(a);
|
||||||
var listener = zap.HttpListener.init(.{
|
var listener = zap.HttpListener.init(.{
|
||||||
.port = 3000,
|
.port = 3000,
|
||||||
|
@ -53,7 +53,7 @@ pub fn main() !void {
|
||||||
|
|
||||||
std.debug.print(
|
std.debug.print(
|
||||||
\\ Listening on 0.0.0.0:3000
|
\\ Listening on 0.0.0.0:3000
|
||||||
\\
|
\\
|
||||||
\\ Check out:
|
\\ Check out:
|
||||||
\\ http://localhost:3000/user/1 # -- first user
|
\\ http://localhost:3000/user/1 # -- first user
|
||||||
\\ http://localhost:3000/user/2 # -- second user
|
\\ http://localhost:3000/user/2 # -- second user
|
||||||
|
|
|
@ -61,9 +61,10 @@ pub fn main() !void {
|
||||||
// iterate over all params
|
// iterate over all params
|
||||||
const params = r.parametersToOwnedList(alloc, false) catch unreachable;
|
const params = r.parametersToOwnedList(alloc, false) catch unreachable;
|
||||||
defer params.deinit();
|
defer params.deinit();
|
||||||
for (params.items) |kv| {
|
// for (params.items) |kv| {
|
||||||
std.log.info("Param `{s}` is {any}", .{ kv.key.str, kv.value });
|
// // TODO: fmt can't format kv.value
|
||||||
}
|
// // std.log.info("Param `{s}` is {any}", .{ kv.key.str, kv.value.? });
|
||||||
|
// }
|
||||||
|
|
||||||
// let's get param "one" by name
|
// let's get param "one" by name
|
||||||
std.debug.print("\n", .{});
|
std.debug.print("\n", .{});
|
||||||
|
|
|
@ -71,9 +71,8 @@ const UserMiddleWare = struct {
|
||||||
// note that the first parameter is of type *Handler, not *Self !!!
|
// note that the first parameter is of type *Handler, not *Self !!!
|
||||||
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {
|
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {
|
||||||
|
|
||||||
// this is how we would get our self pointer
|
// this is how we would get our self pointer:
|
||||||
var self = @fieldParentPtr(Self, "handler", handler);
|
// var self = @fieldParentPtr(Self, "handler", handler);
|
||||||
_ = self;
|
|
||||||
|
|
||||||
// do our work: fill in the user field of the context
|
// do our work: fill in the user field of the context
|
||||||
context.user = User{
|
context.user = User{
|
||||||
|
@ -114,9 +113,8 @@ const SessionMiddleWare = struct {
|
||||||
|
|
||||||
// note that the first parameter is of type *Handler, not *Self !!!
|
// note that the first parameter is of type *Handler, not *Self !!!
|
||||||
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {
|
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {
|
||||||
// this is how we would get our self pointer
|
// this is how we would get our self pointer:
|
||||||
var self = @fieldParentPtr(Self, "handler", handler);
|
// var self = @fieldParentPtr(Self, "handler", handler);
|
||||||
_ = self;
|
|
||||||
|
|
||||||
context.session = Session{
|
context.session = Session{
|
||||||
.info = "secret session",
|
.info = "secret session",
|
||||||
|
@ -149,10 +147,10 @@ const HtmlMiddleWare = struct {
|
||||||
|
|
||||||
// note that the first parameter is of type *Handler, not *Self !!!
|
// note that the first parameter is of type *Handler, not *Self !!!
|
||||||
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {
|
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {
|
||||||
|
_ = handler;
|
||||||
|
|
||||||
// this is how we would get our self pointer
|
// this is how we would get our self pointer:
|
||||||
var self = @fieldParentPtr(Self, "handler", handler);
|
// var self = @fieldParentPtr(Self, "handler", handler);
|
||||||
_ = self;
|
|
||||||
|
|
||||||
std.debug.print("\n\nHtmlMiddleware: handling request with context: {any}\n\n", .{context});
|
std.debug.print("\n\nHtmlMiddleware: handling request with context: {any}\n\n", .{context});
|
||||||
|
|
||||||
|
@ -190,7 +188,7 @@ pub fn main() !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{
|
var gpa = std.heap.GeneralPurposeAllocator(.{
|
||||||
.thread_safe = true,
|
.thread_safe = true,
|
||||||
}){};
|
}){};
|
||||||
var allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
SharedAllocator.init(allocator);
|
SharedAllocator.init(allocator);
|
||||||
|
|
||||||
// we create our HTML middleware component that handles the request
|
// we create our HTML middleware component that handles the request
|
||||||
|
|
|
@ -59,9 +59,8 @@ const UserMiddleWare = struct {
|
||||||
// note that the first parameter is of type *Handler, not *Self !!!
|
// note that the first parameter is of type *Handler, not *Self !!!
|
||||||
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {
|
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {
|
||||||
|
|
||||||
// this is how we would get our self pointer
|
// this is how we would get our self pointer:
|
||||||
var self = @fieldParentPtr(Self, "handler", handler);
|
// var self = @fieldParentPtr(Self, "handler", handler);
|
||||||
_ = self;
|
|
||||||
|
|
||||||
// do our work: fill in the user field of the context
|
// do our work: fill in the user field of the context
|
||||||
context.user = User{
|
context.user = User{
|
||||||
|
@ -104,9 +103,8 @@ const SessionMiddleWare = struct {
|
||||||
|
|
||||||
// note that the first parameter is of type *Handler, not *Self !!!
|
// note that the first parameter is of type *Handler, not *Self !!!
|
||||||
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {
|
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {
|
||||||
// this is how we would get our self pointer
|
// this is how we would get our self pointer:
|
||||||
var self = @fieldParentPtr(Self, "handler", handler);
|
// var self = @fieldParentPtr(Self, "handler", handler);
|
||||||
_ = self;
|
|
||||||
|
|
||||||
context.session = Session{
|
context.session = Session{
|
||||||
.info = "secret session",
|
.info = "secret session",
|
||||||
|
@ -200,7 +198,7 @@ pub fn main() !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{
|
var gpa = std.heap.GeneralPurposeAllocator(.{
|
||||||
.thread_safe = true,
|
.thread_safe = true,
|
||||||
}){};
|
}){};
|
||||||
var allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
SharedAllocator.init(allocator);
|
SharedAllocator.init(allocator);
|
||||||
|
|
||||||
// create the endpoint
|
// create the endpoint
|
||||||
|
|
|
@ -73,7 +73,7 @@ pub fn main() !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{
|
var gpa = std.heap.GeneralPurposeAllocator(.{
|
||||||
.thread_safe = true,
|
.thread_safe = true,
|
||||||
}){};
|
}){};
|
||||||
var allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var simpleRouter = zap.Router.init(allocator, .{
|
var simpleRouter = zap.Router.init(allocator, .{
|
||||||
.not_found = not_found,
|
.not_found = not_found,
|
||||||
|
|
|
@ -88,7 +88,7 @@ fn get_tag_annotation(allocator: std.mem.Allocator, tagname: []const u8) ![]cons
|
||||||
tagname,
|
tagname,
|
||||||
};
|
};
|
||||||
|
|
||||||
const result = try std.ChildProcess.exec(.{
|
const result = try std.ChildProcess.run(.{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.argv = &args,
|
.argv = &args,
|
||||||
});
|
});
|
||||||
|
@ -161,13 +161,13 @@ fn sendToDiscordPart(allocator: std.mem.Allocator, url: []const u8, message_json
|
||||||
defer http_client.deinit();
|
defer http_client.deinit();
|
||||||
|
|
||||||
// request
|
// request
|
||||||
var req = try http_client.request(.POST, uri, h, .{});
|
var req = try http_client.open(.POST, uri, h, .{});
|
||||||
defer req.deinit();
|
defer req.deinit();
|
||||||
|
|
||||||
req.transfer_encoding = .chunked;
|
req.transfer_encoding = .chunked;
|
||||||
|
|
||||||
// connect, send request
|
// connect, send request
|
||||||
try req.start();
|
try req.send(.{ .raw_uri = false });
|
||||||
|
|
||||||
// send POST payload
|
// send POST payload
|
||||||
try req.writer().writeAll(message_json);
|
try req.writer().writeAll(message_json);
|
||||||
|
@ -182,7 +182,7 @@ fn sendToDiscordPart(allocator: std.mem.Allocator, url: []const u8, message_json
|
||||||
fn sendToDiscord(allocator: std.mem.Allocator, url: []const u8, message: []const u8) !void {
|
fn sendToDiscord(allocator: std.mem.Allocator, url: []const u8, message: []const u8) !void {
|
||||||
// json payload
|
// json payload
|
||||||
// max size: 100kB
|
// max size: 100kB
|
||||||
var buf: []u8 = try allocator.alloc(u8, 100 * 1024);
|
const buf: []u8 = try allocator.alloc(u8, 100 * 1024);
|
||||||
defer allocator.free(buf);
|
defer allocator.free(buf);
|
||||||
var fba = std.heap.FixedBufferAllocator.init(buf);
|
var fba = std.heap.FixedBufferAllocator.init(buf);
|
||||||
var string = std.ArrayList(u8).init(fba.allocator());
|
var string = std.ArrayList(u8).init(fba.allocator());
|
||||||
|
@ -399,7 +399,7 @@ fn command_update_readme(allocator: std.mem.Allocator, tag: []const u8) !void {
|
||||||
// we need to put the \n back in.
|
// we need to put the \n back in.
|
||||||
// TODO: change this by using some "search" iterator that just
|
// TODO: change this by using some "search" iterator that just
|
||||||
// returns indices etc
|
// returns indices etc
|
||||||
var output_line = try std.fmt.allocPrint(allocator, "{s}\n", .{line});
|
const output_line = try std.fmt.allocPrint(allocator, "{s}\n", .{line});
|
||||||
defer allocator.free(output_line);
|
defer allocator.free(output_line);
|
||||||
_ = try writer.write(output_line);
|
_ = try writer.write(output_line);
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,22 +56,22 @@ fn showHelp() !void {
|
||||||
pub const usage_pkg =
|
pub const usage_pkg =
|
||||||
\\Usage: pkghash [options]
|
\\Usage: pkghash [options]
|
||||||
\\
|
\\
|
||||||
\\Options:
|
\\Options:
|
||||||
\\ -h --help Print this help and exit.
|
\\ -h --help Print this help and exit.
|
||||||
\\ -g --git Use git ls-files
|
\\ -g --git Use git ls-files
|
||||||
\\
|
\\
|
||||||
\\Sub-options:
|
\\Sub-options:
|
||||||
\\ --allow-directory : calc hash even if no build.zig is present
|
\\ --allow-directory : calc hash even if no build.zig is present
|
||||||
\\ applies in no-git mode only
|
\\ applies in no-git mode only
|
||||||
\\
|
\\
|
||||||
\\Sub-options for --git:
|
\\Sub-options for --git:
|
||||||
\\ --tag=<tag> : specify git tag to use in template
|
\\ --tag=<tag> : specify git tag to use in template
|
||||||
\\ defaults to tag pointing to HEAD
|
\\ defaults to tag pointing to HEAD
|
||||||
\\ --template=<file.md> : specify markdown template to render
|
\\ --template=<file.md> : specify markdown template to render
|
||||||
;
|
;
|
||||||
|
|
||||||
pub fn gitLatestTag(gpa: Allocator, pkg_dir: []const u8) ![]const u8 {
|
pub fn gitLatestTag(gpa: Allocator, pkg_dir: []const u8) ![]const u8 {
|
||||||
const result = try std.ChildProcess.exec(.{
|
const result = try std.ChildProcess.run(.{
|
||||||
.allocator = gpa,
|
.allocator = gpa,
|
||||||
.argv = &.{
|
.argv = &.{
|
||||||
"git",
|
"git",
|
||||||
|
@ -97,7 +97,7 @@ pub fn gitLatestTag(gpa: Allocator, pkg_dir: []const u8) ![]const u8 {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn gitFileList(gpa: Allocator, pkg_dir: []const u8) ![]const u8 {
|
pub fn gitFileList(gpa: Allocator, pkg_dir: []const u8) ![]const u8 {
|
||||||
const result = try std.ChildProcess.exec(.{
|
const result = try std.ChildProcess.run(.{
|
||||||
.allocator = gpa,
|
.allocator = gpa,
|
||||||
.argv = &.{
|
.argv = &.{
|
||||||
"git",
|
"git",
|
||||||
|
@ -266,8 +266,8 @@ pub fn cmdPkg(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
|
||||||
|
|
||||||
// computePackageHash will close the directory after completion
|
// computePackageHash will close the directory after completion
|
||||||
// std.debug.print("abspath: {s}\n", .{cwd_absolute_path});
|
// std.debug.print("abspath: {s}\n", .{cwd_absolute_path});
|
||||||
var cwd_copy = try fs.openIterableDirAbsolute(cwd_absolute_path, .{});
|
var cwd_copy = try fs.openDirAbsolute(cwd_absolute_path, .{ .iterate = true });
|
||||||
errdefer cwd_copy.dir.close();
|
errdefer cwd_copy.close();
|
||||||
|
|
||||||
var thread_pool: ThreadPool = undefined;
|
var thread_pool: ThreadPool = undefined;
|
||||||
try thread_pool.init(.{ .allocator = gpa });
|
try thread_pool.init(.{ .allocator = gpa });
|
||||||
|
@ -281,7 +281,7 @@ pub fn cmdPkg(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
|
||||||
};
|
};
|
||||||
break :blk try computePackageHashExcludingDirectories(
|
break :blk try computePackageHashExcludingDirectories(
|
||||||
&thread_pool,
|
&thread_pool,
|
||||||
.{ .dir = cwd_copy.dir },
|
cwd_copy,
|
||||||
excluded_directories,
|
excluded_directories,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -355,7 +355,7 @@ fn isExecutable(file: fs.File) !bool {
|
||||||
|
|
||||||
pub fn computePackageHashExcludingDirectories(
|
pub fn computePackageHashExcludingDirectories(
|
||||||
thread_pool: *ThreadPool,
|
thread_pool: *ThreadPool,
|
||||||
pkg_dir: fs.IterableDir,
|
pkg_dir: fs.Dir,
|
||||||
excluded_directories: []const []const u8,
|
excluded_directories: []const []const u8,
|
||||||
) ![Manifest.Hash.digest_length]u8 {
|
) ![Manifest.Hash.digest_length]u8 {
|
||||||
const gpa = thread_pool.allocator;
|
const gpa = thread_pool.allocator;
|
||||||
|
@ -405,7 +405,7 @@ pub fn computePackageHashExcludingDirectories(
|
||||||
.failure = undefined, // to be populated by the worker
|
.failure = undefined, // to be populated by the worker
|
||||||
};
|
};
|
||||||
wait_group.start();
|
wait_group.start();
|
||||||
try thread_pool.spawn(workerHashFile, .{ pkg_dir.dir, hashed_file, &wait_group });
|
try thread_pool.spawn(workerHashFile, .{ pkg_dir, hashed_file, &wait_group });
|
||||||
|
|
||||||
try all_files.append(hashed_file);
|
try all_files.append(hashed_file);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,11 +4,9 @@ pub fn main() !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{
|
var gpa = std.heap.GeneralPurposeAllocator(.{
|
||||||
.thread_safe = true,
|
.thread_safe = true,
|
||||||
}){};
|
}){};
|
||||||
var allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var server = std.http.Server.init(allocator, .{
|
var server = std.http.Server.init(.{ .reuse_address = true });
|
||||||
.reuse_address = true,
|
|
||||||
});
|
|
||||||
defer server.deinit();
|
defer server.deinit();
|
||||||
|
|
||||||
const address = try std.net.Address.parseIp("127.0.0.1", 3000);
|
const address = try std.net.Address.parseIp("127.0.0.1", 3000);
|
||||||
|
@ -30,7 +28,7 @@ pub fn main() !void {
|
||||||
res.transfer_encoding = .{ .content_length = server_body.len };
|
res.transfer_encoding = .{ .content_length = server_body.len };
|
||||||
try res.headers.append("content-type", "text/plain");
|
try res.headers.append("content-type", "text/plain");
|
||||||
try res.headers.append("connection", "close");
|
try res.headers.append("connection", "close");
|
||||||
try res.do();
|
try res.send(); // do() -> send()?
|
||||||
|
|
||||||
var buf: [128]u8 = undefined;
|
var buf: [128]u8 = undefined;
|
||||||
_ = try res.readAll(&buf);
|
_ = try res.readAll(&buf);
|
||||||
|
|
Loading…
Add table
Reference in a new issue