diff options
author | Teddy Wing | 2018-11-09 19:47:46 +0100 |
---|---|---|
committer | Teddy Wing | 2018-11-09 19:47:46 +0100 |
commit | 6aa6c70af607ede55f9dafef7278b192a2a6b9d0 (patch) | |
tree | 1afcc1fbe7479c8c2028768f84fce2f2658083dd | |
parent | e8aa1429fa45957a2174446df64b62d407544d8b (diff) | |
download | dome-key-web-6aa6c70af607ede55f9dafef7278b192a2a6b9d0.tar.bz2 |
Set up a file logger
Get a file path from the `LOG_FILE` environment variable and use it for
log output.
Call `database::get_database_connection()` and log the error if it
fails.
Worried about exiting early from the FastCGI program, as DreamHost says
this causes problems
(https://help.dreamhost.com/hc/en-us/articles/217298967). But I don't
see how the program can continue without a database connection.
Return a `Result` from `main()` because it's easier to use the `?`
operator for errors that happen before logging is initialised.
-rw-r--r-- | license-generator/Cargo.lock | 32 | ||||
-rw-r--r-- | license-generator/Cargo.toml | 2 | ||||
-rw-r--r-- | license-generator/src/database.rs | 2 | ||||
-rw-r--r-- | license-generator/src/errors.rs | 3 | ||||
-rw-r--r-- | license-generator/src/lib.rs | 5 | ||||
-rw-r--r-- | license-generator/src/main.rs | 37 |
6 files changed, 77 insertions, 4 deletions
diff --git a/license-generator/Cargo.lock b/license-generator/Cargo.lock index 9510eb7..09cad51 100644 --- a/license-generator/Cargo.lock +++ b/license-generator/Cargo.lock @@ -284,9 +284,19 @@ version = "0.0.1" dependencies = [ "error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "fastcgi 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "mysql 14.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", "sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "simplelog 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "log" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -615,6 +625,16 @@ dependencies = [ ] [[package]] +name = "simplelog" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "term 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "smallvec" version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -633,6 +653,15 @@ dependencies = [ ] [[package]] +name = "term" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "thread_local" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -808,6 +837,7 @@ dependencies = [ "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1" "checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d" +"checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6" "checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" "checksum memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a3eb002f0535929f1199681417029ebea04aadc0c7a4224b46be99c7f5d6a16" "checksum miniz-sys 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0300eafb20369952951699b68243ab4334f4b10a88f411c221d444b36c40e649" @@ -842,8 +872,10 @@ dependencies = [ "checksum serde_json 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)" = "43344e7ce05d0d8280c5940cabb4964bea626aa58b1ec0e8c73fa2a8512a38ce" "checksum sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" "checksum sha2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9eb6be24e4c23a84d7184280d2722f7f2731fcdd4a9d886efbfe4413e4847ea0" +"checksum simplelog 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e95345f185d5adeb8ec93459d2dc99654e294cc6ccf5b75414d8ea262de9a13" "checksum smallvec 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "153ffa32fd170e9944f7e0838edf824a754ec4c1fc64746fcc9fe1f8fa602e5d" "checksum syn 0.15.18 (registry+https://github.com/rust-lang/crates.io-index)" = "90c39a061e2f412a9f869540471ab679e85e50c6b05604daf28bc3060f75c430" +"checksum term 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5e6b677dd1e8214ea1ef4297f85dbcbed8e8cdddb561040cc998ca2551c37561" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "d825be0eb33fda1a7e68012d51e9c7f451dc1a69391e7fdc197060bb8c56667b" "checksum twox-hash 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4f85be565a110ed72ed7048cf56570db04ce0a592c98aa59b7dacde3e5718750" diff --git a/license-generator/Cargo.toml b/license-generator/Cargo.toml index 89b54fa..472f585 100644 --- a/license-generator/Cargo.toml +++ b/license-generator/Cargo.toml @@ -5,9 +5,11 @@ version = "0.0.1" [dependencies] error-chain = "0.12.0" fastcgi = "1.0.0" +log = "0.4.6" mysql = "14.1.1" rand = "0.5.5" sha1 = { version = "0.6.0", features = ["std"] } +simplelog = "0.5.3" [workspace] members = [ diff --git a/license-generator/src/database.rs b/license-generator/src/database.rs index 28673c4..94e925f 100644 --- a/license-generator/src/database.rs +++ b/license-generator/src/database.rs @@ -4,7 +4,7 @@ use mysql; use errors::*; -fn get_database_connection() -> Result<mysql::PooledConn> { +pub fn get_database_connection() -> Result<mysql::PooledConn> { let connection_url = env::var("DATABASE_URL")?; let pool = mysql::Pool::new_manual(10, 50, connection_url)?; let cx = pool.get_conn()?; diff --git a/license-generator/src/errors.rs b/license-generator/src/errors.rs index 29f25ca..0a28341 100644 --- a/license-generator/src/errors.rs +++ b/license-generator/src/errors.rs @@ -1,9 +1,12 @@ +use log; use mysql; error_chain! { foreign_links { EnvVar(::std::env::VarError); + Io(::std::io::Error); + Log(log::SetLoggerError); MySql(mysql::error::Error); } } diff --git a/license-generator/src/lib.rs b/license-generator/src/lib.rs index e129d17..4dd726d 100644 --- a/license-generator/src/lib.rs +++ b/license-generator/src/lib.rs @@ -1,10 +1,11 @@ #[macro_use] extern crate error_chain; +extern crate log; extern crate mysql; extern crate rand; extern crate sha1; -mod database; -mod errors; +pub mod database; +pub mod errors; mod purchaser; diff --git a/license-generator/src/main.rs b/license-generator/src/main.rs index fab9eb1..32fdc3b 100644 --- a/license-generator/src/main.rs +++ b/license-generator/src/main.rs @@ -1,8 +1,41 @@ extern crate fastcgi; +#[macro_use] +extern crate log; +extern crate mysql; +extern crate simplelog; + +extern crate license_generator; + +use std::env; +use std::fs::OpenOptions; use std::io::{Read, Write}; -fn main() { +use simplelog::{Config, LevelFilter, WriteLogger}; + +use license_generator::database; +use license_generator::errors::*; + +fn main() -> Result<()> { + let log_file_path = env::var("LOG_FILE")?; + + let log_file = OpenOptions::new() + .append(true) + .create(true) + .open(log_file_path)?; + + WriteLogger::init(LevelFilter::Info, Config::default(), log_file)?; + + let cx = match database::get_database_connection() + .chain_err(|| "failed to create a database connection") + { + Ok(cx) => cx, + Err(e) => { + error!("{}", e); + return Err(e); + }, + }; + fastcgi::run(|mut req| { write!(&mut req.stdout(), "Content-Type: text/plain\n\nHello, world!") .unwrap_or(()); @@ -21,4 +54,6 @@ fn main() { write!(&mut req.stdout(), "\n\nstdin: {}\n", stdin) .unwrap_or(()); }); + + Ok(()) } |