5c6de4c462fa1504d06626686affc1cca2a28ee7
5 use actix_web
::{get
, web
, Responder
, middleware
, App
, HttpServer
, HttpRequest
};
6 use askama_actix
::Template
;
8 use ron
::de
::from_reader
;
9 use serde
::Deserialize
;
16 #[template(path = "home.html")]
18 recipes
: Vec
<(i32, String
)>,
22 #[template(path = "view_recipe.html")]
23 struct ViewRecipeTemplate
{
24 recipes
: Vec
<(i32, String
)>,
25 current_recipe
: model
::Recipe
,
28 #[derive(Deserialize)]
34 async
fn home_page(req
: HttpRequest
, connection
: web
::Data
<db
::Connection
>) -> impl Responder
{
35 HomeTemplate
{ recipes
: connection
.get_all_recipe_titles().unwrap() } // TODO: unwrap.
38 #[get("/recipe/view/{id}")]
39 async
fn view_recipe(req
: HttpRequest
, path
: web
::Path
<(i32,)>, connection
: web
::Data
<db
::Connection
>) -> impl Responder
{
41 recipes
: connection
.get_all_recipe_titles().unwrap(),
42 current_recipe
: connection
.get_recipe(path
.0).unwrap(),
46 #[derive(Debug, Deserialize)]
51 fn get_exe_name() -> String
{
52 let first_arg
= std
::env
::args().nth(0).unwrap();
53 let sep
: &[_
] = &['
\\'
, '
/'
];
54 first_arg
[first_arg
.rfind(sep
).unwrap()+1..].to_string()
58 async
fn main() -> std
::io
::Result
<()> {
59 if process_args() { return Ok(()) }
61 std
::env
::set_var("RUST_LOG", "actix_web=debug");
64 println!("Starting Recipes as web server...");
66 let config
: Config
= {
67 let f
= File
::open(consts
::FILE_CONF
).unwrap_or_else(|_
| panic!("Failed to open configuration file {}", consts
::FILE_CONF
));
68 match from_reader(f
) {
70 Err(e
) => panic!("Failed to load config: {}", e
)
74 println!("Configuration: {:?}", config
);
76 let db_connection
= web
::Data
::new(db
::Connection
::new().unwrap()); // TODO: remove unwrap.
78 std
::env
::set_var("RUST_LOG", "actix_web=info");
84 .wrap(middleware
::Logger
::default())
85 .wrap(middleware
::Compress
::default())
86 .app_data(db_connection
.clone())
89 .service(fs
::Files
::new("/static", "static").show_files_listing())
93 server
= server
.bind(&format!("0.0.0.0:{}", config
.port
)).unwrap();
98 #[derive(Parser, Debug)]
104 fn process_args() -> bool
{
105 let args
= Args
::parse();
108 if let Err(error
) = db
::Connection
::new() {
109 println!("Error: {:?}", error
)
121 println!(" {} [--help] [--test]", get_exe_name());
124 let args: Vec<String> = args().collect();
126 if args.iter().any(|arg| arg == "--help") {
129 } else if args.iter().any(|arg| arg == "--test") {
130 match db::Connection::new() {
132 Err(error) => println!("Error: {:?}", error)