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