Skip to content

Commit 3ced82b

Browse files
committed
Add solutions for Zig
1 parent 69aae29 commit 3ced82b

File tree

10 files changed

+241
-0
lines changed

10 files changed

+241
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Zig build artifacts
2+
.zig-cache/
3+
zig-cache/
4+
zig-out/
5+
6+
# Compiled binary output
7+
main
8+
bin/
9+
!bin/.gitkeep
10+
11+
# Ignore any .o or .h files generated during build
12+
*.o
13+
*.h
14+
15+
# Ignore OS and editor specific files
16+
.DS_Store
17+
*.swp
18+
*~
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/http-server.png)
2+
3+
This is a starting point for Zig solutions to the
4+
["Build Your Own HTTP server" Challenge](https://app.codecrafters.io/courses/http-server/overview).
5+
6+
[HTTP](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) is the
7+
protocol that powers the web. In this challenge, you'll build a HTTP/1.1 server
8+
that is capable of serving multiple clients.
9+
10+
Along the way you'll learn about TCP servers,
11+
[HTTP request syntax](https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html),
12+
and more.
13+
14+
**Note**: If you're viewing this repo on GitHub, head over to
15+
[codecrafters.io](https://codecrafters.io) to try the challenge.
16+
17+
# Passing the first stage
18+
19+
The entry point for your HTTP server implementation is in `src/main.zig`. Study
20+
and uncomment the relevant code, and push your changes to pass the first stage:
21+
22+
```sh
23+
git add .
24+
git commit -m "pass 1st stage" # any msg
25+
git push origin master
26+
```
27+
28+
Time to move on to the next stage!
29+
30+
# Stage 2 & beyond
31+
32+
Note: This section is for stages 2 and beyond.
33+
34+
1. Ensure you have `zig (0.12)` installed locally
35+
1. Run `./your_server.sh` to run your program, which is implemented in
36+
`src/main.zig`.
37+
1. Commit your changes and run `git push origin master` to submit your solution
38+
to CodeCrafters. Test output will be streamed to your terminal.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const std = @import("std");
2+
3+
// Learn more about this file here: https://ziglang.org/learn/build-system
4+
pub fn build(b: *std.Build) void {
5+
const exe = b.addExecutable(.{
6+
.name = "zig",
7+
.root_source_file = b.path("src/main.zig"),
8+
.target = b.standardTargetOptions(.{}),
9+
.optimize = b.standardOptimizeOption(.{}),
10+
});
11+
12+
// This declares intent for the executable to be installed into the
13+
// standard location when the user invokes the "install" step (the default
14+
// step when running `zig build`).
15+
b.installArtifact(exe);
16+
17+
// This *creates* a Run step in the build graph, to be executed when another
18+
// step is evaluated that depends on it. The next line below will establish
19+
// such a dependency.
20+
const run_cmd = b.addRunArtifact(exe);
21+
22+
// This creates a build step. It will be visible in the `zig build --help` menu,
23+
// and can be selected like this: `zig build run`
24+
// This will evaluate the `run` step rather than the default, which is "install".
25+
const run_step = b.step("run", "Run the app");
26+
run_step.dependOn(&run_cmd.step);
27+
28+
// This allows the user to pass arguments to the application in the build
29+
// command itself, like this: `zig build run -- arg1 arg2 etc`
30+
if (b.args) |args| {
31+
run_cmd.addArgs(args);
32+
}
33+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
.{
2+
.name = "zig",
3+
// This is a [Semantic Version](https://semver.org/).
4+
// In a future version of Zig it will be used for package deduplication.
5+
.version = "0.0.0",
6+
7+
// This field is optional.
8+
// This is currently advisory only; Zig does not yet do anything
9+
// with this value.
10+
//.minimum_zig_version = "0.11.0",
11+
12+
// This field is optional.
13+
// Each dependency must either provide a `url` and `hash`, or a `path`.
14+
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
15+
// Once all dependencies are fetched, `zig build` no longer requires
16+
// internet connectivity.
17+
.dependencies = .{
18+
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
19+
//.example = .{
20+
// // When updating this field to a new URL, be sure to delete the corresponding
21+
// // `hash`, otherwise you are communicating that you expect to find the old hash at
22+
// // the new URL.
23+
// .url = "https://example.com/foo.tar.gz",
24+
//
25+
// // This is computed from the file contents of the directory of files that is
26+
// // obtained after fetching `url` and applying the inclusion rules given by
27+
// // `paths`.
28+
// //
29+
// // This field is the source of truth; packages do not come from a `url`; they
30+
// // come from a `hash`. `url` is just one of many possible mirrors for how to
31+
// // obtain a package matching this `hash`.
32+
// //
33+
// // Uses the [multihash](https://multiformats.io/multihash/) format.
34+
// .hash = "...",
35+
//
36+
// // When this is provided, the package is found in a directory relative to the
37+
// // build root. In this case the package's hash is irrelevant and therefore not
38+
// // computed. This field and `url` are mutually exclusive.
39+
// .path = "foo",
40+
41+
// // When this is set to `true`, a package is declared to be lazily
42+
// // fetched. This makes the dependency only get fetched if it is
43+
// // actually used.
44+
// .lazy = false,
45+
//},
46+
},
47+
48+
// Specifies the set of files and directories that are included in this package.
49+
// Only files and directories listed here are included in the `hash` that
50+
// is computed for this package.
51+
// Paths are relative to the build root. Use the empty string (`""`) to refer to
52+
// the build root itself.
53+
// A directory listed here means that all files within, recursively, are included.
54+
.paths = .{
55+
// This makes *all* files, recursively, included in this package. It is generally
56+
// better to explicitly list the files and directories instead, to insure that
57+
// fetching from tarballs, file system paths, and version control all result
58+
// in the same contents hash.
59+
"",
60+
// For example...
61+
//"build.zig",
62+
//"build.zig.zon",
63+
//"src",
64+
//"LICENSE",
65+
//"README.md",
66+
},
67+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Set this to true if you want debug logs.
2+
#
3+
# These can be VERY verbose, so we suggest turning them off
4+
# unless you really need them.
5+
debug: false
6+
7+
# Use this to change the Zig version used to run your code
8+
# on Codecrafters.
9+
#
10+
# Available versions: zig-0.12
11+
language_pack: zig-0.12
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const std = @import("std");
2+
const net = std.net;
3+
4+
pub fn main() !void {
5+
const stdout = std.io.getStdOut().writer();
6+
7+
const address = try net.Address.resolveIp("127.0.0.1", 4221);
8+
9+
// var listener = try address.listen(.{
10+
// .reuse_address = true,
11+
// });
12+
// defer listener.deinit();
13+
14+
// const connection = try listener.accept();
15+
// try stdout.print("accepted new connection", .{});
16+
// connection.stream.close();
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
#
3+
# DON'T EDIT THIS!
4+
#
5+
# CodeCrafters uses this file to test your code. Don't make any changes here!
6+
#
7+
# DON'T EDIT THIS!
8+
set -e
9+
10+
exec zig build run -- $@
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@@ -1,22 +1,17 @@
2+
const std = @import("std");
3+
-// Uncomment this block to pass the first stage
4+
-// const net = std.net;
5+
+const net = std.net;
6+
7+
pub fn main() !void {
8+
const stdout = std.io.getStdOut().writer();
9+
10+
- // You can use print statements as follows for debugging, they'll be visible when running tests.
11+
- try stdout.print("Logs from your program will appear here!\n", .{});
12+
-
13+
- // Uncomment this block to pass the first stage
14+
- // const address = try net.Address.resolveIp("127.0.0.1", 4221);
15+
+ const address = try net.Address.resolveIp("127.0.0.1", 4221);
16+
17+
// var listener = try address.listen(.{
18+
// .reuse_address = true,
19+
// });
20+
// defer listener.deinit();
21+
22+
// const connection = try listener.accept();
23+
// try stdout.print("accepted new connection", .{});
24+
// connection.stream.close();
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The entry point for your HTTP server implementation is in `src/main.zig`.
2+
3+
Study and uncomment the relevant code:
4+
5+
```zig
6+
// Uncomment this block to pass the first stage
7+
const net = std.net;
8+
```
9+
10+
```zig
11+
// Uncomment this block to pass the first stage
12+
const address = try net.Address.resolveIp("127.0.0.1", 4221);
13+
```
14+
15+
Push your changes to pass the first stage:
16+
17+
```
18+
git add .
19+
git commit -m "pass 1st stage" # any msg
20+
git push origin master
21+
```

0 commit comments

Comments
 (0)