mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-28 19:28:11 -07:00
global: snapshot
This commit is contained in:
@@ -6,7 +6,7 @@ use axum::{
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use color_eyre::{eyre::eyre, owo_colors::OwoColorize};
|
||||
use reqwest::{header::HOST, StatusCode};
|
||||
use reqwest::StatusCode;
|
||||
use serde::Deserialize;
|
||||
|
||||
use parser::{log, Date, DateMap, Height, HeightMap, MapChunkId, HEIGHT_MAP_CHUNK_SIZE, OHLC};
|
||||
@@ -14,10 +14,11 @@ use parser::{log, Date, DateMap, Height, HeightMap, MapChunkId, HEIGHT_MAP_CHUNK
|
||||
use crate::{
|
||||
api::structs::{Chunk, Kind, Route},
|
||||
header_map::HeaderMapUtils,
|
||||
response::typed_value_to_response,
|
||||
AppState,
|
||||
};
|
||||
|
||||
use super::response::typed_value_to_response;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Params {
|
||||
chunk: Option<usize>,
|
||||
@@ -168,13 +169,8 @@ where
|
||||
|
||||
let offsetted_to_url = |offseted| {
|
||||
datasets.get(&ChunkId::from_usize(offseted)).map(|_| {
|
||||
let host = headers[HOST].to_str().unwrap();
|
||||
let scheme = if host.contains("0.0.0.0") || host.contains("localhost") {
|
||||
"http"
|
||||
} else {
|
||||
"https"
|
||||
};
|
||||
|
||||
let scheme = headers.get_scheme();
|
||||
let host = headers.get_host();
|
||||
format!("{scheme}://{host}{}?chunk={offseted}", route.url_path)
|
||||
})
|
||||
};
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
use axum::{extract::State, http::HeaderMap, response::Response};
|
||||
use reqwest::header::HOST;
|
||||
|
||||
use crate::{response::generic_to_reponse, AppState};
|
||||
use crate::AppState;
|
||||
|
||||
use super::response::generic_to_reponse;
|
||||
|
||||
pub async fn fallback(headers: HeaderMap, State(app_state): State<AppState>) -> Response {
|
||||
generic_to_reponse(
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
mod dataset;
|
||||
mod fallback;
|
||||
mod response;
|
||||
|
||||
pub use dataset::*;
|
||||
pub use fallback::*;
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
use std::{fmt::Debug, path::Path};
|
||||
|
||||
use axum::response::{IntoResponse, Json, Response};
|
||||
use bincode::Decode;
|
||||
use parser::{Date, SerializedBTreeMap, SerializedVec};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{
|
||||
api::structs::{Chunk, Kind, Route},
|
||||
header_map::HeaderMapUtils,
|
||||
};
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct WrappedDataset<'a, T>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
source: &'a str,
|
||||
chunk: Chunk,
|
||||
dataset: T,
|
||||
}
|
||||
|
||||
pub fn typed_value_to_response<T>(
|
||||
kind: Kind,
|
||||
route: &Route,
|
||||
chunk: Option<Chunk>,
|
||||
) -> color_eyre::Result<Response>
|
||||
where
|
||||
T: Serialize + Debug + DeserializeOwned + Decode,
|
||||
{
|
||||
Ok(match kind {
|
||||
Kind::Date => dataset_to_response(
|
||||
route
|
||||
.serialization
|
||||
.import::<SerializedBTreeMap<Date, T>>(Path::new(&route.file_path))?,
|
||||
chunk.unwrap(),
|
||||
),
|
||||
Kind::Height => dataset_to_response(
|
||||
route
|
||||
.serialization
|
||||
.import::<SerializedVec<T>>(Path::new(&route.file_path))?,
|
||||
chunk.unwrap(),
|
||||
),
|
||||
Kind::Last => value_to_response(
|
||||
route
|
||||
.serialization
|
||||
.import::<T>(Path::new(&route.file_path))?,
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
fn value_to_response<T>(value: T) -> Response
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
generic_to_reponse(value, None, 5)
|
||||
}
|
||||
|
||||
fn dataset_to_response<T>(dataset: T, chunk: Chunk) -> Response
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
generic_to_reponse(dataset, Some(chunk), 60)
|
||||
}
|
||||
|
||||
pub fn generic_to_reponse<T>(generic: T, chunk: Option<Chunk>, cache_time: u64) -> Response
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
let mut response = {
|
||||
if let Some(chunk) = chunk {
|
||||
Json(WrappedDataset {
|
||||
source: "https://satonomics.xyz",
|
||||
chunk,
|
||||
dataset: generic,
|
||||
})
|
||||
.into_response()
|
||||
} else {
|
||||
Json(generic).into_response()
|
||||
}
|
||||
};
|
||||
|
||||
let headers = response.headers_mut();
|
||||
|
||||
let max_age = cache_time;
|
||||
let stale_while_revalidate = 2 * max_age;
|
||||
|
||||
headers.insert_cors();
|
||||
headers.insert_content_type_application_json();
|
||||
headers.insert_cache_control(max_age, stale_while_revalidate);
|
||||
|
||||
response
|
||||
}
|
||||
@@ -101,7 +101,7 @@ impl Routes {
|
||||
.map(|route| format!("\"{}\"", route.url_path))
|
||||
.join(" | ");
|
||||
|
||||
format!("// This file is auto generated by the server\n// Manual changes are forbidden\n\ntype {}Path = {};\n", name, paths)
|
||||
format!("export type {}Path = {};\n", name, paths)
|
||||
};
|
||||
|
||||
let date_type = map_to_type("Date", &self.date);
|
||||
@@ -112,7 +112,7 @@ impl Routes {
|
||||
|
||||
fs::write(
|
||||
format!("{WEBSITE_TYPES_PATH}/paths.d.ts"),
|
||||
format!("{date_type}\n{height_type}\n{last_type}"),
|
||||
format!("// This file is auto generated by the server\n// Manual changes are forbidden\n\n{date_type}\n{height_type}\n{last_type}"),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user