daeed7afe7f280872d1b1c5a6d674bcf960d0f2d
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
{
42 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 } }
45 #[derive(Debug, Deserialize)]
50 fn get_exe_name() -> String
{
51 let first_arg
= std
::env
::args().nth(0).unwrap();
52 let sep
: &[_
] = &['
\\'
, '
/'
];
53 first_arg
[first_arg
.rfind(sep
).unwrap()+1..].to_string()
57 async
fn main() -> std
::io
::Result
<()> {
58 if process_args() { return Ok(()) }
60 println!("Starting Recipes as web server...");
62 let config
: Config
= {
63 let f
= File
::open(consts
::FILE_CONF
).unwrap_or_else(|_
| panic!("Failed to open configuration file {}", consts
::FILE_CONF
));
64 match from_reader(f
) {
66 Err(e
) => panic!("Failed to load config: {}", e
)
70 println!("Configuration: {:?}", config
);
72 // let database_connection = db::create_or_update();
74 std
::env
::set_var("RUST_LOG", "actix_web=info");
76 let mut listenfd
= ListenFd
::from_env();
81 .wrap(middleware
::Compress
::default())
84 .service(fs
::Files
::new("/static", "static").show_files_listing())
89 if let Some(l
) = listenfd
.take_tcp_listener(0).unwrap() {
90 server
.listen(l
).unwrap()
92 server
.bind(&format!("0.0.0.0:{}", config
.port
)).unwrap()
98 fn process_args() -> bool
{
101 println!(" {} [--help] [--test]", get_exe_name());
104 let args
: Vec
<String
> = args().collect();
106 if args
.iter().any(|arg
| arg
== "--help") {
109 } else if args
.iter().any(|arg
| arg
== "--test") {
110 match db
::Connection
::new() {
112 Err(error
) => println!("Error: {:?}", error
)