make tuts list page render properly

This commit is contained in:
Triston Armstrong 2024-03-26 09:25:37 -05:00
parent c2732fe85c
commit cd75b598bb
4 changed files with 21 additions and 17 deletions

View File

@ -13,24 +13,30 @@ pub fn init_notes_routes() -> Router {
pub fn update_route(router: Router, file_name_str: String, file_path_str: String) -> Router { pub fn update_route(router: Router, file_name_str: String, file_path_str: String) -> Router {
let route = format!("/{}", file_name_str); let route = format!("/{}", file_name_str);
println!("{}", route);
router.route( router.route(
route.clone().as_str(), route.clone().as_str(),
get(move || routes::tuts_builder(file_path_str)), get(move || routes::tuts_builder(file_path_str)),
) )
} }
// put all tutorial routes here // put all tutorial routes here
pub fn init_tuts_routes() -> Router { pub fn init_tuts_routes() -> Router {
let mut router = Router::new().route("/", get(routes::tuts)); let mut current_dir = env::current_dir().unwrap().into_os_string();
let path = env::current_dir().unwrap(); current_dir.push("/tuts");
let mut path_string = path.clone().into_os_string(); let directory = fs::read_dir(current_dir).unwrap();
path_string.push("/tuts"); let mut file_dir_vec = Vec::<String>::new();
for entry in fs::read_dir(path_string).unwrap() {
let entry = entry.unwrap(); let mut router = Router::new();
for file in directory {
let entry = file.unwrap();
let file_path = entry.path().to_str().unwrap().to_string(); let file_path = entry.path().to_str().unwrap().to_string();
let file_name = entry.file_name(); let file_name = entry.file_name();
let file_name_str = file_name.to_str().unwrap().to_string(); let file_name_str = file_name.to_str().unwrap().to_string();
file_dir_vec.push(file_name_str.clone());
router = update_route(router, file_name_str, file_path); router = update_route(router, file_name_str, file_path);
} }
router = router.route("/", get(move || routes::tuts(file_dir_vec)));
router router
} }

View File

@ -1,5 +1,3 @@
use std::{fs::File, io::Read};
use askama::Template; use askama::Template;
use axum::{ use axum::{
http::StatusCode, http::StatusCode,
@ -20,12 +18,8 @@ pub async fn notes() -> impl IntoResponse {
templates::Notes templates::Notes
} }
pub async fn tuts() -> impl IntoResponse { pub async fn tuts(paths: Vec<String>) -> impl IntoResponse {
let tuts_list = Vec::new(); templates::Tuts { tuts_list: paths }
templates::Tuts {
tuts_list: tuts_list,
}
} }
pub async fn tuts_builder(path: String) -> Result<impl IntoResponse, ApiError> { pub async fn tuts_builder(path: String) -> Result<impl IntoResponse, ApiError> {

View File

@ -10,8 +10,8 @@ pub struct Notes;
#[derive(Template)] #[derive(Template)]
#[template(path = "tuts.html")] #[template(path = "tuts.html")]
pub struct Tuts<'a> { pub struct Tuts {
pub tuts_list: Vec<&'a str>, pub tuts_list: Vec<String>,
} }
#[derive(Template)] #[derive(Template)]

View File

@ -8,5 +8,9 @@ content %}
<h1>Notes</h1> <h1>Notes</h1>
<p>Here all my Micro Tuts</p> <p>Here all my Micro Tuts</p>
{{tuts_list}} <ul>
{% for tut in tuts_list %}
<li><a href="tuts/{{tut}}">{{tut}}</a></li>
{% endfor %}
</ul>
{% endblock content%} {% endblock content%}