X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=backend%2Fsrc%2Fmain.rs;h=74e03833977cb1d4574171869e463b230746aec2;hb=cc2e5b6893b582b4b5c4e7a93e914a189f6a959b;hp=df7c44b85e033695c12a663a679c10600d3cab64;hpb=cbe276fc0601041b13087a6ffd80c5b126dfbe59;p=recipes.git diff --git a/backend/src/main.rs b/backend/src/main.rs index df7c44b..74e0383 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -1,24 +1,26 @@ +use std::path::Path; + use actix_files as fs; -use actix_web::{web, middleware, App, HttpServer}; +use actix_web::{middleware, web, App, HttpServer}; use chrono::prelude::*; use clap::Parser; -use log::error; use data::db; +mod config; mod consts; -mod utils; mod data; +mod email; mod hash; mod model; -mod user; -mod email; -mod config; mod services; +mod utils; #[actix_web::main] async fn main() -> std::io::Result<()> { - if process_args() { return Ok(()) } + if process_args() { + return Ok(()); + } std::env::set_var("RUST_LOG", "info,actix_web=info"); env_logger::init(); @@ -32,52 +34,77 @@ async fn main() -> std::io::Result<()> { let db_connection = web::Data::new(db::Connection::new().unwrap()); - let server = - HttpServer::new(move || { - App::new() - .wrap(middleware::Logger::default()) - .wrap(middleware::Compress::default()) - .app_data(db_connection.clone()) - .app_data(config.clone()) - .service(services::home_page) - .service(services::sign_up_get) - .service(services::sign_up_post) - .service(services::sign_up_check_email) - .service(services::sign_up_validation) - .service(services::sign_in_get) - .service(services::sign_in_post) - .service(services::sign_out) - .service(services::view_recipe) - .service(services::edit_recipe) - .service(fs::Files::new("/static", "static")) - .default_service(web::to(services::not_found)) - }); - //.workers(1); + let server = HttpServer::new(move || { + App::new() + .wrap(middleware::Logger::default()) + .wrap(middleware::Compress::default()) + .app_data(db_connection.clone()) + .app_data(config.clone()) + .service(services::home_page) + .service(services::sign_up_get) + .service(services::sign_up_post) + .service(services::sign_up_check_email) + .service(services::sign_up_validation) + .service(services::sign_in_get) + .service(services::sign_in_post) + .service(services::sign_out) + .service(services::view_recipe) + .service(services::edit_recipe) + .service(fs::Files::new("/static", "static")) + .default_service(web::to(services::not_found)) + }); + //.workers(1); server.bind(&format!("0.0.0.0:{}", port))?.run().await } #[derive(Parser, Debug)] struct Args { + /// Will clear the database and insert some test data. (A backup is made first). #[arg(long)] - dbtest: bool + dbtest: bool, } fn process_args() -> bool { let args = Args::parse(); if args.dbtest { + // Make a backup of the database. + let db_path = Path::new(consts::DB_DIRECTORY).join(consts::DB_FILENAME); + if db_path.exists() { + let db_path_bckup = (1..) + .find_map(|n| { + let p = db_path.with_extension(format!("sqlite.bckup{:03}", n)); + if p.exists() { + None + } else { + Some(p) + } + }) + .unwrap(); + std::fs::copy(&db_path, &db_path_bckup).expect(&format!( + "Unable to make backup of {:?} to {:?}", + &db_path, &db_path_bckup + )); + std::fs::remove_file(&db_path) + .expect(&format!("Unable to remove db file: {:?}", &db_path)); + } + match db::Connection::new() { Ok(con) => { if let Err(error) = con.execute_file("sql/data_test.sql") { eprintln!("{}", error); } // Set the creation datetime to 'now'. - con.execute_sql("UPDATE [User] SET [creation_datetime] = ?1 WHERE [email] = 'paul@test.org'", [Utc::now()]).unwrap(); - }, + con.execute_sql( + "UPDATE [User] SET [creation_datetime] = ?1 WHERE [email] = 'paul@test.org'", + [Utc::now()], + ) + .unwrap(); + } Err(error) => { eprintln!("{}", error); - }, + } } return true;