-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
43 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |