From 368675acd7e055e6f4e6a3cc0ecb63ea77f15af1 Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 9 Apr 2021 18:02:31 -0300 Subject: [PATCH 1/4] attach zigmod deps to build.zig --- .gitignore | 1 + build.zig | 2 ++ 2 files changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 3cef7be..b6abbfd 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ zig-cache/ +deps.zig diff --git a/build.zig b/build.zig index 06bf4cc..dafaf08 100644 --- a/build.zig +++ b/build.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const deps = @import("./deps.zig"); pub fn build(b: *std.build.Builder) void { // Standard target options allows the person running `zig build` to choose @@ -14,6 +15,7 @@ pub fn build(b: *std.build.Builder) void { const exe = b.addExecutable("yet-another-pomf-clone", "src/main.zig"); exe.setTarget(target); exe.setBuildMode(mode); + deps.addAllTo(exe); exe.install(); const run_cmd = exe.run(); From 866fee15020835f70b56f2e8ac98deba29d6893a Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 9 Apr 2021 18:11:44 -0300 Subject: [PATCH 2/4] add apple_pie hello world --- .gitignore | 1 + src/main.zig | 14 ++++++++++++++ zig.mod | 4 ++++ 3 files changed, 19 insertions(+) diff --git a/.gitignore b/.gitignore index b6abbfd..5c97abb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ zig-cache/ deps.zig +.zigmod/ diff --git a/src/main.zig b/src/main.zig index d29869f..c7e7679 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,5 +1,19 @@ const std = @import("std"); +const http = @import("apple_pie"); pub fn main() anyerror!void { std.log.info("All your codebase are belong to us.", .{}); + + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + + try http.listenAndServe( + &gpa.allocator, + try std.net.Address.parseIp("127.0.0.1", 8080), + index, + ); +} + +fn index(response: *http.Response, request: http.Request) !void { + try response.writer().writeAll("Hello Zig!"); } diff --git a/zig.mod b/zig.mod index 6627f2f..50c31de 100644 --- a/zig.mod +++ b/zig.mod @@ -2,3 +2,7 @@ id: n5imv64i14btgrqm6qbzyhqs2xc2e7w3hxk1l9b9s5qmtfz0 name: yet-another-pomf-clone main: src/main.zig dependencies: + + - src: git https://github.com/Luukdegram/apple_pie + name: apple_pie + main: src/apple_pie.zig From 48da2945d81ea287b2df733149d61e1cd19b65eb Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 9 Apr 2021 18:30:32 -0300 Subject: [PATCH 3/4] add image uploads --- .gitignore | 1 + src/main.zig | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 5c97abb..91f9966 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ zig-cache/ deps.zig +images/ .zigmod/ diff --git a/src/main.zig b/src/main.zig index c7e7679..a75356f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,19 +1,72 @@ const std = @import("std"); const http = @import("apple_pie"); +const images_dir_path = "./images"; + pub fn main() anyerror!void { std.log.info("All your codebase are belong to us.", .{}); var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); + const bind_addr = try std.net.Address.parseIp("0.0.0.0", 8080); + std.log.info("serving on {}", .{bind_addr}); + + // TODO: configurable path via env var + try std.fs.cwd().makePath(images_dir_path); + try http.listenAndServe( &gpa.allocator, - try std.net.Address.parseIp("127.0.0.1", 8080), - index, + bind_addr, + comptime http.router.router(&[_]http.router.Route{ + http.router.get("/", index), + http.router.post("/api/upload", uploadFile), + http.router.post("/:filename", fetchFile), + }), ); } fn index(response: *http.Response, request: http.Request) !void { try response.writer().writeAll("Hello Zig!"); } + +fn generateImageId(buffer: []u8) []const u8 { + var i: usize = 0; + + const seed = @truncate(u64, @bitCast(u128, std.time.nanoTimestamp())); + var r = std.rand.DefaultPrng.init(seed); + + while (i < 16) : (i += 1) { + // random ascii lowercase char + var idx = @intCast(u8, r.random.uintLessThan(u5, 24)); + var letter = @as(u8, 97) + idx; + buffer[i] = letter; + } + + return buffer[0..i]; +} + +fn uploadFile(response: *http.Response, request: http.Request) !void { + std.log.info("upload! got {d} bytes", .{request.body.len}); + + var image_id_buffer: [256]u8 = undefined; + const image_id = generateImageId(&image_id_buffer); + + var image_path_buffer: [512]u8 = undefined; + const image_path = try std.fmt.bufPrint( + &image_path_buffer, + "{s}/{s}.jpg", + .{ images_dir_path, image_id }, + ); + + const image_file = try std.fs.cwd().createFile(image_path, .{}); + try image_file.writer().writeAll(request.body); + + try response.writer().writeAll(image_path); +} + +fn fetchFile(response: *http.Response, request: http.Request, filename: []const u8) !void { + std.log.info("got name: {s}", .{filename}); + const images_dir = try std.fs.cwd().openDir(images_dir_path, .{}); + try response.writer().writeAll("Hello Zig! fetch"); +} From 43abed7e8053be9275285ba460283e4e012175de Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 9 Apr 2021 18:33:21 -0300 Subject: [PATCH 4/4] add impl for file fetch --- src/main.zig | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main.zig b/src/main.zig index a75356f..6d313d2 100644 --- a/src/main.zig +++ b/src/main.zig @@ -21,7 +21,7 @@ pub fn main() anyerror!void { comptime http.router.router(&[_]http.router.Route{ http.router.get("/", index), http.router.post("/api/upload", uploadFile), - http.router.post("/:filename", fetchFile), + http.router.get("/i/:filename", fetchFile), }), ); } @@ -67,6 +67,21 @@ fn uploadFile(response: *http.Response, request: http.Request) !void { fn fetchFile(response: *http.Response, request: http.Request, filename: []const u8) !void { std.log.info("got name: {s}", .{filename}); + var image_path_buffer: [512]u8 = undefined; + const images_dir = try std.fs.cwd().openDir(images_dir_path, .{}); - try response.writer().writeAll("Hello Zig! fetch"); + const image_path = try std.fmt.bufPrint( + &image_path_buffer, + "{s}/{s}", + .{ images_dir_path, filename }, + ); + + // TODO return 404 on error + const image_file = try std.fs.cwd().openFile(image_path, .{ .read = false }); + while (true) { + var file_write_buffer: [1024]u8 = undefined; + const bytes_read = try image_file.read(&file_write_buffer); + if (bytes_read == 0) return; + try response.writer().writeAll(&file_write_buffer); + } }