Beginning of frontend + recipe editing
[recipes.git] / backend / src / main.rs
1 use actix_files as fs;
2 use actix_web::{web, middleware, App, HttpServer};
3 use chrono::prelude::*;
4 use clap::Parser;
5 use log::error;
6
7 use data::db;
8
9 mod consts;
10 mod utils;
11 mod data;
12 mod hash;
13 mod model;
14 mod user;
15 mod email;
16 mod config;
17 mod services;
18
19 #[actix_web::main]
20 async fn main() -> std::io::Result<()> {
21 if process_args() { return Ok(()) }
22
23 std::env::set_var("RUST_LOG", "info,actix_web=info");
24 env_logger::init();
25
26 println!("Starting Recipes as web server...");
27
28 let config = web::Data::new(config::load());
29 let port = config.as_ref().port;
30
31 println!("Configuration: {:?}", config);
32
33 let db_connection = web::Data::new(db::Connection::new().unwrap());
34
35 let server =
36 HttpServer::new(move || {
37 App::new()
38 .wrap(middleware::Logger::default())
39 .wrap(middleware::Compress::default())
40 .app_data(db_connection.clone())
41 .app_data(config.clone())
42 .service(services::home_page)
43 .service(services::sign_up_get)
44 .service(services::sign_up_post)
45 .service(services::sign_up_check_email)
46 .service(services::sign_up_validation)
47 .service(services::sign_in_get)
48 .service(services::sign_in_post)
49 .service(services::sign_out)
50 .service(services::view_recipe)
51 .service(services::edit_recipe)
52 .service(fs::Files::new("/static", "static"))
53 .default_service(web::to(services::not_found))
54 });
55 //.workers(1);
56
57 server.bind(&format!("0.0.0.0:{}", port))?.run().await
58 }
59
60 #[derive(Parser, Debug)]
61 struct Args {
62 #[arg(long)]
63 dbtest: bool
64 }
65
66 fn process_args() -> bool {
67 let args = Args::parse();
68
69 if args.dbtest {
70 match db::Connection::new() {
71 Ok(con) => {
72 if let Err(error) = con.execute_file("sql/data_test.sql") {
73 eprintln!("{}", error);
74 }
75 // Set the creation datetime to 'now'.
76 con.execute_sql("UPDATE [User] SET [creation_datetime] = ?1 WHERE [email] = 'paul@test.org'", [Utc::now()]).unwrap();
77 },
78 Err(error) => {
79 eprintln!("{}", error);
80 },
81 }
82
83 return true;
84 }
85
86 false
87 }