Skip to content

Commit

Permalink
feat(api): add all the pages
Browse files Browse the repository at this point in the history
  • Loading branch information
i007c committed Jun 2, 2024
1 parent 542d60d commit a9cd191
Showing 1 changed file with 43 additions and 16 deletions.
59 changes: 43 additions & 16 deletions src/web.rs
Original file line number Diff line number Diff line change
@@ -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<HttpResponse, AppErr>;

#[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<Environment<'static>>) -> 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<Environment<'static>>) -> 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<Environment<'static>>) -> 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<Environment<'static>>) -> 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<Environment<'static>>) -> 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)
}

0 comments on commit a9cd191

Please sign in to comment.