1 use std
::io
::prelude
::*;
2 use std
::{fs
::File
, env
::args
};
5 use actix_web
::{get
, web
, Responder
, middleware
, App
, HttpServer
, HttpResponse
, HttpRequest
, web
::Query
};
8 use listenfd
::ListenFd
;
9 use ron
::de
::from_reader
;
10 use serde
::Deserialize
;
12 use itertools
::Itertools
;
18 #[template(path = "home.html")]
20 recipes
: Vec
<db
::Recipe
>
24 #[template(path = "view_recipe.html")]
25 struct ViewRecipeTemplate
{
26 recipes
: Vec
<db
::Recipe
>,
27 current_recipe
: db
::Recipe
30 #[derive(Deserialize)]
36 async
fn home_page(req
: HttpRequest
) -> impl Responder
{
37 HomeTemplate
{ recipes
: vec
![ db
::Recipe
{ title
: String
::from("Saumon en croûte feuilletée"), id
: 1 }, db
::Recipe
{ title
: String
::from("Croissant au jambon"), id
: 2 } ] }
40 #[get("/recipe/view/{id}")]
41 async
fn view_page(req
: HttpRequest
, path
: web
::Path
<(i32,)>) -> impl Responder
{
43 ViewRecipeTemplate
{ recipes
: vec
![ db
::Recipe
{ title
: String
::from("Saumon en croûte feuilletée"), id
: 1 }, db
::Recipe
{ title
: String
::from("Croissant au jambon"), id
: 2 } ], current_recipe
: db
::Recipe
{ title
: String
::from("Saumon en croûte feuilletée"), id
: 1 } }
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 println!("Starting Recipes as web server...");
63 let config
: Config
= {
64 let f
= File
::open(consts
::FILE_CONF
).unwrap_or_else(|_
| panic!("Failed to open configuration file {}", consts
::FILE_CONF
));
65 match from_reader(f
) {
67 Err(e
) => panic!("Failed to load config: {}", e
)
71 println!("Configuration: {:?}", config
);
73 // let database_connection = db::create_or_update();
75 std
::env
::set_var("RUST_LOG", "actix_web=info");
77 let mut listenfd
= ListenFd
::from_env();
82 .wrap(middleware
::Compress
::default())
85 .service(fs
::Files
::new("/static", "static").show_files_listing())
90 if let Some(l
) = listenfd
.take_tcp_listener(0).unwrap() {
91 server
.listen(l
).unwrap()
93 server
.bind(&format!("0.0.0.0:{}", config
.port
)).unwrap()
99 fn process_args() -> bool
{
102 println!(" {} [--help] [--test]", get_exe_name());
105 let args
: Vec
<String
> = args().collect();
107 if args
.iter().any(|arg
| arg
== "--help") {
110 } else if args
.iter().any(|arg
| arg
== "--test") {
111 let db_connection
= db
::Connection
::new();