initial push - init server
This commit is contained in:
parent
9ebffc07a2
commit
34ff28b628
2 changed files with 62 additions and 0 deletions
17
build.zig
Normal file
17
build.zig
Normal file
|
@ -0,0 +1,17 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.build.Builder) void {
|
||||
// Standard release options allow the person running `zig build` to select
|
||||
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
|
||||
const mode = b.standardReleaseOptions();
|
||||
|
||||
const lib = b.addStaticLibrary("zeam", "src/main.zig");
|
||||
lib.setBuildMode(mode);
|
||||
lib.install();
|
||||
|
||||
const main_tests = b.addTest("src/main.zig");
|
||||
main_tests.setBuildMode(mode);
|
||||
|
||||
const test_step = b.step("test", "Run library tests");
|
||||
test_step.dependOn(&main_tests.step);
|
||||
}
|
45
src/main.zig
Normal file
45
src/main.zig
Normal file
|
@ -0,0 +1,45 @@
|
|||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
|
||||
const allocator = std.heap.page_allocator;
|
||||
|
||||
pub fn main() !void {
|
||||
|
||||
// Init server
|
||||
const server_options: std.net.StreamServer.Options = .{};
|
||||
var server = std.net.StreamServer.init(server_options);
|
||||
defer server.deinit();
|
||||
const addr = try std.net.Address.parseIp("0.0.0.0", 8080);
|
||||
|
||||
while (true) {
|
||||
server.listen(addr) catch {
|
||||
server.close();
|
||||
continue;
|
||||
};
|
||||
break;
|
||||
}
|
||||
|
||||
// Handling connections
|
||||
while (true) {
|
||||
const conn = if (server.accept()) |conn| conn else |_| continue;
|
||||
defer conn.stream.close();
|
||||
|
||||
var buffer = std.ArrayList(u8).init(allocator);
|
||||
defer buffer.deinit();
|
||||
|
||||
var chunk_buf: [4096]u8 = undefined;
|
||||
// Collect max 4096 byte of data from the stream into the chunk_buf. Then add it
|
||||
// to the ArrayList. Repeat this until request stream ends by counting the appearence
|
||||
// of "\r\n"
|
||||
while (true) {
|
||||
_ = try conn.stream.read(chunk_buf[0..]);
|
||||
try buffer.appendSlice(chunk_buf[0..]);
|
||||
if (std.mem.containsAtLeast(u8, buffer.items, 2, "\r\n")) break;
|
||||
}
|
||||
std.debug.print("Data sent by the client:\n{s}\n", .{buffer.items});
|
||||
// Creating Response
|
||||
_ = try conn.stream.write("HTTP/1.1 200 OK\r\n");
|
||||
_ = try conn.stream.write("Content-Type: text/html\r\n\r\n");
|
||||
_ = try conn.stream.write("<h1>It works!</h1>");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue