You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.5 KiB
93 lines
2.5 KiB
extern crate actix_web;
|
|
extern crate bytes;
|
|
extern crate clap;
|
|
extern crate cloudflare;
|
|
extern crate env_logger;
|
|
extern crate futures;
|
|
#[macro_use]
|
|
extern crate lazy_static;
|
|
#[macro_use]
|
|
extern crate log;
|
|
extern crate num_cpus;
|
|
extern crate serde;
|
|
#[macro_use]
|
|
extern crate serde_derive;
|
|
extern crate serde_yaml;
|
|
|
|
use std::process;
|
|
use std::sync::Arc;
|
|
|
|
use cloudflare::Cloudflare;
|
|
|
|
mod args;
|
|
mod config;
|
|
mod server;
|
|
|
|
|
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
|
const DEFAULT_HOST: &str = "localhost";
|
|
const DEFAULT_PORT: i16 = 8080;
|
|
|
|
lazy_static! {
|
|
static ref DEFAULT_PORT_STR: String = DEFAULT_PORT.to_string();
|
|
static ref DEFAULT_WORKERS: usize = num_cpus::get();
|
|
static ref DEFAULT_WORKERS_STR: String = DEFAULT_WORKERS.to_string();
|
|
}
|
|
|
|
fn main() {
|
|
match std::env::var("RUST_LOG") {
|
|
Ok(_) => (),
|
|
Err(_) => std::env::set_var("RUST_LOG", "error,rsddns=info,actix_web=info")
|
|
}
|
|
env_logger::init();
|
|
|
|
let args = args::get_app().get_matches();
|
|
|
|
let config_path: &str = args.value_of("config").unwrap_or("/etc/rsddns/rsddns.yml");
|
|
let config = match config::load::read(config_path) {
|
|
Ok(c) => {
|
|
info!("Loaded configuration from \"{}\"", config_path);
|
|
c
|
|
}
|
|
Err(e) => {
|
|
error!("{}", e);
|
|
process::exit(-1)
|
|
}
|
|
};
|
|
|
|
match config::validate::validate(&config) {
|
|
Ok(_) => info!("Configuration Is Valid"),
|
|
Err(e) => {
|
|
error!("{}", e);
|
|
process::exit(-1)
|
|
}
|
|
}
|
|
|
|
let host = args::parse::get_host(&args, &config, DEFAULT_HOST);
|
|
let port = args::parse::get_port(&args, &config, DEFAULT_PORT);
|
|
let workers = args::parse::get_workers(&args, &config, *DEFAULT_WORKERS);
|
|
|
|
let bind = format!("{}:{}", host, port);
|
|
info!(
|
|
"Starting server on {} with workers={} and config \"{}\"",
|
|
bind, workers, config_path
|
|
);
|
|
|
|
let shared_config = Arc::new(config);
|
|
let shared_cloudflare = match Cloudflare::new(
|
|
&shared_config.cloudflare.key,
|
|
&shared_config.cloudflare.email,
|
|
"https://api.cloudflare.com/client/v4/") {
|
|
Ok(api) => Arc::new(api),
|
|
Err(_e) => process::exit(-1)
|
|
};
|
|
|
|
let actix_server = actix_web::server::new(move || server::router::create(shared_config.clone(), shared_cloudflare.clone()))
|
|
.workers(workers)
|
|
.bind(bind);
|
|
|
|
match actix_server {
|
|
Ok(server) => server.run(),
|
|
Err(e) => error!("{}", e)
|
|
}
|
|
}
|