@@ -24,12 +24,11 @@ pub struct LookupResult {
2424 pub country_code : Option < String > ,
2525}
2626
27- #[ derive( Clone ) ]
2827pub struct LiwanGeoIP {
2928 pool : SqlitePool ,
30- reader : Arc < ArcSwapOption < maxminddb:: Reader < Vec < u8 > > > > ,
29+ reader : ArcSwapOption < maxminddb:: Reader < Vec < u8 > > > ,
3130
32- downloading : Arc < AtomicBool > ,
31+ downloading : AtomicBool ,
3332 geoip : crate :: config:: GeoIpConfig ,
3433 path : PathBuf ,
3534}
@@ -61,7 +60,7 @@ impl LiwanGeoIP {
6160 maxminddb:: Reader :: open_readfile ( path. clone ( ) ) . expect ( "Failed to open GeoIP database file" ) . into ( )
6261 } ) ;
6362
64- Ok ( Self { geoip, pool, reader : ArcSwapOption :: new ( reader) . into ( ) , path, downloading : Default :: default ( ) } )
63+ Ok ( Self { geoip, pool, reader : ArcSwapOption :: new ( reader) , path, downloading : Default :: default ( ) } )
6564 }
6665
6766 fn is_enabled ( & self ) -> bool {
@@ -72,7 +71,7 @@ impl LiwanGeoIP {
7271 Self {
7372 geoip : Default :: default ( ) ,
7473 pool,
75- reader : ArcSwapOption :: new ( None ) . into ( ) ,
74+ reader : ArcSwapOption :: new ( None ) ,
7675 downloading : Default :: default ( ) ,
7776 path : PathBuf :: new ( ) ,
7877 }
@@ -84,11 +83,9 @@ impl LiwanGeoIP {
8483 return Ok ( Default :: default ( ) ) ;
8584 } ;
8685
87- let lookup =
88- reader. lookup :: < maxminddb:: geoip2:: City > ( * ip) ?. ok_or_else ( || anyhow ! ( "No data found for IP address" ) ) ?;
89-
90- let city = lookup. city . and_then ( |city| city. names . and_then ( |names| names. get ( "en" ) . map ( |s| ( * s) . to_string ( ) ) ) ) ;
91- let country_code = lookup. country . and_then ( |country| country. iso_code . map ( ToString :: to_string) ) ;
86+ let lookup = reader. lookup ( * ip) ?. decode :: < maxminddb:: geoip2:: City > ( ) . context ( "failed to decode data" ) ?;
87+ let city = lookup. as_ref ( ) . and_then ( |lookup| lookup. city . names . english . map ( |v| v. to_string ( ) ) ) ;
88+ let country_code = lookup. and_then ( |lookup| lookup. country . iso_code . map ( |v| v. to_string ( ) ) ) ;
9289 Ok ( LookupResult { city, country_code } )
9390 }
9491
@@ -154,7 +151,7 @@ impl LiwanGeoIP {
154151 }
155152}
156153
157- pub fn keep_updated ( geoip : LiwanGeoIP ) {
154+ pub fn keep_updated ( geoip : Arc < LiwanGeoIP > ) {
158155 if !geoip. is_enabled ( ) {
159156 return ;
160157 }
@@ -230,11 +227,11 @@ async fn download_maxmind_db(edition: &str, account_id: &str, license_key: &str)
230227 entry. set_allow_external_symlinks ( false ) ;
231228 entry. set_preserve_permissions ( false ) ;
232229
233- return Ok ( entry
230+ return entry
234231 . unpack_in ( folder)
235232 . await
236233 . context ( "Failed to unpack entry" ) ?
237- . ok_or_else ( || anyhow ! ( "Failed to unpack entry" ) ) ? ) ;
234+ . ok_or_else ( || anyhow ! ( "Failed to unpack entry" ) ) ;
238235 }
239236 }
240237}
0 commit comments