cargo fmt

This commit is contained in:
Gabriel Simmer 2023-07-19 21:04:25 +01:00
parent 3b849e6e6e
commit c2449dcaf1
Signed by: arch
GPG key ID: C81B106D46C5B875

View file

@ -77,27 +77,27 @@ async fn homepage() -> Markup {
} }
async fn list_blog_posts() -> Markup { async fn list_blog_posts() -> Markup {
let mut posts = Vec::new(); let mut posts = Vec::new();
for entry in fs::read_dir("./posts").unwrap() { for entry in fs::read_dir("./posts").unwrap() {
let entry = entry.unwrap(); let entry = entry.unwrap();
let path = entry.path(); let path = entry.path();
let filename = path.file_name().unwrap().to_str().unwrap(); let filename = path.file_name().unwrap().to_str().unwrap();
let ext = path.extension().unwrap().to_str().unwrap(); let ext = path.extension().unwrap().to_str().unwrap();
// strip extension // strip extension
let fname = filename.replace(&format!(".{}", ext), ""); let fname = filename.replace(&format!(".{}", ext), "");
if ext == "md" || ext == "org" { if ext == "md" || ext == "org" {
posts.push(fname); posts.push(fname);
} }
} }
html! { html! {
h1 { "Blog Posts" } h1 { "Blog Posts" }
ul { ul {
@for post in posts { @for post in posts {
li { a href=(format!("/blog/{}", post)) { (post) } } li { a href=(format!("/blog/{}", post)) { (post) } }
} }
} }
} }
} }
async fn blog_post(Path(post): Path<String>) -> Result<impl IntoResponse, StatusCode> { async fn blog_post(Path(post): Path<String>) -> Result<impl IntoResponse, StatusCode> {
@ -168,37 +168,37 @@ async fn cached_page<T>(
.fetch_one(&state.database) .fetch_one(&state.database)
.await; .await;
if let Ok(res) = res { if let Ok(res) = res {
let current_time = SystemTime::now() let current_time = SystemTime::now()
.duration_since(UNIX_EPOCH) .duration_since(UNIX_EPOCH)
.expect("SystemTime before UNIX EPOCH!"); .expect("SystemTime before UNIX EPOCH!");
// SQLite cache is valid for 10 minutes. // SQLite cache is valid for 10 minutes.
if current_time.as_secs() <= (content.cached as u64 + (10 * 60) as u64) { if current_time.as_secs() <= (content.cached as u64 + (10 * 60) as u64) {
let c = CachedPage { let c = CachedPage {
content_type: res.content_type, content_type: res.content_type,
content: res.content, content: res.content,
cached: SystemTime::now() cached: SystemTime::now()
.duration_since(UNIX_EPOCH) .duration_since(UNIX_EPOCH)
.expect("Failed to get current time") .expect("Failed to get current time")
.as_secs() .as_secs()
.try_into() .try_into()
.unwrap(), .unwrap(),
}; };
// Refresh our memory cache. // Refresh our memory cache.
data.insert(path, c.clone()); data.insert(path, c.clone());
return Response::builder() return Response::builder()
.header("content-type", c.content_type) .header("content-type", c.content_type)
.header("cache", "hit-sqlite") .header("cache", "hit-sqlite")
.status(StatusCode::OK) .status(StatusCode::OK)
.body(Full::from(c.content)) .body(Full::from(c.content))
.unwrap(); .unwrap();
} else { } else {
let cache_sqlite = sqlx::query("DELETE FROM cached WHERE route = $1") let cache_sqlite = sqlx::query("DELETE FROM cached WHERE route = $1")
.bind(&path) .bind(&path)
.execute(&state.database) .execute(&state.database)
.await; .await;
} }
} }
let res = next.run(request).await; let res = next.run(request).await;
@ -229,34 +229,33 @@ async fn cached_page<T>(
let content = String::from_utf8(res).unwrap(); let content = String::from_utf8(res).unwrap();
let cache = CachedPage { let cache = CachedPage {
content_type: String::from_str(contenttype).unwrap(), content_type: String::from_str(contenttype).unwrap(),
content, content,
cached: SystemTime::now() cached: SystemTime::now()
.duration_since(UNIX_EPOCH) .duration_since(UNIX_EPOCH)
.expect("Failed to get current time") .expect("Failed to get current time")
.as_secs() .as_secs()
.try_into() .try_into()
.unwrap(), .unwrap(),
}; };
data.insert( data.insert(path.clone(), cache.clone());
path.clone(),
cache.clone()
);
let cache_sqlite = sqlx::query("INSERT INTO cached (route, cached, content_type, content) VALUES ( $1, $2, $3, $4 )") let cache_sqlite = sqlx::query(
.bind(path) "INSERT INTO cached (route, cached, content_type, content) VALUES ( $1, $2, $3, $4 )",
.bind(cache.cached) )
.bind(cache.content_type) .bind(path)
.bind(cache.content) .bind(cache.cached)
.execute(&state.database) .bind(cache.content_type)
.await; .bind(cache.content)
.execute(&state.database)
.await;
match cache_sqlite { match cache_sqlite {
Ok(_) => println!("cached"), Ok(_) => println!("cached"),
Err(e) => println!("{}", e), Err(e) => println!("{}", e),
} }
Response::builder() Response::builder()
.header("content-type", contenttype) .header("content-type", contenttype)