mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-11 19:18:14 -07:00
general: update
This commit is contained in:
@@ -49,10 +49,6 @@ fn _dataset_handler(
|
||||
query: Query<Params>,
|
||||
AppState { routes }: AppState,
|
||||
) -> color_eyre::Result<Response> {
|
||||
if path.contains("favicon") {
|
||||
return Err(eyre!("Don't support favicon"));
|
||||
}
|
||||
|
||||
log(&format!(
|
||||
"{}{}",
|
||||
path,
|
||||
@@ -98,11 +94,13 @@ fn _dataset_handler(
|
||||
match kind {
|
||||
Kind::Date => {
|
||||
let datasets = DateMap::<usize>::_read_dir(&route.file_path, &route.serialization);
|
||||
|
||||
process_datasets(headers, kind, &mut chunk, &mut route, query, datasets)?;
|
||||
}
|
||||
Kind::Height => {
|
||||
let datasets =
|
||||
HeightMap::<usize>::_read_dir(&route.file_path, &route.serialization);
|
||||
|
||||
process_datasets(headers, kind, &mut chunk, &mut route, query, datasets)?;
|
||||
}
|
||||
_ => panic!(),
|
||||
@@ -159,7 +157,9 @@ where
|
||||
return Err(eyre!("Couldn't find chunk"));
|
||||
}
|
||||
|
||||
route.file_path = path.unwrap().to_str().unwrap().to_string();
|
||||
let path = path.unwrap();
|
||||
|
||||
route.file_path = path.to_str().unwrap().to_string();
|
||||
|
||||
let offset = match kind {
|
||||
Kind::Date => 1,
|
||||
|
||||
@@ -54,14 +54,14 @@ fn value_to_response<T>(value: T) -> Response
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
generic_to_reponse(value, None, 5)
|
||||
generic_to_reponse(value, None, 1)
|
||||
}
|
||||
|
||||
fn dataset_to_response<T>(dataset: T, chunk: Chunk) -> Response
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
generic_to_reponse(dataset, Some(chunk), 60)
|
||||
generic_to_reponse(dataset, Some(chunk), 5)
|
||||
}
|
||||
|
||||
pub fn generic_to_reponse<T>(generic: T, chunk: Option<Chunk>, cache_time: u64) -> Response
|
||||
@@ -71,7 +71,7 @@ where
|
||||
let mut response = {
|
||||
if let Some(chunk) = chunk {
|
||||
Json(WrappedDataset {
|
||||
source: "https://satonomics.xyz",
|
||||
source: "https://kibo.money",
|
||||
chunk,
|
||||
dataset: generic,
|
||||
})
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
use std::path::Path;
|
||||
|
||||
use axum::http::{header, HeaderMap};
|
||||
use chrono::{DateTime, Utc};
|
||||
use axum::{
|
||||
body::Body,
|
||||
http::{header, HeaderMap, Response},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use chrono::{DateTime, Timelike, Utc};
|
||||
use parser::log;
|
||||
use reqwest::header::HOST;
|
||||
use reqwest::{
|
||||
header::{HOST, IF_MODIFIED_SINCE},
|
||||
StatusCode,
|
||||
};
|
||||
|
||||
const STALE_IF_ERROR: u64 = 30_000_000; // 1 Year ish
|
||||
const MODIFIED_SINCE_FORMAT: &str = "%a, %d %b %Y %H:%M:%S GMT";
|
||||
|
||||
pub trait HeaderMapUtils {
|
||||
fn get_scheme(&self) -> &str;
|
||||
@@ -16,6 +24,12 @@ pub trait HeaderMapUtils {
|
||||
|
||||
fn insert_cors(&mut self);
|
||||
|
||||
fn get_if_modified_since(&self) -> Option<DateTime<Utc>>;
|
||||
fn check_if_modified_since(
|
||||
&self,
|
||||
path: &Path,
|
||||
) -> color_eyre::Result<(DateTime<Utc>, Option<Response<Body>>)>;
|
||||
|
||||
fn insert_cache_control_immutable(&mut self);
|
||||
fn insert_cache_control_revalidate(&mut self, max_age: u64, stale_while_revalidate: u64);
|
||||
fn insert_last_modified(&mut self, date: DateTime<Utc>);
|
||||
@@ -83,11 +97,49 @@ impl HeaderMapUtils for HeaderMap {
|
||||
}
|
||||
|
||||
fn insert_last_modified(&mut self, date: DateTime<Utc>) {
|
||||
let formatted = date.format("%a, %d %b %Y %H:%M:%S GMT").to_string();
|
||||
let formatted = date.format(MODIFIED_SINCE_FORMAT).to_string();
|
||||
|
||||
self.insert(header::LAST_MODIFIED, formatted.parse().unwrap());
|
||||
}
|
||||
|
||||
fn check_if_modified_since(
|
||||
&self,
|
||||
path: &Path,
|
||||
) -> color_eyre::Result<(DateTime<Utc>, Option<Response<Body>>)> {
|
||||
let time = path.metadata()?.modified()?;
|
||||
let date: DateTime<Utc> = time.into();
|
||||
let date = date.with_nanosecond(0).unwrap();
|
||||
let mut response_opt = None;
|
||||
|
||||
if let Some(if_modified_since) = self.get_if_modified_since() {
|
||||
if if_modified_since == date {
|
||||
let mut response = (StatusCode::NOT_MODIFIED, "").into_response();
|
||||
let headers = response.headers_mut();
|
||||
headers.insert_cors();
|
||||
response_opt.replace(response);
|
||||
}
|
||||
}
|
||||
|
||||
Ok((date, response_opt))
|
||||
}
|
||||
|
||||
fn get_if_modified_since(&self) -> Option<DateTime<Utc>> {
|
||||
if let Some(modified_since) = self.get(IF_MODIFIED_SINCE) {
|
||||
if let Ok(modified_since) = modified_since.to_str() {
|
||||
let date = DateTime::parse_from_str(
|
||||
&format!("{modified_since} +00:00"),
|
||||
&format!("{MODIFIED_SINCE_FORMAT} %z"),
|
||||
);
|
||||
|
||||
if let Ok(x) = date {
|
||||
return Some(x.to_utc());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
|
||||
fn insert_content_type(&mut self, path: &Path) {
|
||||
match path.extension().unwrap().to_str().unwrap() {
|
||||
|
||||
@@ -9,7 +9,6 @@ use axum::{
|
||||
http::HeaderMap,
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use chrono::{DateTime, Utc};
|
||||
use parser::log;
|
||||
use reqwest::StatusCode;
|
||||
|
||||
@@ -47,10 +46,13 @@ pub async fn index_handler(headers: HeaderMap) -> Response {
|
||||
}
|
||||
|
||||
fn path_to_response(headers: HeaderMap, path: &Path) -> Response {
|
||||
let mut response;
|
||||
let (date, response) = headers.check_if_modified_since(path).unwrap();
|
||||
|
||||
let time = path.metadata().unwrap().modified().unwrap();
|
||||
let date: DateTime<Utc> = time.into();
|
||||
if let Some(response) = response {
|
||||
return response;
|
||||
}
|
||||
|
||||
let mut response;
|
||||
|
||||
let is_localhost = headers.check_if_host_is_localhost();
|
||||
|
||||
@@ -76,7 +78,10 @@ fn path_to_response(headers: HeaderMap, path: &Path) -> Response {
|
||||
if !is_localhost {
|
||||
let serialized_path = path.to_str().unwrap();
|
||||
|
||||
if serialized_path.contains("fonts/") || serialized_path.contains("assets/pwa/") || serialized_path.contains("packages/") {
|
||||
if serialized_path.contains("fonts/")
|
||||
|| serialized_path.contains("assets/pwa/")
|
||||
|| serialized_path.contains("packages/")
|
||||
{
|
||||
headers.insert_cache_control_immutable();
|
||||
} else {
|
||||
headers.insert_cache_control_revalidate(10, 50);
|
||||
|
||||
Reference in New Issue
Block a user