requires at least zig 0.11.0
This commit is contained in:
parent
32c1f95938
commit
9ee1561eae
4 changed files with 10 additions and 15 deletions
|
@ -1,7 +1,7 @@
|
||||||
# zerve
|
# zerve
|
||||||
A simple framework for writing web services in zig.
|
A simple framework for writing web services in zig.
|
||||||
|
|
||||||
## Relies on zig 0.10.1 !
|
## Requires at least zig 0.11.0!
|
||||||
|
|
||||||
|
|
||||||
## Create a simple web app
|
## Create a simple web app
|
||||||
|
|
|
@ -18,16 +18,11 @@ pub const Server = struct {
|
||||||
// Init server
|
// Init server
|
||||||
const server_options: std.net.StreamServer.Options = .{};
|
const server_options: std.net.StreamServer.Options = .{};
|
||||||
var server = std.net.StreamServer.init(server_options);
|
var server = std.net.StreamServer.init(server_options);
|
||||||
|
defer server.close();
|
||||||
defer server.deinit();
|
defer server.deinit();
|
||||||
const addr = try std.net.Address.parseIp(ip, port);
|
const addr = try std.net.Address.parseIp(ip, port);
|
||||||
|
|
||||||
while (true) {
|
try server.listen(addr);
|
||||||
server.listen(addr) catch {
|
|
||||||
server.close();
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handling connections
|
// Handling connections
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -46,7 +41,7 @@ pub const Server = struct {
|
||||||
if (std.mem.containsAtLeast(u8, buffer.items, 2, "\r\n")) break;
|
if (std.mem.containsAtLeast(u8, buffer.items, 2, "\r\n")) break;
|
||||||
}
|
}
|
||||||
// Build the Request
|
// Build the Request
|
||||||
const req_stream = buffer.toOwnedSlice();
|
const req_stream = try buffer.toOwnedSlice();
|
||||||
var req = try buildRequest(req_stream, allocator);
|
var req = try buildRequest(req_stream, allocator);
|
||||||
|
|
||||||
// if there ist a path set in the uri trim the trailing slash in order to accept it later during the matching check.
|
// if there ist a path set in the uri trim the trailing slash in order to accept it later during the matching check.
|
||||||
|
@ -101,7 +96,7 @@ fn buildRequest(bytes: []const u8, allocator: std.mem.Allocator) !Request {
|
||||||
const header_pair = Header{ .key = item1, .value = item2 };
|
const header_pair = Header{ .key = item1, .value = item2 };
|
||||||
try header_buffer.append(header_pair);
|
try header_buffer.append(header_pair);
|
||||||
}
|
}
|
||||||
req.headers = header_buffer.toOwnedSlice();
|
req.headers = try header_buffer.toOwnedSlice();
|
||||||
req.body = if (parts.next()) |value| value else "";
|
req.body = if (parts.next()) |value| value else "";
|
||||||
return req;
|
return req;
|
||||||
}
|
}
|
||||||
|
@ -129,13 +124,13 @@ fn stringifyResponse(r: Response, allocator: std.mem.Allocator) ![]const u8 {
|
||||||
try res.appendSlice("\r\n");
|
try res.appendSlice("\r\n");
|
||||||
|
|
||||||
for (r.headers) |header| {
|
for (r.headers) |header| {
|
||||||
try res.appendSlice(header.stringify());
|
try res.appendSlice(try header.stringify());
|
||||||
try res.appendSlice("\n");
|
try res.appendSlice("\n");
|
||||||
}
|
}
|
||||||
try res.appendSlice("\r\n\r\n");
|
try res.appendSlice("\r\n\r\n");
|
||||||
try res.appendSlice(r.body);
|
try res.appendSlice(r.body);
|
||||||
|
|
||||||
return res.toOwnedSlice();
|
return try res.toOwnedSlice();
|
||||||
}
|
}
|
||||||
|
|
||||||
test "stringify Response" {
|
test "stringify Response" {
|
||||||
|
|
|
@ -17,12 +17,12 @@ pub const Header = struct {
|
||||||
value: []const u8,
|
value: []const u8,
|
||||||
|
|
||||||
/// Turns the header key and value into a string.
|
/// Turns the header key and value into a string.
|
||||||
pub fn stringify(header: Header) []const u8 {
|
pub fn stringify(header: Header) ![]const u8 {
|
||||||
var string = std.ArrayList(u8).init(allocator);
|
var string = std.ArrayList(u8).init(allocator);
|
||||||
string.appendSlice(header.key) catch unreachable;
|
string.appendSlice(header.key) catch unreachable;
|
||||||
string.appendSlice(": ") catch unreachable;
|
string.appendSlice(": ") catch unreachable;
|
||||||
string.appendSlice(header.value) catch unreachable;
|
string.appendSlice(header.value) catch unreachable;
|
||||||
const out = string.toOwnedSlice();
|
const out = try string.toOwnedSlice();
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
zig.mod
2
zig.mod
|
@ -2,5 +2,5 @@ name: zerve
|
||||||
main: src/zerve.zig
|
main: src/zerve.zig
|
||||||
license: MIT
|
license: MIT
|
||||||
description: Simple web framework for zig
|
description: Simple web framework for zig
|
||||||
min_zig_version: 0.10.1
|
min_zig_version: 0.11.0
|
||||||
dependencies:
|
dependencies:
|
||||||
|
|
Loading…
Reference in a new issue