X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=backend%2Fsrc%2Fmain.rs;h=5c6de4c462fa1504d06626686affc1cca2a28ee7;hb=cdb883c3c4ccbb82774ecfbfad059f3392e75432;hp=fcb8e1284c93395cc9dfb6ab69c733f5c6ec439a;hpb=87bc628b563f61185c4f9ec48627029b17d32bba;p=recipes.git diff --git a/backend/src/main.rs b/backend/src/main.rs index fcb8e12..5c6de4c 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -1,26 +1,28 @@ -extern crate actix_web; -extern crate listenfd; -extern crate askama; +use std::fs::File; +use std::sync::Mutex; -use listenfd::ListenFd; use actix_files as fs; -use actix_web::{web, middleware, App, HttpServer, HttpResponse, Result, web::Query}; -use askama::Template; - -use std::io::prelude::*; +use actix_web::{get, web, Responder, middleware, App, HttpServer, HttpRequest}; +use askama_actix::Template; +use clap::Parser; use ron::de::from_reader; use serde::Deserialize; -use std::{fs::File, env::args}; - -use itertools::Itertools; mod consts; +mod model; mod db; #[derive(Template)] -#[template(path = "main.html")] -struct MainTemplate<'a> { - test: &'a str, +#[template(path = "home.html")] +struct HomeTemplate { + recipes: Vec<(i32, String)>, +} + +#[derive(Template)] +#[template(path = "view_recipe.html")] +struct ViewRecipeTemplate { + recipes: Vec<(i32, String)>, + current_recipe: model::Recipe, } #[derive(Deserialize)] @@ -28,12 +30,17 @@ pub struct Request { m: Option } -fn main_page(query: Query) -> Result { - - let main_template = MainTemplate { test: &"test" }; +#[get("/")] +async fn home_page(req: HttpRequest, connection: web::Data) -> impl Responder { + HomeTemplate { recipes: connection.get_all_recipe_titles().unwrap() } // TODO: unwrap. +} - let s = main_template.render().unwrap(); - Ok(HttpResponse::Ok().content_type("text/html").body(s)) +#[get("/recipe/view/{id}")] +async fn view_recipe(req: HttpRequest, path: web::Path<(i32,)>, connection: web::Data) -> impl Responder { + ViewRecipeTemplate { + recipes: connection.get_all_recipe_titles().unwrap(), + current_recipe: connection.get_recipe(path.0).unwrap(), + } } #[derive(Debug, Deserialize)] @@ -47,10 +54,14 @@ fn get_exe_name() -> String { first_arg[first_arg.rfind(sep).unwrap()+1..].to_string() } -fn main() -> std::io::Result<()> { +#[actix_web::main] +async fn main() -> std::io::Result<()> { if process_args() { return Ok(()) } - println!("Starting RUP as web server..."); + std::env::set_var("RUST_LOG", "actix_web=debug"); + env_logger::init(); + + 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)); @@ -62,31 +73,49 @@ fn main() -> std::io::Result<()> { println!("Configuration: {:?}", config); - let database_connection = db::create_or_update(); + let db_connection = web::Data::new(db::Connection::new().unwrap()); // TODO: remove unwrap. + + std::env::set_var("RUST_LOG", "actix_web=info"); - let mut listenfd = ListenFd::from_env(); let mut server = HttpServer::new( - || { + move || { App::new() - .wrap(middleware::Compress::default()) .wrap(middleware::Logger::default()) - .service(web::resource("/").to(main_page)) + .wrap(middleware::Compress::default()) + .app_data(db_connection.clone()) + .service(home_page) + .service(view_recipe) .service(fs::Files::new("/static", "static").show_files_listing()) } ); - server = - if let Some(l) = listenfd.take_tcp_listener(0).unwrap() { - server.listen(l).unwrap() - } else { - server.bind(&format!("0.0.0.0:{}", config.port)).unwrap() - }; + server = server.bind(&format!("0.0.0.0:{}", config.port)).unwrap(); - server.run() + server.run().await +} + +#[derive(Parser, Debug)] +struct Args { + #[arg(long)] + test: bool } fn process_args() -> bool { + let args = Args::parse(); + + if args.test { + if let Err(error) = db::Connection::new() { + println!("Error: {:?}", error) + } + return true; + } + + false + + /* + + fn print_usage() { println!("Usage:"); println!(" {} [--help] [--test]", get_exe_name()); @@ -98,9 +127,12 @@ fn process_args() -> bool { print_usage(); return true } else if args.iter().any(|arg| arg == "--test") { - let database_connection = db::create_or_update(); + match db::Connection::new() { + Ok(_) => (), + Err(error) => println!("Error: {:?}", error) + } return true } - false + */ }