4 use actix_web
::{middleware
, web
, App
, HttpServer
};
5 use chrono
::prelude
::*;
20 async
fn main() -> std
::io
::Result
<()> {
25 std
::env
::set_var("RUST_LOG", "info,actix_web=info");
28 println!("Starting Recipes as web server...");
30 let config
= web
::Data
::new(config
::load());
31 let port
= config
.as_ref().port
;
33 println!("Configuration: {:?}", config
);
35 let db_connection
= web
::Data
::new(db
::Connection
::new().unwrap());
37 let server
= HttpServer
::new(move || {
39 .wrap(middleware
::Logger
::default())
40 .wrap(middleware
::Compress
::default())
41 .app_data(db_connection
.clone())
42 .app_data(config
.clone())
43 .service(services
::home_page
)
44 .service(services
::sign_up_get
)
45 .service(services
::sign_up_post
)
46 .service(services
::sign_up_check_email
)
47 .service(services
::sign_up_validation
)
48 .service(services
::sign_in_get
)
49 .service(services
::sign_in_post
)
50 .service(services
::sign_out
)
51 .service(services
::view_recipe
)
52 .service(services
::edit_recipe
)
53 .service(fs
::Files
::new("/static", "static"))
54 .default_service(web
::to(services
::not_found
))
58 server
.bind(&format!("0.0.0.0:{}", port
))?
.run().await
61 #[derive(Parser, Debug)]
63 /// Will clear the database and insert some test data. (A backup is made first).
68 fn process_args() -> bool
{
69 let args
= Args
::parse();
72 // Make a backup of the database.
73 let db_path
= Path
::new(consts
::DB_DIRECTORY
).join(consts
::DB_FILENAME
);
75 let db_path_bckup
= (1..)
77 let p
= db_path
.with_extension(format!("sqlite.bckup{:03}", n
));
85 std
::fs
::copy(&db_path
, &db_path_bckup
).expect(&format!(
86 "Unable to make backup of {:?} to {:?}",
87 &db_path
, &db_path_bckup
89 std
::fs
::remove_file(&db_path
)
90 .expect(&format!("Unable to remove db file: {:?}", &db_path
));
93 match db
::Connection
::new() {
95 if let Err(error
) = con
.execute_file("sql/data_test.sql") {
96 eprintln!("{}", error
);
98 // Set the creation datetime to 'now'.
100 "UPDATE [User] SET [creation_datetime] = ?1 WHERE [email] = 'paul@test.org'",
106 eprintln!("{}", error
);