diff options
-rw-r--r-- | Cargo.lock | 8 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/cli.rs | 35 | ||||
-rw-r--r-- | src/db.rs | 8 | ||||
-rw-r--r-- | src/main.rs | 12 |
5 files changed, 57 insertions, 8 deletions
@@ -390,9 +390,11 @@ version = "0.1.0" | |||
390 | dependencies = [ | 390 | dependencies = [ |
391 | "anyhow", | 391 | "anyhow", |
392 | "hyper", | 392 | "hyper", |
393 | "lazy_static", | ||
393 | "log", | 394 | "log", |
394 | "multer", | 395 | "multer", |
395 | "nanoid", | 396 | "nanoid", |
397 | "pico-args", | ||
396 | "pretty_env_logger", | 398 | "pretty_env_logger", |
397 | "rusqlite", | 399 | "rusqlite", |
398 | "smol", | 400 | "smol", |
@@ -694,6 +696,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
694 | checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" | 696 | checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" |
695 | 697 | ||
696 | [[package]] | 698 | [[package]] |
699 | name = "pico-args" | ||
700 | version = "0.3.4" | ||
701 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
702 | checksum = "28b9b4df73455c861d7cbf8be42f01d3b373ed7f02e378d55fa84eafc6f638b1" | ||
703 | |||
704 | [[package]] | ||
697 | name = "pin-project" | 705 | name = "pin-project" |
698 | version = "0.4.23" | 706 | version = "0.4.23" |
699 | source = "registry+https://github.com/rust-lang/crates.io-index" | 707 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -15,6 +15,8 @@ anyhow = "1.0" | |||
15 | multer = "1.2" | 15 | multer = "1.2" |
16 | log = "0.4" | 16 | log = "0.4" |
17 | pretty_env_logger = "0.4.0" | 17 | pretty_env_logger = "0.4.0" |
18 | pico-args = "0.3.4" | ||
19 | lazy_static = "1.4.0" | ||
18 | 20 | ||
19 | 21 | ||
20 | [dependencies.smol] | 22 | [dependencies.smol] |
diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..009397d --- /dev/null +++ b/src/cli.rs | |||
@@ -0,0 +1,35 @@ | |||
1 | use anyhow::Result; | ||
2 | use lazy_static::lazy_static; | ||
3 | |||
4 | use std::default::Default; | ||
5 | use std::path::PathBuf; | ||
6 | |||
7 | pub struct Config { | ||
8 | pub port: u16, | ||
9 | pub db_path: PathBuf, | ||
10 | } | ||
11 | |||
12 | impl Default for Config { | ||
13 | fn default() -> Self { | ||
14 | Config { | ||
15 | port: 3000, | ||
16 | db_path: "./urls.db_3".into(), | ||
17 | } | ||
18 | } | ||
19 | } | ||
20 | |||
21 | lazy_static! { | ||
22 | pub static ref CONFIG: Config = parse_args().unwrap_or(Default::default()); | ||
23 | } | ||
24 | |||
25 | fn parse_args() -> Result<Config> { | ||
26 | let mut _a = pico_args::Arguments::from_env(); | ||
27 | return Ok(Config { | ||
28 | port: _a | ||
29 | .opt_value_from_fn("--port", str::parse::<u16>)? | ||
30 | .unwrap_or(7878), | ||
31 | db_path: _a | ||
32 | .opt_value_from_str("--database")? | ||
33 | .unwrap_or(PathBuf::from("./urls.db_3")), | ||
34 | }); | ||
35 | } | ||
@@ -9,7 +9,7 @@ use std::path::Path; | |||
9 | 9 | ||
10 | pub fn open_connection<P>(p: P) -> Result<Connection> | 10 | pub fn open_connection<P>(p: P) -> Result<Connection> |
11 | where | 11 | where |
12 | P: AsRef<Path> + fmt::Display, | 12 | P: AsRef<Path>, |
13 | { | 13 | { |
14 | info!("Opened connection to database"); | 14 | info!("Opened connection to database"); |
15 | Ok(Connection::open_with_flags( | 15 | Ok(Connection::open_with_flags( |
@@ -18,8 +18,8 @@ where | |||
18 | )?) | 18 | )?) |
19 | } | 19 | } |
20 | 20 | ||
21 | pub fn init_db<P: AsRef<Path> + fmt::Display>(p: P) -> Result<()> { | 21 | pub fn init_db<P: AsRef<Path> + fmt::Debug>(p: P) -> Result<()> { |
22 | debug!("Looking for database at `{}`", p); | 22 | debug!("Looking for database at `{:?}`", p); |
23 | let conn = open_connection(&p)?; | 23 | let conn = open_connection(&p)?; |
24 | conn.execute( | 24 | conn.execute( |
25 | "CREATE TABLE IF NOT EXISTS urls ( | 25 | "CREATE TABLE IF NOT EXISTS urls ( |
@@ -28,6 +28,6 @@ pub fn init_db<P: AsRef<Path> + fmt::Display>(p: P) -> Result<()> { | |||
28 | )", | 28 | )", |
29 | NO_PARAMS, | 29 | NO_PARAMS, |
30 | )?; | 30 | )?; |
31 | info!("SQLite3 database `{}` initialized", &p); | 31 | info!("SQLite3 database `{:?}` initialized", &p); |
32 | Ok(()) | 32 | Ok(()) |
33 | } | 33 | } |
diff --git a/src/main.rs b/src/main.rs index 35e686e..5cd3492 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -11,14 +11,18 @@ use db::{init_db, open_connection}; | |||
11 | mod service; | 11 | mod service; |
12 | use service::shortner_service; | 12 | use service::shortner_service; |
13 | 13 | ||
14 | mod cli; | ||
15 | use cli::CONFIG; | ||
16 | |||
14 | fn main() -> Result<()> { | 17 | fn main() -> Result<()> { |
15 | pretty_env_logger::init(); | 18 | pretty_env_logger::init(); |
16 | init_db("./urls.db_3")?; | 19 | |
20 | init_db(&CONFIG.db_path)?; | ||
17 | smol::run(async { | 21 | smol::run(async { |
18 | let addr = ([127, 0, 0, 1], 3000).into(); | 22 | let addr = ([127, 0, 0, 1], CONFIG.port).into(); |
19 | let service = make_service_fn(move |_| async { | 23 | let service = make_service_fn(|_| async { |
20 | Ok::<_, hyper::Error>(service_fn(move |req| { | 24 | Ok::<_, hyper::Error>(service_fn(move |req| { |
21 | let db_conn = open_connection("./urls.db_3").unwrap(); | 25 | let db_conn = open_connection(&CONFIG.db_path).unwrap(); |
22 | shortner_service(req, db_conn) | 26 | shortner_service(req, db_conn) |
23 | })) | 27 | })) |
24 | }); | 28 | }); |