Readme, Box leak, async mutex, repo URL parsing
This commit is contained in:
parent
da6fb1c3c8
commit
44c310ac11
8 changed files with 103 additions and 32 deletions
|
|
@ -1,13 +1,9 @@
|
|||
use crate::{cache, config::Config, repo::ReadRepoMetadataError};
|
||||
|
||||
use askama::Template;
|
||||
use async_lock::Mutex;
|
||||
use log::error;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
io::ErrorKind,
|
||||
path::PathBuf,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use std::{collections::HashMap, io::ErrorKind, path::PathBuf};
|
||||
use trillium::{Conn, Handler};
|
||||
use trillium_router::{Router, RouterConnExt};
|
||||
|
||||
|
|
@ -15,14 +11,18 @@ pub async fn hello_world(conn: Conn) -> Conn {
|
|||
conn.ok("hello world!")
|
||||
}
|
||||
|
||||
pub fn make_router(config: Arc<Config>) -> impl Handler {
|
||||
pub fn make_router(config: &'static Config) -> impl Handler {
|
||||
let mut hl_registry = giallo::Registry::builtin().unwrap();
|
||||
hl_registry.link_grammars();
|
||||
let hl_registry = Arc::new(hl_registry);
|
||||
let hl_registry: &'static _ = Box::leak(Box::new(hl_registry));
|
||||
|
||||
let mut metadata_cache = Arc::new(Mutex::new(
|
||||
cache::Cache::<String, HashMap<String, String>>::default(),
|
||||
));
|
||||
let metadata_cache: &'static _ = Box::leak(Box::new(Mutex::new(cache::Cache::<
|
||||
String,
|
||||
HashMap<String, String>,
|
||||
>::default())));
|
||||
let client: &'static _ = Box::leak(Box::new(async_lock::Mutex::new(
|
||||
crate::api_client::make_client(),
|
||||
)));
|
||||
|
||||
(
|
||||
trillium_caching_headers::CachingHeaders::new(),
|
||||
|
|
@ -31,25 +31,42 @@ pub fn make_router(config: Arc<Config>) -> impl Handler {
|
|||
.get("/", |conn: Conn| async move {
|
||||
conn.ok(crate::templates::Home {}.render().unwrap())
|
||||
})
|
||||
.post("/fetch", |mut conn: Conn| async move {
|
||||
.post("/fetch", move |mut conn: Conn| async move {
|
||||
if let Ok(request_body) = conn.request_body().await.with_max_len(8192).await {
|
||||
let mut repo_url = None;
|
||||
let mut commit_hash = None;
|
||||
for (key, val) in form_urlencoded::parse(request_body.as_bytes()) {
|
||||
if key == "repo-url" {
|
||||
repo_url = Some(val);
|
||||
match key.as_ref() {
|
||||
"repo-url" => {
|
||||
repo_url = Some(val);
|
||||
}
|
||||
"commit" => {
|
||||
commit_hash = Some(val);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let Some(repo_url) = repo_url else {
|
||||
let (Some(repo_url), Some(commit_hash)) = (repo_url, commit_hash) else {
|
||||
return conn.ok("Missing arg");
|
||||
};
|
||||
|
||||
let mut client = client.lock().await;
|
||||
let repo_index = 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_index, None)
|
||||
.await
|
||||
.expect("todo handle error");
|
||||
}
|
||||
//let planet = conn.param("planet").unwrap();
|
||||
conn.ok(crate::templates::Home {}.render().unwrap())
|
||||
})
|
||||
.get("/r/:hash/*", move |conn: Conn| {
|
||||
let hl_registry = hl_registry.clone();
|
||||
let config = config.clone();
|
||||
let metadata_cache = metadata_cache.clone();
|
||||
async move {
|
||||
let Some(repo_hash_str) = conn.param("hash") else {
|
||||
return conn.with_status(401);
|
||||
|
|
@ -66,7 +83,7 @@ pub fn make_router(config: Arc<Config>) -> impl Handler {
|
|||
.join(crate::SUBDIR_REPOS)
|
||||
.join(repo_hash_str);
|
||||
let repo_metadata =
|
||||
match crate::repo::RepoMetadata::read_from_file(&config, &repo_dir) {
|
||||
match crate::repo::RepoMetadata::read_from_file(config, &repo_dir) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
if let ReadRepoMetadataError::CannotOpenFile(e) = &e {
|
||||
|
|
@ -94,7 +111,7 @@ pub fn make_router(config: Arc<Config>) -> impl Handler {
|
|||
// TODO replace mutex with better thing (less contention or async mutex)
|
||||
metadata_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.await
|
||||
.fetch(repo_hash_str.to_string(), cache_fetch);
|
||||
|
||||
let hl_options = giallo::HighlightOptions::new(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue