Fixes for passing in sqlite file name

This commit is contained in:
Nathan Anderson 2023-07-27 12:30:56 -06:00
parent cdabcf61f3
commit fb955625bc
5 changed files with 48 additions and 13 deletions

View File

@ -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,

View File

@ -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 });

View File

@ -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();
}

View File

@ -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;
}

View File

@ -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.?);