98bebc9ba1778bc33ff5e1e6a32839a6af7b006e
5 use actix_web
::{get
, web
, Responder
, middleware
, App
, HttpServer
, HttpRequest
};
6 use askama_actix
::Template
;
7 use chrono
::prelude
::*;
9 use ron
::de
::from_reader
;
10 use serde
::Deserialize
;
19 #[template(path = "home.html")]
21 recipes
: Vec
<(i32, String
)>,
25 #[template(path = "view_recipe.html")]
26 struct ViewRecipeTemplate
{
27 recipes
: Vec
<(i32, String
)>,
28 current_recipe
: model
::Recipe
,
31 #[derive(Deserialize)]
37 async
fn home_page(req
: HttpRequest
, connection
: web
::Data
<db
::Connection
>) -> impl Responder
{
38 HomeTemplate
{ recipes
: connection
.get_all_recipe_titles().unwrap() } // TODO: unwrap.
41 #[get("/recipe/view/{id}")]
42 async
fn view_recipe(req
: HttpRequest
, path
: web
::Path
<(i32,)>, connection
: web
::Data
<db
::Connection
>) -> impl Responder
{
44 recipes
: connection
.get_all_recipe_titles().unwrap(),
45 current_recipe
: connection
.get_recipe(path
.0).unwrap(),
49 #[derive(Debug, Deserialize)]
54 fn get_exe_name() -> String
{
55 let first_arg
= std
::env
::args().nth(0).unwrap();
56 let sep
: &[_
] = &['
\\'
, '
/'
];
57 first_arg
[first_arg
.rfind(sep
).unwrap()+1..].to_string()
61 async
fn main() -> std
::io
::Result
<()> {
62 if process_args() { return Ok(()) }
64 std
::env
::set_var("RUST_LOG", "actix_web=debug");
67 println!("Starting Recipes as web server...");
69 let config
: Config
= {
70 let f
= File
::open(consts
::FILE_CONF
).unwrap_or_else(|_
| panic!("Failed to open configuration file {}", consts
::FILE_CONF
));
71 match from_reader(f
) {
73 Err(e
) => panic!("Failed to load config: {}", e
)
77 println!("Configuration: {:?}", config
);
79 let db_connection
= web
::Data
::new(db
::Connection
::new().unwrap()); // TODO: remove unwrap.
81 std
::env
::set_var("RUST_LOG", "actix_web=info");
87 .wrap(middleware
::Logger
::default())
88 .wrap(middleware
::Compress
::default())
89 .app_data(db_connection
.clone())
92 .service(fs
::Files
::new("/static", "static").show_files_listing())
96 server
= server
.bind(&format!("0.0.0.0:{}", config
.port
)).unwrap();
101 #[derive(Parser, Debug)]
107 fn process_args() -> bool
{
108 let args
= Args
::parse();
111 match db
::Connection
::new() {
113 if let Err(error
) = con
.execute_file("sql/data_test.sql") {
114 println!("Error: {:?}", error
);
116 // Set the creation datetime to 'now'.
117 con
.execute_sql("UPDATE [User] SET [creation_datetime] = ?1 WHERE [email] = 'paul@test.org'", [Utc
::now()]).unwrap();
120 println!("Error: {:?}", error
)
134 println!(" {} [--help] [--test]", get_exe_name());
137 let args: Vec<String> = args().collect();
139 if args.iter().any(|arg| arg == "--help") {
142 } else if args.iter().any(|arg| arg == "--test") {
143 match db::Connection::new() {
145 Err(error) => println!("Error: {:?}", error)