From fb955625bc47d963cf64c73acb4e3e9451e6cfe0 Mon Sep 17 00:00:00 2001 From: Nathan Anderson Date: Thu, 27 Jul 2023 12:30:56 -0600 Subject: [PATCH] Fixes for passing in sqlite file name --- src/db/db.zig | 6 ++++-- src/http_handler.zig | 37 +++++++++++++++++++++++++++++++++---- src/main.zig | 8 +++----- src/routes/auth.zig | 2 +- src/routes/user.zig | 8 +++++++- 5 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/db/db.zig b/src/db/db.zig index 98e58c4..a7b3a71 100644 --- a/src/db/db.zig +++ b/src/db/db.zig @@ -11,9 +11,11 @@ pub const Db = struct { _mode: ?sqlite.Db.Mode, _sql_db: sqlite.Db, - pub fn init(allocator: Allocator, mode: ?sqlite.Db.Mode) !Db { + pub fn init(allocator: Allocator, db_path: ?[]const u8, mode: ?sqlite.Db.Mode) !Db { + const path: [:0]const u8 = if (db_path != null) try std.mem.Allocator.dupeZ(allocator, u8, db_path.?) else "./data.db"; + defer allocator.free(path); var sqlDb = try sqlite.Db.init(.{ - .mode = if (mode != null) mode.? else sqlite.Db.Mode{ .File = "./data.db" }, + .mode = if (mode != null) mode.? else sqlite.Db.Mode{ .File = path }, .open_flags = .{ .write = true, .create = true, diff --git a/src/http_handler.zig b/src/http_handler.zig index 16bb12d..0a76921 100644 --- a/src/http_handler.zig +++ b/src/http_handler.zig @@ -14,18 +14,47 @@ const note = @import("routes/shared_note.zig"); const Db = @import("db/db.zig").Db; -var db: Db = undefined; +var db: ?Db = null; pub fn getDb() *Db { - return &db; + return &db.?; } pub fn startHttpServer() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); - db = try Db.init(allocator, null); - defer db.deinit(); + // db = try Db.init(allocator, null); + // defer db.deinit(); + var args = try std.process.argsWithAllocator(allocator); + + // skip program name + _ = args.skip(); + while (args.next()) |arg| { + if (std.mem.eql(u8, arg, "--db_path")) { + const path = args.next(); + // std.debug.print("Got path: {any}", .{path}); + if (path) |db_path| { + db = try Db.init(allocator, db_path, null); + } else { + std.log.err("Db path not provided after arg", .{}); + return; + } + } + if (std.mem.eql(u8, arg, "--make-migration")) { + if (db == null) { + std.log.err("Cannot migrate, provide db path first", .{}); + return; + } + try db.?.wipeAndMigrateDb(); + } + } + + if (db == null) { + db = try Db.init(allocator, null, null); + } + + defer db.?.deinit(); var server = try httpz.Server().init(allocator, .{ .port = 8081 }); diff --git a/src/main.zig b/src/main.zig index b354c30..7dd94c9 100644 --- a/src/main.zig +++ b/src/main.zig @@ -5,10 +5,8 @@ const http = @import("./http_handler.zig"); pub fn main() !void { std.debug.print("\nStarting Server...\n", .{}); - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; - const allocator = gpa.allocator(); - var db = try Db.init(allocator, null); - defer db.deinit(); - // try db.wipeAndMigrateDb(); + // var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + // const allocator = gpa.allocator(); + // _ = allocator; try http.startHttpServer(); } diff --git a/src/routes/auth.zig b/src/routes/auth.zig index b88c5fd..84785a0 100644 --- a/src/routes/auth.zig +++ b/src/routes/auth.zig @@ -65,7 +65,7 @@ pub fn generateToken(user: models.User) ![]const u8 { seven_days = seven_days.addDays(7); const token: models.Token = .{ .user_id = user.id, .family_id = user.family_id, .generated_at = now, .expires_at = seven_days.toUnixMilli() }; const encoded_token = try jwt.encode(allocator, .HS256, token, .{ .key = key }); - std.log.info("Generated token for User {} @ {}", .{ user.id, now }); + // std.log.info("Generated token for User {} @ {}", .{ user.id, now }); return encoded_token; } diff --git a/src/routes/user.zig b/src/routes/user.zig index cd4530d..f845754 100644 --- a/src/routes/user.zig +++ b/src/routes/user.zig @@ -4,6 +4,7 @@ const models = @import("../db/models.zig"); const utils = @import("../utils.zig"); const auth = @import("auth.zig"); const handler = @import("../http_handler.zig"); +const ztime = @import("../.deps/time.zig"); const LoginReq = struct { username: ?[]const u8, @@ -28,6 +29,8 @@ pub fn login(req: *httpz.Request, res: *httpz.Response) !void { handler.returnError("Bad Request: No Data", 400, res); return; } + const formatted_now = ztime.DateTime.now().formatAlloc(allocator, "DD.MM.YYYY HH:mm:ss") catch "N/A"; + std.log.info("{s} {s} @ {s}\n", .{ @tagName(req.method), req.url.raw, formatted_now }); var body = body_data.?; if (body.username == null and body.email == null) { @@ -95,6 +98,9 @@ pub fn signup(req: *httpz.Request, res: *httpz.Response) !void { return; } + const formatted_now = ztime.DateTime.now().formatAlloc(allocator, "DD.MM.YYYY HH:mm:ss") catch "N/A"; + std.log.info("{s} {s} @ {s}\n", .{ @tagName(req.method), req.url.raw, formatted_now }); + var body = body_data.?; // if (body.username == null and body.email == null) { // handler.returnError("Bad Request: Missing username / email", 400, res); @@ -191,7 +197,7 @@ pub fn signup(req: *httpz.Request, res: *httpz.Response) !void { return; } - std.log.info("User created: {any}\nFamily created: {any}\n", .{ user.?, family.? }); + // std.log.info("User created: {any}\nFamily created: {any}\n", .{ user.?, family.? }); const token = try auth.generateToken(user.?);