API Refactoring
This commit is contained in:
parent
ff6d8f8e03
commit
013bc015c5
4 changed files with 29 additions and 40 deletions
|
@ -1,19 +1,19 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const ciutf8 = @import("util").ciutf8;
|
const ciutf8 = @import("util").ciutf8;
|
||||||
|
|
||||||
const conn = @import("./conn.zig");
|
|
||||||
const response_stream = @import("./response_stream.zig");
|
|
||||||
const routing = @import("./routing.zig");
|
const routing = @import("./routing.zig");
|
||||||
const request = @import("./request.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 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 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 const Headers = std.HashMap([]const u8, []const u8, struct {
|
||||||
pub fn eql(_: @This(), a: []const u8, b: []const u8) bool {
|
pub fn eql(_: @This(), a: []const u8, b: []const u8) bool {
|
||||||
return ciutf8.eql(a, b);
|
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);
|
}, 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 {
|
test {
|
||||||
_ = conn;
|
|
||||||
_ = response_stream;
|
|
||||||
_ = routing;
|
_ = routing;
|
||||||
_ = server;
|
_ = server;
|
||||||
_ = request;
|
_ = request;
|
||||||
|
|
|
@ -2,9 +2,14 @@ const std = @import("std");
|
||||||
const util = @import("util");
|
const util = @import("util");
|
||||||
const http = @import("./lib.zig");
|
const http = @import("./lib.zig");
|
||||||
|
|
||||||
const Request = http.Request;
|
const connection = @import("./server/connection.zig");
|
||||||
const Connection = http.Connection;
|
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;
|
const request_buf_size = 1 << 16;
|
||||||
|
|
||||||
pub const Context = struct {
|
pub const Context = struct {
|
||||||
|
@ -12,10 +17,8 @@ pub const Context = struct {
|
||||||
request: Request,
|
request: Request,
|
||||||
connection: Connection,
|
connection: Connection,
|
||||||
|
|
||||||
const Writer = http.ResponseStream(Connection.Writer);
|
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 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 close(self: *Context) void {
|
pub fn close(self: *Context) void {
|
||||||
|
@ -24,15 +27,15 @@ pub const Context = struct {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const HttpServer = struct {
|
pub const Server = struct {
|
||||||
conn_server: http.ConnectionServer,
|
conn_server: ConnectionServer,
|
||||||
pub fn listen(addr: std.net.Address) !HttpServer {
|
pub fn listen(addr: std.net.Address) !Server {
|
||||||
return HttpServer{
|
return Server{
|
||||||
.conn_server = try http.ConnectionServer.listen(addr),
|
.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) {
|
while (true) {
|
||||||
const conn = try self.conn_server.accept();
|
const conn = try self.conn_server.accept();
|
||||||
errdefer conn.close();
|
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();
|
self.conn_server.shutdown();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -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),
|
next_conn_id: std.atomic.Atomic(Connection.Id) = std.atomic.Atomic(Connection.Id).init(1),
|
||||||
stream_server: std.net.StreamServer,
|
stream_server: std.net.StreamServer,
|
||||||
|
|
||||||
pub fn listen(addr: std.net.Address) !ConnectionServer {
|
pub fn listen(addr: std.net.Address) !Server {
|
||||||
var self = ConnectionServer{
|
var self = Server{
|
||||||
.stream_server = std.net.StreamServer.init(.{ .reuse_address = true }),
|
.stream_server = std.net.StreamServer.init(.{ .reuse_address = true }),
|
||||||
};
|
};
|
||||||
errdefer self.stream_server.deinit();
|
errdefer self.stream_server.deinit();
|
||||||
|
@ -38,14 +38,14 @@ pub const ConnectionServer = struct {
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn accept(self: *ConnectionServer) !Connection {
|
pub fn accept(self: *Server) !Connection {
|
||||||
const conn = try self.stream_server.accept();
|
const conn = try self.stream_server.accept();
|
||||||
const id = self.next_conn_id.fetchAdd(1, .SeqCst);
|
const id = self.next_conn_id.fetchAdd(1, .SeqCst);
|
||||||
|
|
||||||
return Connection.new(id, conn);
|
return Connection.new(id, conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn shutdown(self: *ConnectionServer) void {
|
pub fn shutdown(self: *Server) void {
|
||||||
self.stream_server.deinit();
|
self.stream_server.deinit();
|
||||||
}
|
}
|
||||||
};
|
};
|
|
@ -1,5 +1,5 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const http = @import("./lib.zig");
|
const http = @import("../lib.zig");
|
||||||
|
|
||||||
const Status = http.Status;
|
const Status = http.Status;
|
||||||
const Headers = http.Headers;
|
const Headers = http.Headers;
|
Loading…
Reference in a new issue