X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=backend%2Fsrc%2Fmain.rs;h=037650771f26c9fe8e2d0c2db0fbc7273a5961e8;hb=e2e54b8f43015738af860b005825dc809f5275ab;hp=6d6b09aefb34a91a39de4818f2b24d8bbe68b486;hpb=ee48054a52b97e1d8bce3865b74462fd5c8a30d8;p=recipes.git diff --git a/backend/src/main.rs b/backend/src/main.rs index 6d6b09a..0376507 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -2,13 +2,12 @@ use std::io::prelude::*; use std::{fs::File, env::args}; use actix_files as fs; -use actix_web::{web, middleware, App, HttpServer, HttpResponse, Result, web::Query, middleware::Logger}; +use actix_web::{get, web, Responder, middleware, App, HttpServer, HttpResponse, HttpRequest, web::Query}; use askama::Template; use listenfd::ListenFd; use ron::de::from_reader; use serde::Deserialize; -use env_logger; use itertools::Itertools; @@ -16,9 +15,16 @@ mod consts; mod db; #[derive(Template)] -#[template(path = "main.html")] -struct MainTemplate<'a> { - test: &'a str, +#[template(path = "home.html")] +struct HomeTemplate { + recipes: Vec +} + +#[derive(Template)] +#[template(path = "view_recipe.html")] +struct ViewRecipeTemplate { + recipes: Vec, + current_recipe: db::Recipe } #[derive(Deserialize)] @@ -26,12 +32,15 @@ pub struct Request { m: Option } -fn main_page(query: Query) -> Result { - - let main_template = MainTemplate { test: &"*** test ***" }; +#[get("/")] +async fn home_page(req: HttpRequest) -> impl Responder { + 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 } ] } +} - let s = main_template.render().unwrap(); - Ok(HttpResponse::Ok().content_type("text/html").body(s)) +#[get("/recipe/view/{id}")] +async fn view_page(req: HttpRequest, path: web::Path<(i32,)>) -> impl Responder { + panic!("ERROR"); + 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 } } } #[derive(Debug, Deserialize)] @@ -45,10 +54,11 @@ fn get_exe_name() -> String { first_arg[first_arg.rfind(sep).unwrap()+1..].to_string() } -fn main() -> std::io::Result<()> { +#[actix_rt::main] +async fn main() -> std::io::Result<()> { if process_args() { return Ok(()) } - println!("Starting RUP as web server..."); + println!("Starting Recipes as web server..."); let config: Config = { let f = File::open(consts::FILE_CONF).unwrap_or_else(|_| panic!("Failed to open configuration file {}", consts::FILE_CONF)); @@ -63,7 +73,6 @@ fn main() -> std::io::Result<()> { // let database_connection = db::create_or_update(); std::env::set_var("RUST_LOG", "actix_web=info"); - env_logger::init(); let mut listenfd = ListenFd::from_env(); let mut server = @@ -71,9 +80,8 @@ fn main() -> std::io::Result<()> { || { App::new() .wrap(middleware::Compress::default()) - .wrap(Logger::default()) - .wrap(Logger::new("%a %{User-Agent}i")) - .service(web::resource("/").to(main_page)) + .service(home_page) + .service(view_page) .service(fs::Files::new("/static", "static").show_files_listing()) } ); @@ -85,7 +93,7 @@ fn main() -> std::io::Result<()> { server.bind(&format!("0.0.0.0:{}", config.port)).unwrap() }; - server.run() + server.run().await } fn process_args() -> bool { @@ -100,7 +108,7 @@ fn process_args() -> bool { print_usage(); return true } else if args.iter().any(|arg| arg == "--test") { - let database_connection = db::create_or_update(); + let db_connection = db::Connection::new(); return true }