Skip to content

Commit 0db9675

Browse files
committed
add: stuff
1 parent f3b2417 commit 0db9675

File tree

8 files changed

+105
-109
lines changed

8 files changed

+105
-109
lines changed

docs/example-config.yaml

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,54 @@
1-
# Example DNRS configuration
2-
31
resolver:
42
ipv4:
53
url: https://ip.cancom.io
64
type: Raw
75
ipv6:
86
url: https://ipv6.cancom.io
97
type: Raw
10-
118
providers:
12-
- Nitrado:
13-
name: MyNitradoProvider
14-
api_key: your-nitrado-api-key-here
15-
api_base_url: https://api.nitrado.net
16-
9+
- Nitrado:
10+
name: Nitrado1
11+
api_key: your_api_key
12+
api_base_url: https://api.nitrado.net
1713
dns:
18-
- Nitrado:
19-
provider_name: MyNitradoProvider
20-
domains:
21-
- domain: example.com
22-
records:
23-
# Use !Manual to define static records
24-
- !Manual
25-
domain: www
26-
value: !A 127.0.0.1
27-
ttl: 3600
28-
- !Manual
29-
domain: mail
30-
value: !MX
31-
priority: 10
32-
target: mail.example.com
33-
ttl: 3600
34-
- !Manual
35-
domain: ipv6-test
36-
value: !AAAA ::1
37-
ttl: 3600
38-
- !Manual
39-
domain: alias
40-
value: !CNAME example.com
41-
ttl: 300
42-
- !Manual
43-
domain: txt-record
44-
value: !TXT "v=spf1 include:_spf.google.com ~all"
45-
ttl: 300
46-
47-
# Use !Automatic to resolve IPs dynamically
48-
- !Automatic
49-
domain: dynamic-ipv4
50-
ttl: 300
51-
resolve_type: IPv4
52-
- !Automatic
53-
domain: dynamic-ipv6
54-
ttl: 300
55-
resolve_type: IPv6
14+
- Nitrado:
15+
provider_name: Nitrado1
16+
domains:
17+
- domain: example.com
18+
records:
19+
- !Manual
20+
domain: ipv4
21+
value: !A 127.0.0.1
22+
ttl: 3600
23+
- !Manual
24+
domain: ipv6
25+
value: !AAAA ::1
26+
ttl: 3600
27+
- !Manual
28+
domain: forward
29+
value: !CNAME example.com
30+
ttl: 3600
31+
- !Manual
32+
domain: '@'
33+
value: !MX
34+
priority: 10
35+
target: mail.example.com
36+
ttl: 3600
37+
- !Manual
38+
domain: '@'
39+
value: !TXT v=spf1 include:example.com ~all
40+
ttl: 3600
41+
- !Manual
42+
domain: '@'
43+
value: !Custom
44+
- RecordType
45+
- Value
46+
ttl: 3600
47+
- !Automatic
48+
domain: auto-ipv4
49+
ttl: 300
50+
resolve_type: IPv4
51+
- !Automatic
52+
domain: auto-ipv6
53+
ttl: 300
54+
resolve_type: IPv6

src/config.rs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,27 @@ pub mod dns;
55
pub mod provider;
66
pub mod resolver;
77

8-
#[derive(Debug, Clone, Serialize, Deserialize)]
9-
#[serde(crate = "lum_libs::serde")]
10-
#[serde(default)]
11-
pub struct FileConfig {
12-
resolver: resolver::FileConfig,
13-
providers: Vec<provider::FileConfig>,
14-
dns: Vec<dns::FileConfig>,
15-
}
16-
17-
impl Default for FileConfig {
18-
fn default() -> Self {
19-
FileConfig {
20-
resolver: resolver::FileConfig::default(),
21-
providers: vec![provider::FileConfig::default()],
22-
dns: vec![dns::FileConfig::default()],
23-
}
24-
}
25-
}
26-
278
#[derive(Debug, Clone, Serialize, Deserialize)]
289
#[serde(crate = "lum_libs::serde")]
2910
#[serde(default)]
3011
pub struct Config {
31-
pub resolver: resolver::FileConfig,
32-
pub providers: Vec<provider::FileConfig>,
33-
pub dns: Vec<dns::FileConfig>,
12+
pub resolver: resolver::Config,
13+
pub providers: Vec<provider::Config>,
14+
pub dns: Vec<dns::Config>,
3415
}
3516

3617
impl Default for Config {
3718
fn default() -> Self {
38-
let file_config = FileConfig::default();
39-
4019
Config {
41-
resolver: file_config.resolver,
42-
providers: file_config.providers,
43-
dns: file_config.dns,
20+
resolver: resolver::Config::default(),
21+
providers: vec![provider::Config::default()],
22+
dns: vec![dns::Config::default()],
4423
}
4524
}
4625
}
4726

