diff options
author | Joris | 2021-01-03 13:40:40 +0100 |
---|---|---|
committer | Joris | 2021-01-03 13:54:20 +0100 |
commit | 11052951b74b9ad4b6a9412ae490086235f9154b (patch) | |
tree | 64526ac926c1bf470ea113f6cac8a33158684e8d /src/main.rs | |
parent | 371449b0e312a03162b78797b83dee9d81706669 (diff) |
Rewrite in Rust
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..502f608 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,88 @@ +use hyper::service::{make_service_fn, service_fn}; +use hyper::Server; +use sqlx::sqlite::SqlitePool; +use std::convert::Infallible; +use std::net::SocketAddr; +use structopt::StructOpt; + +#[macro_use] +extern crate log; + +mod assets; +mod controller; +mod db; +mod jobs; +mod mail; +mod model; +mod payer; +mod queries; +mod routes; +mod templates; +mod utils; +mod validation; + +use model::config::Config; + +#[derive(StructOpt)] +#[structopt()] +struct Opt { + #[structopt(short, long, default_value = "127.0.0.1:3000")] + address: SocketAddr, + + #[structopt(short, long, default_value = "config.json")] + config: String, + + #[structopt(short, long, default_value = "database.db")] + database: String, +} + +#[tokio::main] +async fn main() { + env_logger::Builder::from_env( + env_logger::Env::default().default_filter_or("warn"), + ) + .init(); + + let opt = Opt::from_args(); + + let config_str = std::fs::read_to_string(&opt.config) + .expect(&format!("Missing {}", opt.config)); + let config: Config = serde_json::from_str(&config_str).unwrap(); + + let pool = SqlitePool::connect(&format!("sqlite:{}", opt.database)) + .await + .unwrap(); + + let assets = assets::get(); + + let templates = templates::get(); + + tokio::spawn(jobs::jobs::start( + config.clone(), + pool.clone(), + templates.clone(), + )); + + let make_svc = make_service_fn(|_conn| { + let config = config.clone(); + let pool = pool.clone(); + let assets = assets.clone(); + let templates = templates.clone(); + async move { + Ok::<_, Infallible>(service_fn(move |req| { + routes::routes( + config.clone(), + pool.clone(), + assets.clone(), + templates.clone(), + req, + ) + })) + } + }); + + info!("Starting server at {}", opt.address); + if let Err(e) = Server::bind(&opt.address).serve(make_svc).await { + error!("server error: {}", e); + } +} |