X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=backend%2Fsrc%2Fmain.rs;h=037650771f26c9fe8e2d0c2db0fbc7273a5961e8;hb=e2e54b8f43015738af860b005825dc809f5275ab;hp=299f5c55c36ac60b35416d327fe41b2d59e22175;hpb=eab43f8995eff5b8a4f6c4ded6a655866feddedb;p=recipes.git diff --git a/backend/src/main.rs b/backend/src/main.rs index 299f5c5..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::{get, web, Responder, middleware, App, HttpServer, HttpResponse, 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) -> HttpResponse { - - 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(); - 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)] @@ -49,7 +58,7 @@ fn get_exe_name() -> String { 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)); @@ -64,7 +73,6 @@ async 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 = @@ -72,9 +80,8 @@ async 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()) } ); @@ -102,7 +109,6 @@ fn process_args() -> bool { return true } else if args.iter().any(|arg| arg == "--test") { let db_connection = db::Connection::new(); - db_connection.create_or_update(); return true }