address location fallback logic, for now
This commit is contained in:
parent
9c7902aca8
commit
7d06d281e0
3 changed files with 27 additions and 5 deletions
|
|
@ -24,9 +24,21 @@ pub fn handleWeather(
|
||||||
req: *httpz.Request,
|
req: *httpz.Request,
|
||||||
res: *httpz.Response,
|
res: *httpz.Response,
|
||||||
) !void {
|
) !void {
|
||||||
// Get client IP for location detection
|
// Check for location query parameter first
|
||||||
const client_ip = getClientIP(req);
|
const query_string = req.url.query;
|
||||||
try handleWeatherInternal(opts, req, res, client_ip);
|
const params = try QueryParams.parse(req.arena, query_string);
|
||||||
|
defer {
|
||||||
|
if (params.format) |f| req.arena.free(f);
|
||||||
|
if (params.lang) |l| req.arena.free(l);
|
||||||
|
}
|
||||||
|
|
||||||
|
const location = if (params.location) |loc| loc else blk: {
|
||||||
|
// Fall back to IP-based detection
|
||||||
|
const client_ip = getClientIP(req);
|
||||||
|
break :blk client_ip;
|
||||||
|
};
|
||||||
|
|
||||||
|
try handleWeatherInternal(opts, req, res, location);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handleWeatherLocation(
|
pub fn handleWeatherLocation(
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ const std = @import("std");
|
||||||
pub const QueryParams = struct {
|
pub const QueryParams = struct {
|
||||||
format: ?[]const u8 = null,
|
format: ?[]const u8 = null,
|
||||||
lang: ?[]const u8 = null,
|
lang: ?[]const u8 = null,
|
||||||
|
location: ?[]const u8 = null,
|
||||||
units: ?Units = null,
|
units: ?Units = null,
|
||||||
transparency: ?u8 = null,
|
transparency: ?u8 = null,
|
||||||
|
|
||||||
|
|
@ -26,10 +27,16 @@ pub const QueryParams = struct {
|
||||||
params.format = if (value) |v| try allocator.dupe(u8, v) else null;
|
params.format = if (value) |v| try allocator.dupe(u8, v) else null;
|
||||||
} else if (std.mem.eql(u8, key, "lang")) {
|
} else if (std.mem.eql(u8, key, "lang")) {
|
||||||
params.lang = if (value) |v| try allocator.dupe(u8, v) else null;
|
params.lang = if (value) |v| try allocator.dupe(u8, v) else null;
|
||||||
|
} else if (std.mem.eql(u8, key, "location")) {
|
||||||
|
params.location = if (value) |v| try allocator.dupe(u8, v) else null;
|
||||||
} else if (std.mem.eql(u8, key, "u")) {
|
} else if (std.mem.eql(u8, key, "u")) {
|
||||||
params.units = .uscs;
|
params.units = .uscs;
|
||||||
} else if (std.mem.eql(u8, key, "m")) {
|
} else if (std.mem.eql(u8, key, "m")) {
|
||||||
params.units = .metric;
|
params.units = .metric;
|
||||||
|
} else if (std.mem.eql(u8, key, "use_imperial")) {
|
||||||
|
params.units = .uscs;
|
||||||
|
} else if (std.mem.eql(u8, key, "use_metric")) {
|
||||||
|
params.units = .metric;
|
||||||
} else if (std.mem.eql(u8, key, "transparency")) {
|
} else if (std.mem.eql(u8, key, "transparency")) {
|
||||||
if (value) |v| {
|
if (value) |v| {
|
||||||
params.transparency = try std.fmt.parseInt(u8, v, 10);
|
params.transparency = try std.fmt.parseInt(u8, v, 10);
|
||||||
|
|
|
||||||
|
|
@ -107,14 +107,17 @@ pub const Resolver = struct {
|
||||||
|
|
||||||
var response_buf: [1024 * 1024]u8 = undefined;
|
var response_buf: [1024 * 1024]u8 = undefined;
|
||||||
var writer = std.Io.Writer.fixed(&response_buf);
|
var writer = std.Io.Writer.fixed(&response_buf);
|
||||||
const result = try client.fetch(.{
|
const result = client.fetch(.{
|
||||||
.location = .{ .uri = uri },
|
.location = .{ .uri = uri },
|
||||||
.method = .GET,
|
.method = .GET,
|
||||||
.response_writer = &writer,
|
.response_writer = &writer,
|
||||||
.extra_headers = &.{
|
.extra_headers = &.{
|
||||||
.{ .name = "User-Agent", .value = "wttr.in-zig/1.0" },
|
.{ .name = "User-Agent", .value = "wttr.in-zig/1.0" },
|
||||||
},
|
},
|
||||||
});
|
}) catch {
|
||||||
|
std.log.warn("Nominatim API call failed for: {s}", .{name});
|
||||||
|
return error.GeocodingFailed;
|
||||||
|
};
|
||||||
|
|
||||||
if (result.status != .ok) {
|
if (result.status != .ok) {
|
||||||
return error.GeocodingFailed;
|
return error.GeocodingFailed;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue