Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/p1atdev/zig-simple-http-server
https://github.com/p1atdev/zig-simple-http-server
Last synced: 6 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/p1atdev/zig-simple-http-server
- Owner: p1atdev
- Created: 2022-08-12T22:53:42.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-08-12T22:58:21.000Z (over 2 years ago)
- Last Synced: 2024-11-22T00:48:22.350Z (about 2 months ago)
- Language: Zig
- Size: 232 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Zig simple http server example
## Code
```zig
const print = std.debug.print;
const std = @import("std");
const net = std.net;pub fn main() anyerror!void {
var server = net.StreamServer.init(.{});
defer server.deinit();try server.listen(net.Address.parseIp("0.0.0.0", 3000) catch unreachable);
while (true) {
const conn: net.StreamServer.Connection = try server.accept();
var buff: [1024]u8 = undefined;const r = conn.stream.reader();
_ = try r.read(buff[0..]);
print("Request: {s}\n\n", .{buff});var w = conn.stream.writer();
const msg = "HTTP/1.1 200 OK\r\n\r\nHello, World\n";
try w.writeAll(msg);
print("Response: {s}\n\n", .{msg});conn.stream.close();
}
}
```## Reference
https://github.com/ziglang/zig/blob/master/lib/std/x/net/tcp.zig
https://doc.rust-lang.org/book/ch20-01-single-threaded.html