API Refactoring

This commit is contained in:
jaina heartles 2022-07-09 15:43:35 -07:00
parent ff6d8f8e03
commit 013bc015c5
4 changed files with 29 additions and 40 deletions

View file

@ -1,19 +1,19 @@
const std = @import("std");
const ciutf8 = @import("util").ciutf8;
const conn = @import("./conn.zig");
const response_stream = @import("./response_stream.zig");
const routing = @import("./routing.zig");
const request = @import("./request.zig");
const server = @import("./server.zig");
pub const Connection = conn.Connection;
pub const server = @import("./server.zig");
pub const Method = std.http.Method;
pub const ResponseStream = response_stream.ResponseStream;
pub const Request = request.Request;
//pub const Router = routing.Router(Context);
pub const Status = std.http.Status;
pub const Request = request.Request;
pub const Server = server.Server;
//pub const Router = routing.Router(Context);
pub const Headers = std.HashMap([]const u8, []const u8, struct {
pub fn eql(_: @This(), a: []const u8, b: []const u8) bool {
return ciutf8.eql(a, b);
@ -24,21 +24,7 @@ pub const Headers = std.HashMap([]const u8, []const u8, struct {
}
}, std.hash_map.default_max_load_percentage);
pub const Context = struct {
arena_alloc: std.mem.Allocator,
request: *const Request,
connection: *const Connection,
const Writer = ResponseStream(Connection.Writer);
pub fn openResponse(self: *Context, headers: *const Headers, status: Status) !Writer {
return try response_stream.open(self.arena_alloc, self.connection.stream.writer(), headers, status);
}
};
test {
_ = conn;
_ = response_stream;
_ = routing;
_ = server;
_ = request;

View file

@ -2,9 +2,14 @@ const std = @import("std");
const util = @import("util");
const http = @import("./lib.zig");
const Request = http.Request;
const Connection = http.Connection;
const connection = @import("./server/connection.zig");
const response = @import("./server/response.zig");
pub const Connection = connection.Connection;
pub const Response = response.ResponseStream(Connection.Writer);
const ConnectionServer = connection.Server;
const Request = http.Request;
const request_buf_size = 1 << 16;
pub const Context = struct {
@ -12,10 +17,8 @@ pub const Context = struct {
request: Request,
connection: Connection,
const Writer = http.ResponseStream(Connection.Writer);
pub fn openResponse(self: *Context, headers: *const http.Headers, status: http.Status) !Writer {
return try http.response_stream.open(self.alloc, self.connection.stream.writer(), headers, status);
pub fn openResponse(self: *Context, headers: *const http.Headers, status: http.Status) !Response {
return try response.open(self.alloc, self.connection.stream.writer(), headers, status);
}
pub fn close(self: *Context) void {
@ -24,15 +27,15 @@ pub const Context = struct {
}
};
const HttpServer = struct {
conn_server: http.ConnectionServer,
pub fn listen(addr: std.net.Address) !HttpServer {
return HttpServer{
.conn_server = try http.ConnectionServer.listen(addr),
pub const Server = struct {
conn_server: ConnectionServer,
pub fn listen(addr: std.net.Address) !Server {
return Server{
.conn_server = try ConnectionServer.listen(addr),
};
}
pub fn accept(self: *HttpServer, alloc: std.mem.Allocator) !Context {
pub fn accept(self: *Server, alloc: std.mem.Allocator) !Context {
while (true) {
const conn = try self.conn_server.accept();
errdefer conn.close();
@ -46,7 +49,7 @@ const HttpServer = struct {
}
}
pub fn shutdown(self: *HttpServer) void {
pub fn shutdown(self: *Server) void {
self.conn_server.shutdown();
}
};

View file

@ -24,12 +24,12 @@ pub const Connection = struct {
}
};
pub const ConnectionServer = struct {
pub const Server = struct {
next_conn_id: std.atomic.Atomic(Connection.Id) = std.atomic.Atomic(Connection.Id).init(1),
stream_server: std.net.StreamServer,
pub fn listen(addr: std.net.Address) !ConnectionServer {
var self = ConnectionServer{
pub fn listen(addr: std.net.Address) !Server {
var self = Server{
.stream_server = std.net.StreamServer.init(.{ .reuse_address = true }),
};
errdefer self.stream_server.deinit();
@ -38,14 +38,14 @@ pub const ConnectionServer = struct {
return self;
}
pub fn accept(self: *ConnectionServer) !Connection {
pub fn accept(self: *Server) !Connection {
const conn = try self.stream_server.accept();
const id = self.next_conn_id.fetchAdd(1, .SeqCst);
return Connection.new(id, conn);
}
pub fn shutdown(self: *ConnectionServer) void {
pub fn shutdown(self: *Server) void {
self.stream_server.deinit();
}
};

View file

@ -1,5 +1,5 @@
const std = @import("std");
const http = @import("./lib.zig");
const http = @import("../lib.zig");
const Status = http.Status;
const Headers = http.Headers;