Title
This commit is contained in:
parent
001a27c694
commit
fbf3d7640a
8 changed files with 110 additions and 38 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{cache, config::Config, repo::ReadRepoMetadataError, templates};
|
||||
use crate::{cache, config::Config, repo, templates};
|
||||
|
||||
use askama::Template;
|
||||
use log::error;
|
||||
|
|
@ -16,7 +16,7 @@ pub fn make_router(config: &'static Config) -> impl Handler {
|
|||
|
||||
let metadata_cache: &'static _ = Box::leak(Box::new(cache::Cache::<
|
||||
String,
|
||||
templates::Directory,
|
||||
(repo::RepoMetadata, templates::Directory),
|
||||
>::default()));
|
||||
let file_cache: &'static _ =
|
||||
Box::leak(Box::new(cache::Cache::<(String, String), String>::default()));
|
||||
|
|
@ -26,7 +26,6 @@ pub fn make_router(config: &'static Config) -> impl Handler {
|
|||
|
||||
(
|
||||
trillium_caching_headers::CachingHeaders::new(),
|
||||
//trillium_static_compiled::static_compiled!("./static").with_index_file("index.html"),
|
||||
Router::new()
|
||||
.get("/", |conn: Conn| async move {
|
||||
conn.ok(crate::templates::Home {}.render().unwrap())
|
||||
|
|
@ -34,10 +33,14 @@ pub fn make_router(config: &'static Config) -> impl Handler {
|
|||
.post("/fetch", move |mut conn: Conn| async move {
|
||||
if let Ok(request_body) = conn.request_body().with_max_len(8192).read_bytes().await
|
||||
{
|
||||
let mut title = String::new();
|
||||
let mut repo_url = None;
|
||||
let mut commit_hash = None;
|
||||
for (key, val) in form_urlencoded::parse(&request_body) {
|
||||
match key.as_ref() {
|
||||
"title" => {
|
||||
title = val.to_string();
|
||||
}
|
||||
"repo-url" => {
|
||||
repo_url = Some(val);
|
||||
}
|
||||
|
|
@ -61,6 +64,7 @@ pub fn make_router(config: &'static Config) -> impl Handler {
|
|||
)
|
||||
.await
|
||||
.expect("todo handle error");
|
||||
repo_metadata.title = title;
|
||||
crate::api_client::fetch_repo_files(
|
||||
config,
|
||||
&mut client,
|
||||
|
|
@ -71,7 +75,6 @@ pub fn make_router(config: &'static Config) -> impl Handler {
|
|||
.await
|
||||
.expect("todo handle error");
|
||||
}
|
||||
//let planet = conn.param("planet").unwrap();
|
||||
conn.ok(crate::templates::Home {}.render().unwrap())
|
||||
})
|
||||
.get("/r/:hash/*", move |conn: Conn| {
|
||||
|
|
@ -81,21 +84,14 @@ pub fn make_router(config: &'static Config) -> impl Handler {
|
|||
};
|
||||
|
||||
let fetch_metadata = |key| {
|
||||
//let mut repo_hash = [0; 24];
|
||||
//if dbg!(base64_turbo::URL_SAFE.decode_into(key, &mut repo_hash))
|
||||
// != Ok(16)
|
||||
//{
|
||||
// return None;
|
||||
//}
|
||||
//let repo_hash = &repo_hash[0..16];
|
||||
let repo_dir = PathBuf::from(&config.data_dir)
|
||||
.join(crate::SUBDIR_REPOS)
|
||||
.join(key);
|
||||
let repo_metadata =
|
||||
let mut repo_metadata =
|
||||
match crate::repo::RepoMetadata::read_from_file(config, &repo_dir) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
if let ReadRepoMetadataError::CannotOpenFile(e) = &e {
|
||||
if let repo::ReadRepoMetadataError::CannotOpenFile(e) = &e {
|
||||
if e.kind() == ErrorKind::NotFound {
|
||||
return None;
|
||||
}
|
||||
|
|
@ -119,6 +115,10 @@ pub fn make_router(config: &'static Config) -> impl Handler {
|
|||
name: name.into(),
|
||||
path: dbg!(entry.file_path).into(),
|
||||
hash: entry.hash.into(),
|
||||
link: format!(
|
||||
"/r/{repo_hash_str}/{0}",
|
||||
entry.file_path
|
||||
),
|
||||
}),
|
||||
path,
|
||||
);
|
||||
|
|
@ -128,9 +128,11 @@ pub fn make_router(config: &'static Config) -> impl Handler {
|
|||
}
|
||||
}
|
||||
}
|
||||
Some(root)
|
||||
// Save memory
|
||||
repo_metadata.content.clear();
|
||||
Some((repo_metadata, root))
|
||||
};
|
||||
let Some(root) =
|
||||
let Some((repo_metadata, root)) =
|
||||
metadata_cache.fetch(repo_hash_str.to_string(), fetch_metadata)
|
||||
else {
|
||||
return conn.with_status(404);
|
||||
|
|
@ -180,14 +182,16 @@ pub fn make_router(config: &'static Config) -> impl Handler {
|
|||
file_lang,
|
||||
giallo::ThemeVariant::Single("catppuccin-frappe"),
|
||||
);
|
||||
let highlighted = hl_registry.highlight(&file_content, &hl_options).unwrap_or_else(|_e| {
|
||||
// If extension is unknown
|
||||
let hl_options = giallo::HighlightOptions::new(
|
||||
giallo::PLAIN_GRAMMAR_NAME,
|
||||
giallo::ThemeVariant::Single("catppuccin-frappe"),
|
||||
);
|
||||
hl_registry.highlight(&file_content, &hl_options).unwrap()
|
||||
});
|
||||
let highlighted = hl_registry
|
||||
.highlight(&file_content, &hl_options)
|
||||
.unwrap_or_else(|_e| {
|
||||
// If extension is unknown
|
||||
let hl_options = giallo::HighlightOptions::new(
|
||||
giallo::PLAIN_GRAMMAR_NAME,
|
||||
giallo::ThemeVariant::Single("catppuccin-frappe"),
|
||||
);
|
||||
hl_registry.highlight(&file_content, &hl_options).unwrap()
|
||||
});
|
||||
let html = giallo::HtmlRenderer::default().render(
|
||||
&highlighted,
|
||||
&giallo::RenderOptions {
|
||||
|
|
@ -195,12 +199,13 @@ pub fn make_router(config: &'static Config) -> impl Handler {
|
|||
..Default::default()
|
||||
},
|
||||
);
|
||||
conn.ok(crate::templates::Repo {
|
||||
let template = crate::templates::Repo {
|
||||
content: html.clone(),
|
||||
root,
|
||||
}
|
||||
.render()
|
||||
.unwrap())
|
||||
path: conn.path().split('/').map(|s| s.to_string()).collect(),
|
||||
title: repo_metadata.title,
|
||||
};
|
||||
conn.ok(template.render().unwrap())
|
||||
}
|
||||
})
|
||||
.get("/e/:secret", |conn: Conn| async move {
|
||||
|
|
|
|||
Loading…
Reference in a new issue