mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-26 15:49:58 -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::*;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -1,14 +1,23 @@
|
||||
use std::path::Path;
|
||||
|
||||
use axum::http::{header, HeaderMap};
|
||||
use chrono::{DateTime, Utc};
|
||||
use parser::log;
|
||||
use reqwest::header::HOST;
|
||||
|
||||
const STALE_IF_ERROR: u64 = 31_536_000; // 1 Year
|
||||
const STALE_IF_ERROR: u64 = 30_000_000; // 1 Year ish
|
||||
|
||||
pub trait HeaderMapUtils {
|
||||
fn get_scheme(&self) -> &str;
|
||||
fn get_host(&self) -> &str;
|
||||
fn check_if_host_is_any_local(&self) -> bool;
|
||||
fn check_if_host_is_0000(&self) -> bool;
|
||||
fn check_if_host_is_localhost(&self) -> bool;
|
||||
|
||||
fn insert_cors(&mut self);
|
||||
|
||||
fn insert_cache_control(&mut self, max_age: u64, stale_while_revalidate: u64);
|
||||
fn insert_last_modified(&mut self, date: DateTime<Utc>);
|
||||
|
||||
fn insert_content_type(&mut self, path: &Path);
|
||||
fn insert_content_type_image_icon(&mut self);
|
||||
@@ -24,6 +33,30 @@ pub trait HeaderMapUtils {
|
||||
}
|
||||
|
||||
impl HeaderMapUtils for HeaderMap {
|
||||
fn get_scheme(&self) -> &str {
|
||||
if self.check_if_host_is_any_local() {
|
||||
"http"
|
||||
} else {
|
||||
"https"
|
||||
}
|
||||
}
|
||||
|
||||
fn get_host(&self) -> &str {
|
||||
self[HOST].to_str().unwrap()
|
||||
}
|
||||
|
||||
fn check_if_host_is_any_local(&self) -> bool {
|
||||
self.check_if_host_is_localhost() || self.check_if_host_is_0000()
|
||||
}
|
||||
|
||||
fn check_if_host_is_0000(&self) -> bool {
|
||||
self.get_host().contains("0.0.0.0")
|
||||
}
|
||||
|
||||
fn check_if_host_is_localhost(&self) -> bool {
|
||||
self.get_host().contains("localhost")
|
||||
}
|
||||
|
||||
fn insert_cors(&mut self) {
|
||||
self.insert(header::ACCESS_CONTROL_ALLOW_ORIGIN, "*".parse().unwrap());
|
||||
self.insert(header::ACCESS_CONTROL_ALLOW_HEADERS, "*".parse().unwrap());
|
||||
@@ -39,6 +72,12 @@ 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();
|
||||
|
||||
self.insert(header::LAST_MODIFIED, formatted.parse().unwrap());
|
||||
}
|
||||
|
||||
// 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() {
|
||||
@@ -60,24 +99,15 @@ impl HeaderMapUtils for HeaderMap {
|
||||
}
|
||||
|
||||
fn insert_content_type_image_icon(&mut self) {
|
||||
self.insert(
|
||||
header::CONTENT_TYPE,
|
||||
"image/x-icon".parse().unwrap(),
|
||||
);
|
||||
self.insert(header::CONTENT_TYPE, "image/x-icon".parse().unwrap());
|
||||
}
|
||||
|
||||
fn insert_content_type_image_jpeg(&mut self) {
|
||||
self.insert(
|
||||
header::CONTENT_TYPE,
|
||||
"image/jpeg".parse().unwrap(),
|
||||
);
|
||||
self.insert(header::CONTENT_TYPE, "image/jpeg".parse().unwrap());
|
||||
}
|
||||
|
||||
fn insert_content_type_image_png(&mut self) {
|
||||
self.insert(
|
||||
header::CONTENT_TYPE,
|
||||
"image/png".parse().unwrap(),
|
||||
);
|
||||
self.insert(header::CONTENT_TYPE, "image/png".parse().unwrap());
|
||||
}
|
||||
|
||||
fn insert_content_type_application_javascript(&mut self) {
|
||||
@@ -92,7 +122,10 @@ impl HeaderMapUtils for HeaderMap {
|
||||
}
|
||||
|
||||
fn insert_content_type_application_manifest_json(&mut self) {
|
||||
self.insert(header::CONTENT_TYPE, "application/manifest+json".parse().unwrap());
|
||||
self.insert(
|
||||
header::CONTENT_TYPE,
|
||||
"application/manifest+json".parse().unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
fn insert_content_type_text_css(&mut self) {
|
||||
|
||||
@@ -10,7 +10,6 @@ use website::WebsiteRoutes;
|
||||
|
||||
mod api;
|
||||
mod header_map;
|
||||
mod response;
|
||||
mod website;
|
||||
|
||||
#[derive(Clone, Debug, Default, Serialize)]
|
||||
|
||||
@@ -9,6 +9,7 @@ use axum::{
|
||||
http::HeaderMap,
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use chrono::{DateTime, Utc};
|
||||
use parser::log;
|
||||
use reqwest::StatusCode;
|
||||
|
||||
@@ -38,17 +39,22 @@ pub async fn file_handler(headers: HeaderMap, path: extract::Path<String>) -> Re
|
||||
}
|
||||
}
|
||||
|
||||
path_to_response(&path)
|
||||
path_to_response(headers, &path)
|
||||
}
|
||||
|
||||
pub async fn index_handler(headers: HeaderMap) -> Response {
|
||||
path_to_response(&str_to_path("index.html"))
|
||||
path_to_response(headers, &str_to_path("index.html"))
|
||||
}
|
||||
|
||||
fn path_to_response(path: &Path) -> Response {
|
||||
fn path_to_response(headers: HeaderMap, path: &Path) -> Response {
|
||||
let mut response;
|
||||
|
||||
if path.extension().unwrap() == "js" {
|
||||
let time = path.metadata().unwrap().modified().unwrap();
|
||||
let date: DateTime<Utc> = time.into();
|
||||
|
||||
let is_localhost = headers.check_if_host_is_localhost();
|
||||
|
||||
if !is_localhost && path.extension().unwrap() == "js" {
|
||||
let content = minify_js(path);
|
||||
|
||||
response = Response::new(content.into());
|
||||
@@ -67,6 +73,12 @@ fn path_to_response(path: &Path) -> Response {
|
||||
headers.insert_cors();
|
||||
headers.insert_content_type(path);
|
||||
|
||||
if !is_localhost {
|
||||
headers.insert_cache_control(10, 50);
|
||||
}
|
||||
|
||||
headers.insert_last_modified(date);
|
||||
|
||||
response
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user