1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use clap::{App, Arg};
use std::process;
use std::time::Duration;
fn is_num(s: String) -> Result<(), String> {
if let Err(..) = s.parse::<usize>() {
return Err(String::from("Not a valid number!"));
}
Ok(())
}
#[must_use]
pub fn new() -> (bool, u16, mysql_async::Pool) {
let matches = App::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION"))
.arg(
Arg::with_name("dsn")
.env("DSN")
.help("mysql://<username>:<password>@tcp(<host>:<port>)/<database>")
.long("dsn")
.short("d")
.required(true),
)
.arg(
Arg::with_name("port")
.default_value("9200")
.help("listening port")
.long("port")
.validator(is_num)
.required(true),
)
.arg(
Arg::with_name("v46")
.help("listen in both IPv4 and IPv6")
.long("46"),
)
.get_matches();
let dsn = matches.value_of("dsn").unwrap();
let dsn = dsn::parse(dsn).unwrap_or_else(|e| {
eprintln!("{}", e);
process::exit(1);
});
let opts = mysql_async::OptsBuilder::default()
.user(dsn.username)
.pass(dsn.password)
.db_name(dsn.database)
.ip_or_hostname(dsn.host.unwrap_or_else(|| String::from("127.0.0.1")))
.tcp_port(dsn.port.unwrap_or(3306))
.socket(dsn.socket)
.conn_ttl(Duration::new(60, 0));
if let Some(tls) = dsn.params.get("tls") {
if *tls == "skip-verify" {
mysql_async::SslOpts::default().with_danger_accept_invalid_certs(true);
}
}
let port = matches.value_of("port").unwrap().parse::<u16>().unwrap();
(
matches.is_present("v46"),
port,
mysql_async::Pool::new(opts),
)
}