48-
impl MergeFrom<FileConfig> for Config {
49-
fn merge_from(self, other: FileConfig) -> Self {
27+
impl MergeFrom<Self> for Config {
28+
fn merge_from(self, other: Self) -> Self {
5029
Self {
5130
resolver: other.resolver,
5231
providers: other.providers,

src/config/dns.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ pub enum ResolveType {
3232

3333
#[derive(Debug, Clone, Serialize, Deserialize)]
3434
#[serde(crate = "lum_libs::serde")]
35-
pub struct FileConfig {
35+
pub struct Config {
3636
#[serde(flatten)]
3737
pub dns: Type,
3838
}
3939

40-
impl Default for FileConfig {
40+
impl Default for Config {
4141
fn default() -> Self {
42-
FileConfig {
42+
Config {
4343
dns: Type::Nitrado(nitrado::DnsConfig::default()),
4444
}
4545
}

src/config/provider.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ pub enum Provider {
1010

1111
#[derive(Debug, Clone, Serialize, Deserialize)]
1212
#[serde(crate = "lum_libs::serde")]
13-
pub struct FileConfig {
13+
pub struct Config {
1414
#[serde(flatten)]
1515
pub provider: Provider,
1616
}
1717

18-
impl Default for FileConfig {
18+
impl Default for Config {
1919
fn default() -> Self {
20-
FileConfig {
20+
Config {
2121
provider: Provider::Nitrado(nitrado::ProviderConfig::default()),
2222
}
2323
}

src/config/resolver.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ pub struct IpResolver {
1818

1919
#[derive(Debug, Clone, Serialize, Deserialize)]
2020
#[serde(crate = "lum_libs::serde")]
21-
pub struct FileConfig {
21+
pub struct Config {
2222
pub ipv4: IpResolver,
2323
pub ipv6: IpResolver,
2424
}
2525

26-
impl Default for FileConfig {
26+
impl Default for Config {
2727
fn default() -> Self {
28-
FileConfig {
28+
Config {
2929
ipv4: IpResolver {
3030
url: "https://ip.cancom.io".to_string(),
3131
type_: IpResolverType::Raw,

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub mod provider;
1212
pub mod resolver;
1313
pub mod types;
1414

15-
pub use config::{Config, FileConfig};
15+
pub use config::Config;
1616
pub use logger::setup_logger;
1717

1818
pub const PROGRAM_NAME: &str = env!("CARGO_PKG_NAME");

src/main.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::fmt::{self, Debug};
2-
use std::fs::File;
2+
use std::fs::{self, File};
33

4-
use dnrs::{Config, FileConfig, RuntimeError, run, setup_logger};
4+
use dnrs::{Config, RuntimeError, run, setup_logger};
55
use lum_config::{ConfigPathError, EnvironmentConfigParseError, FileConfigParseError, merge};
6-
use lum_log::{debug, error, log::SetLoggerError};
6+
use lum_log::info;
7+
use lum_log::{error, log::SetLoggerError};
78
use thiserror::Error;
89

910
/*
@@ -57,9 +58,7 @@ enum Error {
5758
#[error("IO error: {0}")]
5859
Io(#[from] std::io::Error),
5960

60-
#[error(
61-
"Unable to determine config directory. No config directory, home directory, or temp directory available."
62-
)]
61+
#[error("Unable to determine config directory")]
6362
NoConfigDirectory,
6463

6564
#[error("Runtime error: {0}")]
@@ -78,31 +77,32 @@ async fn main() -> Result<(), Error> {
7877
setup_logger()?;
7978

8079
let config_path = dirs::config_dir()
81-
.or_else(|| dirs::home_dir())
82-
.or_else(|| Some(std::env::temp_dir()))
8380
.ok_or(Error::NoConfigDirectory)?
8481
.join(APP_NAME)
8582
.join("config.yaml");
8683

87-
let file_config: FileConfig = if config_path.exists() {
84+
let mut loaded_config: Option<Config> = None;
85+
if config_path.exists() {
8886
let file = File::open(&config_path)?;
89-
serde_yaml::from_reader(file)?
90-
} else {
91-
let default_config = FileConfig::default();
92-
93-
if let Some(parent) = config_path.parent() {
94-
std::fs::create_dir_all(parent)?;
95-
}
96-
97-
let file = File::create(&config_path)?;
98-
serde_yaml::to_writer(file, &default_config)?;
87+
loaded_config = Some(serde_yaml::from_reader(file)?);
88+
}
89+
let config_existed = loaded_config.is_some();
9990

100-
debug!("Created default config file at: {}", config_path.display());
101-
default_config
91+
let config = Config::default();
92+
let config = match loaded_config {
93+
Some(loaded_config) => merge(config, loaded_config),
94+
None => config,
10295
};
10396

104-
let config = Config::default();
105-
let config = merge(config, file_config);
97+
if let Some(parent) = config_path.parent() {
98+
fs::create_dir_all(parent)?;
99+
}
100+
let file = File::create(&config_path)?;
101+
serde_yaml::to_writer(file, &config)?;
102+
103+
if !config_existed {
104+
info!("Created default config file at: {}", config_path.display());
105+
}
106106

107107
run(config).await?;
108108
Ok(())

src/provider/nitrado.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use thiserror::Error;
1515
use crate::{
1616
config::dns::{AutomaticRecordConfig, RecordConfig, ResolveType},
1717
provider::{self, Feature, Provider},
18-
types::dns::{self, Record, RecordValue},
18+
types::dns::{self, MxRecord, Record, RecordValue},
1919
};
2020

2121
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -72,6 +72,24 @@ impl Default for DnsConfig {
7272
value: RecordValue::CNAME("example.com".to_string()),
7373
ttl: Some(3600),
7474
}),
75+
RecordConfig::Manual(dns::Record {
76+
domain: "@".to_string(),
77+
value: RecordValue::MX(MxRecord {
78+
priority: 10,
79+
target: "mail.example.com".to_string(),
80+
}),
81+
ttl: Some(3600),
82+
}),
83+
RecordConfig::Manual(dns::Record {
84+
domain: "@".to_string(),
85+
value: RecordValue::TXT("v=spf1 include:example.com ~all".to_string()),
86+
ttl: Some(3600),
87+
}),
88+
RecordConfig::Manual(dns::Record {
89+
domain: "@".to_string(),
90+
value: RecordValue::Custom("RecordType".to_string(), "Value".to_string()),
91+
ttl: Some(3600),
92+
}),
7593
RecordConfig::Automatic(AutomaticRecordConfig {
7694
domain: "auto-ipv4".to_string(),
7795
ttl: Some(300),

0 commit comments

Comments
 (0)