Add config file

This commit is contained in:
jaina heartles 2022-07-29 23:14:42 -07:00
parent 99192bccdd
commit 40a2391942
3 changed files with 26 additions and 5 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
**/zig-out **/zig-out
**/zig-cache **/zig-cache
**.db **.db
/config.json

View File

@ -2,10 +2,10 @@ const std = @import("std");
const util = @import("util"); const util = @import("util");
const db = @import("./db.zig"); const db = @import("./db.zig");
const models = @import("./db/models.zig"); const models = @import("./db/models.zig");
pub const DateTime = util.DateTime; pub const DateTime = util.DateTime;
pub const Uuid = util.Uuid; pub const Uuid = util.Uuid;
const Config = @import("./main.zig").Config;
const PwHash = std.crypto.pwhash.scrypt; const PwHash = std.crypto.pwhash.scrypt;
const pw_hash_params = PwHash.Params.interactive; const pw_hash_params = PwHash.Params.interactive;
@ -101,13 +101,15 @@ pub fn initThreadPrng(seed: u64) void {
pub const ApiSource = struct { pub const ApiSource = struct {
db: db.Database, db: db.Database,
internal_alloc: std.mem.Allocator, internal_alloc: std.mem.Allocator,
config: Config,
pub const Conn = ApiConn(db.Database); pub const Conn = ApiConn(db.Database);
pub fn init(alloc: std.mem.Allocator) !ApiSource { pub fn init(alloc: std.mem.Allocator, cfg: Config) !ApiSource {
return ApiSource{ return ApiSource{
.db = try db.Database.init(), .db = try db.Database.init(),
.internal_alloc = alloc, .internal_alloc = alloc,
.config = cfg,
}; };
} }

View File

@ -34,11 +34,13 @@ const router = Router{
pub const RequestServer = struct { pub const RequestServer = struct {
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
api: api.ApiSource, api: api.ApiSource,
config: Config,
fn init(alloc: std.mem.Allocator) !RequestServer { fn init(alloc: std.mem.Allocator, config: Config) !RequestServer {
return RequestServer{ return RequestServer{
.alloc = alloc, .alloc = alloc,
.api = try api.ApiSource.init(alloc), .api = try api.ApiSource.init(alloc, config),
.config = config,
}; };
} }
@ -63,9 +65,25 @@ pub const RequestServer = struct {
} }
}; };
pub const Config = struct {
host: []const u8,
};
fn loadConfig(alloc: std.mem.Allocator) !Config {
var file = try std.fs.cwd().openFile("config.json", .{});
defer file.close();
const bytes = try file.reader().readAllAlloc(alloc, 1 << 63);
defer alloc.free(bytes);
var ts = std.json.TokenStream.init(bytes);
return std.json.parse(Config, &ts, .{ .allocator = alloc });
}
pub fn main() anyerror!void { pub fn main() anyerror!void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var srv = try RequestServer.init(gpa.allocator()); const cfg = try loadConfig(gpa.allocator());
var srv = try RequestServer.init(gpa.allocator(), cfg);
api.initThreadPrng(@bitCast(u64, std.time.milliTimestamp())); api.initThreadPrng(@bitCast(u64, std.time.milliTimestamp()));
return srv.listenAndRun(std.net.Address.parseIp("0.0.0.0", 8080) catch unreachable); return srv.listenAndRun(std.net.Address.parseIp("0.0.0.0", 8080) catch unreachable);
} }