baseline: mongo-light working tree before concurrency refactor
This commit is contained in:
58
src/main.zig
Normal file
58
src/main.zig
Normal file
@@ -0,0 +1,58 @@
|
||||
const std = @import("std");
|
||||
const mongo = @import("mongo");
|
||||
|
||||
const usage =
|
||||
\\mongo-light — lightweight MongoDB-compatible document database
|
||||
\\
|
||||
\\usage: mongo-light [options]
|
||||
\\ --port <n> listen port (default 27017)
|
||||
\\ --bind <ip> bind address (default 127.0.0.1)
|
||||
\\ --db <path> database file (default mongo-light.log)
|
||||
\\ --help show this help
|
||||
\\
|
||||
;
|
||||
|
||||
pub fn main(init: std.process.Init) !void {
|
||||
var port: u16 = 27017;
|
||||
var bind_ip: []const u8 = "127.0.0.1";
|
||||
var db_path: []const u8 = "mongo-light.log";
|
||||
|
||||
var it = std.process.Args.Iterator.init(init.minimal.args);
|
||||
defer it.deinit();
|
||||
_ = it.next(); // program name
|
||||
while (it.next()) |arg| {
|
||||
if (std.mem.eql(u8, arg, "--port")) {
|
||||
const v = it.next() orelse return error.MissingValue;
|
||||
port = std.fmt.parseInt(u16, v, 10) catch {
|
||||
std.debug.print("mongo-light: invalid port '{s}'\n", .{v});
|
||||
return error.InvalidPort;
|
||||
};
|
||||
} else if (std.mem.eql(u8, arg, "--bind")) {
|
||||
bind_ip = it.next() orelse return error.MissingValue;
|
||||
} else if (std.mem.eql(u8, arg, "--db")) {
|
||||
db_path = it.next() orelse return error.MissingValue;
|
||||
} else if (std.mem.eql(u8, arg, "--help") or std.mem.eql(u8, arg, "-h")) {
|
||||
try std.Io.File.writeStreamingAll(.stdout(), init.io, usage);
|
||||
return;
|
||||
} else {
|
||||
std.debug.print("mongo-light: unknown option '{s}'\n{s}", .{ arg, usage });
|
||||
return error.UnknownOption;
|
||||
}
|
||||
}
|
||||
|
||||
const oid_gen = mongo.bson.ObjectIdGen.init(init.io);
|
||||
var engine = try mongo.db.Engine.open(init.gpa, init.io, db_path);
|
||||
defer engine.deinit();
|
||||
std.debug.print("mongo-light: opened database '{s}'\n", .{db_path});
|
||||
|
||||
var server = mongo.server.Server{
|
||||
.gpa = init.gpa,
|
||||
.port = port,
|
||||
.bind_ip = bind_ip,
|
||||
.oid_gen = oid_gen,
|
||||
.connection_counter = .init(1),
|
||||
.engine = &engine,
|
||||
.start_time = std.Io.Timestamp.now(init.io, .real),
|
||||
};
|
||||
try server.run();
|
||||
}
|
||||
Reference in New Issue
Block a user