aboutsummaryrefslogtreecommitdiffstats
path: root/license-generator/src/main.rs
diff options
context:
space:
mode:
authorTeddy Wing2018-11-09 19:47:46 +0100
committerTeddy Wing2018-11-09 19:47:46 +0100
commit6aa6c70af607ede55f9dafef7278b192a2a6b9d0 (patch)
tree1afcc1fbe7479c8c2028768f84fce2f2658083dd /license-generator/src/main.rs
parente8aa1429fa45957a2174446df64b62d407544d8b (diff)
downloaddome-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/main.rs')
-rw-r--r--license-generator/src/main.rs37
1 files changed, 36 insertions, 1 deletions
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(())
}