|
1 | 1 | //! This module keeps track of the last known good API IP address and reads and stores it on disk.
|
2 | 2 |
|
3 |
| -use super::API; |
4 |
| -use crate::DnsResolver; |
| 3 | +use crate::{ApiEndpoint, DnsResolver}; |
5 | 4 | use async_trait::async_trait;
|
6 | 5 | use std::{io, net::SocketAddr, path::Path, sync::Arc};
|
7 | 6 | use tokio::{
|
@@ -38,42 +37,42 @@ impl DnsResolver for AddressCache {
|
38 | 37 |
|
39 | 38 | #[derive(Clone)]
|
40 | 39 | pub struct AddressCache {
|
| 40 | + hostname: String, |
41 | 41 | inner: Arc<Mutex<AddressCacheInner>>,
|
42 | 42 | write_path: Option<Arc<Path>>,
|
43 | 43 | }
|
44 | 44 |
|
45 | 45 | impl AddressCache {
|
46 | 46 | /// Initialize cache using the hardcoded address, and write changes to `write_path`.
|
47 |
| - pub fn new(write_path: Option<Box<Path>>) -> Self { |
48 |
| - Self::new_inner(API.address(), write_path) |
49 |
| - } |
50 |
| - |
51 |
| - pub fn with_static_addr(address: SocketAddr) -> Self { |
52 |
| - Self::new_inner(address, None) |
| 47 | + pub fn new(endpoint: &ApiEndpoint, write_path: Option<Box<Path>>) -> Self { |
| 48 | + Self::new_inner(endpoint.address(), endpoint.host().to_owned(), write_path) |
53 | 49 | }
|
54 | 50 |
|
55 | 51 | /// Initialize cache using `read_path`, and write changes to `write_path`.
|
56 |
| - pub async fn from_file(read_path: &Path, write_path: Option<Box<Path>>) -> Result<Self, Error> { |
| 52 | + pub async fn from_file( |
| 53 | + read_path: &Path, |
| 54 | + write_path: Option<Box<Path>>, |
| 55 | + hostname: String, |
| 56 | + ) -> Result<Self, Error> { |
57 | 57 | log::debug!("Loading API addresses from {}", read_path.display());
|
58 |
| - Ok(Self::new_inner( |
59 |
| - read_address_file(read_path).await?, |
60 |
| - write_path, |
61 |
| - )) |
| 58 | + let address = read_address_file(read_path).await?; |
| 59 | + Ok(Self::new_inner(address, hostname, write_path)) |
62 | 60 | }
|
63 | 61 |
|
64 |
| - fn new_inner(address: SocketAddr, write_path: Option<Box<Path>>) -> Self { |
| 62 | + fn new_inner(address: SocketAddr, hostname: String, write_path: Option<Box<Path>>) -> Self { |
65 | 63 | let cache = AddressCacheInner::from_address(address);
|
66 | 64 | log::debug!("Using API address: {}", cache.address);
|
67 | 65 |
|
68 | 66 | Self {
|
69 | 67 | inner: Arc::new(Mutex::new(cache)),
|
70 | 68 | write_path: write_path.map(Arc::from),
|
| 69 | + hostname, |
71 | 70 | }
|
72 | 71 | }
|
73 | 72 |
|
74 | 73 | /// Returns the address if the hostname equals `API.host`. Otherwise, returns `None`.
|
75 | 74 | async fn resolve_hostname(&self, hostname: &str) -> Option<SocketAddr> {
|
76 |
| - if hostname.eq_ignore_ascii_case(API.host()) { |
| 75 | + if hostname.eq_ignore_ascii_case(&self.hostname) { |
77 | 76 | Some(self.get_address().await)
|
78 | 77 | } else {
|
79 | 78 | None
|
|
0 commit comments