From a9cd191e153b4a813706680ea111e20d6180ee72 Mon Sep 17 00:00:00 2001 From: 007 Date: Sun, 2 Jun 2024 20:38:52 +0330 Subject: [PATCH] feat(api): add all the pages --- src/web.rs | 59 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/src/web.rs b/src/web.rs index 0dca970..e7ba11f 100644 --- a/src/web.rs +++ b/src/web.rs @@ -1,29 +1,56 @@ use actix_web::dev::HttpServiceFactory; use actix_web::http::header::ContentType; use actix_web::middleware::NormalizePath; -use actix_web::{get, routes, HttpResponse, Scope}; -use std::fs::read_to_string; +use actix_web::web::Data; +use actix_web::{get, HttpResponse, Scope}; +use minijinja::{path_loader, Environment}; +use std::path::PathBuf; + +use crate::models::AppErr; + +type Response = Result; -#[routes] #[get("/")] -// #[get("/login")] -// #[get("/products")] -async fn app_index() -> HttpResponse { - let result = read_to_string("app/dist/index.html") - .unwrap_or("err reading app index.html".to_string()); - HttpResponse::Ok().content_type(ContentType::html()).body(result) +async fn home(env: Data>) -> Response { + let result = env.get_template("home/index.html")?.render(())?; + Ok(HttpResponse::Ok().content_type(ContentType::html()).body(result)) } -#[get("/admin")] -async fn admin_index() -> HttpResponse { - let result = read_to_string("admin/dist/index.html") - .unwrap_or("err reading admin index.html".to_string()); - HttpResponse::Ok().content_type(ContentType::html()).body(result) +#[get("/products")] +async fn products(env: Data>) -> Response { + let result = env.get_template("products/index.html")?.render(())?; + Ok(HttpResponse::Ok().content_type(ContentType::html()).body(result)) +} + +#[get("/contact")] +async fn contact(env: Data>) -> Response { + let result = env.get_template("contact/index.html")?.render(())?; + Ok(HttpResponse::Ok().content_type(ContentType::html()).body(result)) +} + +#[get("/about")] +async fn about(env: Data>) -> Response { + let result = env.get_template("about/index.html")?.render(())?; + Ok(HttpResponse::Ok().content_type(ContentType::html()).body(result)) +} + +#[get("/blogs")] +async fn blogs(env: Data>) -> Response { + let result = env.get_template("blogs/index.html")?.render(())?; + Ok(HttpResponse::Ok().content_type(ContentType::html()).body(result)) } pub fn router() -> impl HttpServiceFactory { + let tmpl_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("templates"); + let mut tmpl_env = Environment::new(); + tmpl_env.set_loader(path_loader(tmpl_path)); + Scope::new("") .wrap(NormalizePath::trim()) - .service(app_index) - .service(admin_index) + .app_data(Data::new(tmpl_env)) + .service(home) + .service(products) + .service(contact) + .service(about) + .service(blogs) }