wip render repo

This commit is contained in:
Pascal Engélibert 2026-03-29 12:00:57 +02:00
commit 1c0156f512
6 changed files with 203 additions and 56 deletions

View file

@ -1,9 +1,13 @@
use crate::{cache, config::Config, repo::ReadRepoMetadataError};
use crate::{cache, config::Config, repo::ReadRepoMetadataError, templates};
use askama::Template;
use async_lock::Mutex;
use log::error;
use std::{collections::HashMap, io::ErrorKind, path::PathBuf};
use std::{
collections::{BTreeMap, BTreeSet, HashMap},
io::ErrorKind,
path::PathBuf,
};
use trillium::{Conn, Handler};
use trillium_router::{Router, RouterConnExt};
@ -51,17 +55,24 @@ pub fn make_router(config: &'static Config) -> impl Handler {
};
let mut client = client.lock().await;
let repo_index = crate::api_client::fetch_repo_tree_index_at_commit(
let (repo_index, mut repo_metadata) =
crate::api_client::fetch_repo_tree_index_at_commit(
&mut client,
&repo_url,
&commit_hash,
None,
)
.await
.expect("todo handle error");
crate::api_client::fetch_repo_files(
config,
&mut client,
&repo_url,
&commit_hash,
&repo_index,
&mut repo_metadata,
None,
)
.await
.expect("todo handle error");
crate::api_client::fetch_repo_files(config, &mut client, &repo_index, None)
.await
.expect("todo handle error");
}
//let planet = conn.param("planet").unwrap();
conn.ok(crate::templates::Home {}.render().unwrap())
@ -74,9 +85,7 @@ pub fn make_router(config: &'static Config) -> impl Handler {
let cache_fetch = |key| {
let mut repo_hash = [0; 32];
if base64_turbo::URL_SAFE.decode_into(repo_hash_str, &mut repo_hash)
!= Ok(32)
{
if base64_turbo::URL_SAFE.decode_into(key, &mut repo_hash) != Ok(32) {
return None;
}
let repo_dir = PathBuf::from(&config.data_dir)
@ -95,11 +104,16 @@ pub fn make_router(config: &'static Config) -> impl Handler {
return None;
}
};
let mut files = HashMap::new();
for file in repo_metadata.iter_files() {
match file {
Ok(file) => {
files.insert(file.file_path.to_string(), file.hash.to_string());
let mut entries = templates::Directory::default();
for entry in repo_metadata.iter_files() {
match entry {
Ok(entry) => {
let Some(name) = entry.file_path.rsplit('/').next() else {
error!("Entry has no name");
continue;
};
let path = entry.file_path.split('/').peekable();
entries.insert(entry, path);
}
Err(e) => {
error!("Reading repo metadata file index: {e:?}")
@ -109,10 +123,13 @@ pub fn make_router(config: &'static Config) -> impl Handler {
Some(files)
};
// TODO replace mutex with better thing (less contention or async mutex)
metadata_cache
let Some(metadata) = metadata_cache
.lock()
.await
.fetch(repo_hash_str.to_string(), cache_fetch);
.fetch(repo_hash_str.to_string(), cache_fetch)
else {
return conn.with_status(404);
};
let hl_options = giallo::HighlightOptions::new(
"py",
@ -130,6 +147,7 @@ pub fn make_router(config: &'static Config) -> impl Handler {
);
conn.ok(crate::templates::Repo {
content: html.clone(),
entries: Vec::new(),
}
.render()
.unwrap())