Lot of thing

This commit is contained in:
Greg Burri 2020-05-20 17:25:56 +02:00
parent 3ebbe8172b
commit e2e54b8f43
12 changed files with 421 additions and 227 deletions

View file

@ -2,7 +2,7 @@ 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};
use actix_web::{get, web, Responder, middleware, App, HttpServer, HttpResponse, HttpRequest, web::Query};
use askama::Template;
use listenfd::ListenFd;
@ -15,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<db::Recipe>
}
#[derive(Template)]
#[template(path = "view_recipe.html")]
struct ViewRecipeTemplate {
recipes: Vec<db::Recipe>,
current_recipe: db::Recipe
}
#[derive(Deserialize)]
@ -25,12 +32,15 @@ pub struct Request {
m: Option<String>
}
fn main_page(query: Query<Request>) -> HttpResponse {
#[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 main_template = MainTemplate { test: &"*** test ***" };
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)]
@ -70,7 +80,8 @@ async fn main() -> std::io::Result<()> {
|| {
App::new()
.wrap(middleware::Compress::default())
.service(web::resource("/").to(main_page))
.service(home_page)
.service(view_page)
.service(fs::Files::new("/static", "static").show_files_listing())
}
);
@ -98,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
}