clean up geolite db download process
This commit is contained in:
parent
3a837e2c1f
commit
7e7fd77ec7
2 changed files with 7 additions and 4 deletions
|
|
@ -1,10 +1,11 @@
|
|||
const std = @import("std");
|
||||
const log = std.log.scoped(.geolite2);
|
||||
|
||||
pub fn ensureDatabase(allocator: std.mem.Allocator, path: []const u8) !void {
|
||||
std.fs.cwd().access(path, .{}) catch {
|
||||
std.log.info("GeoLite2 database not found at {s}, downloading...", .{path});
|
||||
log.info("GeoLite2 database not found at {s}, will download", .{path});
|
||||
try downloadDatabase(allocator, path);
|
||||
std.log.info("GeoLite2 database downloaded successfully", .{});
|
||||
log.info("GeoLite2 database downloaded successfully", .{});
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
|
@ -39,6 +40,7 @@ fn getLatestReleaseUrl(allocator: std.mem.Allocator) ![]const u8 {
|
|||
defer client.deinit();
|
||||
|
||||
const api_url = "https://api.github.com/repos/P3TERX/GeoLite.mmdb/releases/latest";
|
||||
log.info("Finding latest release from {s}", .{api_url});
|
||||
const uri = try std.Uri.parse(api_url);
|
||||
|
||||
const response_buf = try allocator.alloc(u8, 1024 * 1024);
|
||||
|
|
@ -65,6 +67,7 @@ fn getLatestReleaseUrl(allocator: std.mem.Allocator) ![]const u8 {
|
|||
const name = asset.object.get("name") orelse continue;
|
||||
if (std.mem.eql(u8, name.string, "GeoLite2-City.mmdb")) {
|
||||
const url = asset.object.get("browser_download_url") orelse return error.NoDownloadUrl;
|
||||
log.info("Latest release url is {s}", .{url.string});
|
||||
return allocator.dupe(u8, url.string);
|
||||
}
|
||||
}
|
||||
|
|
@ -9,7 +9,7 @@ const GeoIp = @import("location/GeoIp.zig");
|
|||
const GeoCache = @import("location/GeoCache.zig");
|
||||
const Airports = @import("location/Airports.zig");
|
||||
const Resolver = @import("location/resolver.zig").Resolver;
|
||||
const geolite_downloader = @import("location/geolite_downloader.zig");
|
||||
const GeoLite2 = @import("location/GeoLite2.zig");
|
||||
|
||||
pub const std_options: std.Options = .{
|
||||
.log_level = .info,
|
||||
|
|
@ -42,7 +42,7 @@ pub fn main() !void {
|
|||
try stdout.flush();
|
||||
|
||||
// Ensure GeoLite2 database exists
|
||||
try geolite_downloader.ensureDatabase(allocator, cfg.geolite_path);
|
||||
try GeoLite2.ensureDatabase(allocator, cfg.geolite_path);
|
||||
|
||||
// Initialize GeoIP database
|
||||
var geoip = GeoIp.init(cfg.geolite_path) catch |err| {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue