Skip to content

Commit 3d7db35

Browse files
authored
Add Zig Zinc framework (#9289)
* Adding Zinc framework * Update num_threads and fixed headers * Update dependency * Update build command * Update Zinc documentation * Run workflow * Update Zinc approach * Update zinc Allocator and plaintext test * Update zinc dependencies * bug fix * Fixed zinc dependencies and port
1 parent 42acadf commit 3d7db35

File tree

8 files changed

+232
-0
lines changed

8 files changed

+232
-0
lines changed

frameworks/Zig/zinc/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
zig-cache/**/*',
2+
zig-out: 'zig-out/**/*',

frameworks/Zig/zinc/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [Zinc](https://zinc.zon.dev) web framework
2+
3+
## Description
4+
5+
Zinc is a web framework written in pure Zig with a focus on high performance, usability, security, and extensibility.
6+
7+
* [Documentation](https://zinc.zon.dev/)
8+
9+
### Some features are:
10+
- **Fast**
11+
- **Custom allocator**
12+
- **Multithreading**
13+
- **Middleware**
14+
- **Routes grouping**
15+
- **Rendering built-in**
16+
- **Extensible**
17+
- **Suite of unit tests**
18+
- **Usability**
19+
20+
## Important Libraries
21+
The tests were run with:
22+
* [Software](https://zinc.zon.dev/)
23+
* [Example](https://github.com/zon-dev/zinc-examples)
24+
25+
## Test URLs
26+
### JSON
27+
28+
http://localhost:8080/json
29+
30+
### PLAINTEXT
31+
32+
http://localhost:8080/plaintext
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"framework": "zinc",
3+
"tests": [
4+
{
5+
"default": {
6+
"json_url": "/json",
7+
"plaintext_url": "/plaintext",
8+
"port": 3000,
9+
"approach": "Realistic",
10+
"classification": "Fullstack",
11+
"database": "None",
12+
"framework": "Zinc",
13+
"language": "Zig",
14+
"flavor": "None",
15+
"orm": "None",
16+
"platform": "None",
17+
"webserver": "None",
18+
"os": "Linux",
19+
"database_os": "Linux",
20+
"display_name": "Zinc",
21+
"notes": "",
22+
"versus": "None"
23+
}
24+
}
25+
]
26+
}

frameworks/Zig/zinc/build.zig

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const std = @import("std");
2+
3+
// Although this function looks imperative, note that its job is to
4+
// declaratively construct a build graph that will be executed by an external
5+
// runner.
6+
pub fn build(b: *std.Build) void {
7+
// Standard target options allows the person running `zig build` to choose
8+
// what target to build for. Here we do not override the defaults, which
9+
// means any target is allowed, and the default is native. Other options
10+
// for restricting supported target set are available.
11+
const target = b.standardTargetOptions(.{});
12+
13+
// Standard optimization options allow the person running `zig build` to select
14+
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
15+
// set a preferred release mode, allowing the user to decide how to optimize.
16+
const optimize = b.standardOptimizeOption(.{});
17+
18+
const exe = b.addExecutable(.{
19+
.name = "zinc",
20+
.root_source_file = b.path("src/main.zig"),
21+
.target = target,
22+
.optimize = optimize,
23+
});
24+
25+
const zinc = b.dependency("zinc", .{
26+
.target = target,
27+
.optimize = optimize,
28+
});
29+
exe.root_module.addImport("zinc", zinc.module("zinc"));
30+
31+
const datetime = b.dependency("zig-datetime", .{
32+
.target = target,
33+
.optimize = optimize,
34+
});
35+
exe.root_module.addImport("datetime", datetime.module("zig-datetime"));
36+
37+
// This declares intent for the executable to be installed into the
38+
// standard location when the user invokes the "install" step (the default
39+
// step when running `zig build`).
40+
b.installArtifact(exe);
41+
42+
// This *creates* a Run step in the build graph, to be executed when another
43+
// step is evaluated that depends on it. The next line below will establish
44+
// such a dependency.
45+
const run_cmd = b.addRunArtifact(exe);
46+
47+
// By making the run step depend on the install step, it will be run from the
48+
// installation directory rather than directly from within the cache directory.
49+
// This is not necessary, however, if the application depends on other installed
50+
// files, this ensures they will be present and in the expected location.
51+
run_cmd.step.dependOn(b.getInstallStep());
52+
53+
// This allows the user to pass arguments to the application in the build
54+
// command itself, like this: `zig build run -- arg1 arg2 etc`
55+
if (b.args) |args| {
56+
run_cmd.addArgs(args);
57+
}
58+
59+
// This creates a build step. It will be visible in the `zig build --help` menu,
60+
// and can be selected like this: `zig build run`
61+
// This will evaluate the `run` step rather than the default, which is "install".
62+
const run_step = b.step("run", "Run the app");
63+
run_step.dependOn(&run_cmd.step);
64+
65+
const exe_unit_tests = b.addTest(.{
66+
.root_source_file = b.path("src/main.zig"),
67+
.target = target,
68+
.optimize = optimize,
69+
});
70+
71+
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
72+
73+
// Similar to creating the run step earlier, this exposes a `test` step to
74+
// the `zig build --help` menu, providing a way for the user to request
75+
// running the unit tests.
76+
const test_step = b.step("test", "Run unit tests");
77+
test_step.dependOn(&run_exe_unit_tests.step);
78+
}

frameworks/Zig/zinc/build.zig.zon

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.{
2+
.name = "zinc",
3+
.version = "0.1.0",
4+
.dependencies = .{
5+
.zinc = .{
6+
.url = "https://github.com/zon-dev/zinc/archive/refs/tags/0.1.0-beta.5.tar.gz",
7+
.hash = "12201444aa36b4a83f262f319e7c17ccdcff9fbde2efbeb5fc94f1a07eda0d99428e",
8+
},
9+
.@"zig-datetime" = .{
10+
.url = "git+https://github.com/frmdstryr/zig-datetime#70aebf28fb3e137cd84123a9349d157a74708721",
11+
.hash = "122077215ce36e125a490e59ec1748ffd4f6ba00d4d14f7308978e5360711d72d77f",
12+
},
13+
.pg = .{
14+
.url = "git+https://github.com/karlseguin/pg.zig#21db2306aff657802f9cb10a1e7f8fe9c33e7990",
15+
.hash = "1220df8995ceea78a4a37a505fc779ded75725d0606c33fded26103953524dde1619",
16+
},
17+
.mustache = .{
18+
.url = "git+https://github.com/batiati/mustache-zig#ac358646ab9e6123285b90c947ecd40f7966d531",
19+
.hash = "1220cd6e1b49bdd0a568682957dab9a6864554755908f7de990ec7c050f58cf41da2",
20+
},
21+
},
22+
.paths = .{
23+
"build.zig",
24+
"build.zig.zon",
25+
"src",
26+
},
27+
}

frameworks/Zig/zinc/run.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
echo "Waiting for Zinc framework to start..."
2+
3+
zinc

frameworks/Zig/zinc/src/main.zig

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const std = @import("std");
2+
const zinc = @import("zinc");
3+
const Datetime = @import("datetime").datetime.Datetime;
4+
5+
pub fn main() !void {
6+
var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true }){};
7+
defer _ = gpa.deinit();
8+
const allocator = gpa.allocator();
9+
10+
var z = try zinc.init(.{
11+
.port = 3000,
12+
.allocator = allocator,
13+
.num_threads = 16 * @as(u8, @intCast(std.Thread.getCpuCount() catch 1)),
14+
});
15+
defer z.deinit();
16+
17+
var router = z.getRouter();
18+
try router.use(&.{setupHeader});
19+
try router.get("/json", json);
20+
try router.get("/plaintext", plaintext);
21+
22+
try z.run();
23+
}
24+
25+
fn plaintext(ctx: *zinc.Context) anyerror!void {
26+
try ctx.setHeader("Content-Type", "text/plain; charset=utf-8");
27+
try ctx.setBody("Hello, world!");
28+
}
29+
30+
fn json(ctx: *zinc.Context) anyerror!void {
31+
try ctx.json(.{ .message = "Hello, World!" }, .{});
32+
}
33+
34+
fn setupHeader(ctx: *zinc.Context) anyerror!void {
35+
try ctx.setHeader("Server", "Zinc");
36+
37+
const now = Datetime.now();
38+
const now_str = try now.formatHttp(ctx.allocator);
39+
// defer ctx.allocator.free(now_str);
40+
41+
// The time is now: Fri, 20 Dec 2019 22:03:02 UTC
42+
try ctx.setHeader("date", now_str);
43+
}

frameworks/Zig/zinc/zinc.dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM fedora:40 AS build
2+
3+
WORKDIR /zinc
4+
5+
COPY src src
6+
COPY run.sh run.sh
7+
8+
COPY build.zig.zon build.zig.zon
9+
COPY build.zig build.zig
10+
11+
RUN dnf install -y zig
12+
RUN zig version
13+
RUN zig build -Doptimize=ReleaseFast
14+
RUN cp /zinc/zig-out/bin/zinc /usr/local/bin
15+
16+
EXPOSE 3000
17+
ARG BENCHMARK_ENV
18+
ARG TFB_TEST_DATABASE
19+
ARG TFB_TEST_NAME
20+
21+
CMD ["sh", "run.sh"]

0 commit comments

Comments
 (0)