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 /license-generator/src | |
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.
Diffstat (limited to 'license-generator/src')
-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 |
4 files changed, 43 insertions, 4 deletions
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(()) } |