global: one big snapshot

This commit is contained in:
nym21
2025-08-02 16:59:22 +02:00
parent aa8b47a3dd
commit f7aa9424db
252 changed files with 6283 additions and 5264 deletions

View File

@@ -4,8 +4,9 @@ use axum::{
http::{HeaderMap, StatusCode},
response::{IntoResponse, Response},
};
use brk_error::{Error, Result};
use brk_interface::{Format, Output, Params};
use color_eyre::eyre::eyre;
use brk_vecs::Stamp;
use crate::traits::{HeaderMapExtended, ResponseExtended};
@@ -37,7 +38,7 @@ fn req_to_response_res(
headers: HeaderMap,
Query(params): Query<Params>,
AppState { interface, .. }: AppState,
) -> color_eyre::Result<Response> {
) -> Result<Response> {
let vecs = interface.search(&params);
if vecs.is_empty() {
@@ -56,11 +57,17 @@ fn req_to_response_res(
.sum::<usize>();
if weight > MAX_WEIGHT {
return Err(eyre!("Request is too heavy, max weight is {MAX_WEIGHT}"));
return Err(Error::Str(
"Request is too heavy, max weight is {MAX_WEIGHT}",
));
}
// TODO: height should be from vec, but good enough for now
let etag = vecs.first().unwrap().1.etag(interface.get_height(), to);
let etag = vecs
.first()
.unwrap()
.1
.etag(Stamp::from(u64::from(interface.get_height())), to);
if headers
.get_if_none_match()

View File

@@ -6,6 +6,7 @@ use axum::{
http::{HeaderMap, StatusCode},
response::{IntoResponse, Response},
};
use brk_error::Result;
use log::{error, info};
use crate::{
@@ -73,7 +74,7 @@ fn path_to_response(headers: &HeaderMap, path: &Path) -> Response {
}
}
fn path_to_response_(headers: &HeaderMap, path: &Path) -> color_eyre::Result<Response> {
fn path_to_response_(headers: &HeaderMap, path: &Path) -> Result<Response> {
let (modified, date) = headers.check_if_modified_since(path)?;
if modified == ModifiedState::NotModifiedSince {
return Ok(Response::new_not_modified());

View File

@@ -21,13 +21,13 @@ use axum::{
};
use brk_bundler::bundle;
use brk_computer::Computer;
use brk_core::dot_brk_path;
use brk_error::Result;
use brk_indexer::Indexer;
use brk_interface::Interface;
use brk_mcp::route::MCPRoutes;
use color_eyre::owo_colors::OwoColorize;
use files::FilesRoutes;
use log::{error, info};
use owo_colors::OwoColorize;
use tokio::net::TcpListener;
use tower_http::{compression::CompressionLayer, trace::TraceLayer};
@@ -57,13 +57,17 @@ impl AppState {
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
const DEV_PATH: &str = "../..";
const DOWNLOADS: &str = "downloads";
const WEBSITES: &str = "websites";
pub struct Server(AppState);
impl Server {
pub fn new(indexer: Indexer, computer: Computer, website: Website) -> color_eyre::Result<Self> {
pub fn new(
indexer: Indexer,
computer: Computer,
website: Website,
downloads_path: &Path,
) -> Result<Self> {
let indexer = Box::leak(Box::new(indexer));
let computer = Box::leak(Box::new(computer));
let interface = Box::leak(Box::new(Interface::build(indexer, computer)));
@@ -74,8 +78,6 @@ impl Server {
let websites_path = if fs::exists(&websites_dev_path)? {
websites_dev_path
} else {
let downloads_path = dot_brk_path().join(DOWNLOADS);
let downloaded_websites_path =
downloads_path.join(format!("brk-{VERSION}")).join(WEBSITES);
@@ -90,9 +92,9 @@ impl Server {
let bytes = response.as_bytes();
let cursor = Cursor::new(bytes);
let mut zip = zip::ZipArchive::new(cursor)?;
let mut zip = zip::ZipArchive::new(cursor).unwrap();
zip.extract(&downloads_path)?;
zip.extract(downloads_path).unwrap();
}
downloaded_websites_path
@@ -112,7 +114,7 @@ impl Server {
}))
}
pub async fn serve(self, watch: bool, mcp: bool) -> color_eyre::Result<()> {
pub async fn serve(self, watch: bool, mcp: bool) -> Result<()> {
let state = self.0;
if let Some(websites_path) = state.websites_path.clone() {

View File

@@ -7,6 +7,7 @@ use axum::http::{
HeaderMap,
header::{self, IF_MODIFIED_SINCE, IF_NONE_MATCH},
};
use brk_error::Result;
use jiff::{Timestamp, civil::DateTime, fmt::strtime, tz::TimeZone};
const MODIFIED_SINCE_FORMAT: &str = "%a, %d %b %Y %H:%M:%S GMT";
@@ -23,12 +24,8 @@ pub trait HeaderMapExtended {
fn get_if_none_match(&self) -> Option<&str>;
fn get_if_modified_since(&self) -> Option<DateTime>;
fn check_if_modified_since(&self, path: &Path)
-> color_eyre::Result<(ModifiedState, DateTime)>;
fn check_if_modified_since_(
&self,
duration: Duration,
) -> color_eyre::Result<(ModifiedState, DateTime)>;
fn check_if_modified_since(&self, path: &Path) -> Result<(ModifiedState, DateTime)>;
fn check_if_modified_since_(&self, duration: Duration) -> Result<(ModifiedState, DateTime)>;
fn insert_cache_control_must_revalidate(&mut self);
fn insert_cache_control_immutable(&mut self);
@@ -91,10 +88,7 @@ impl HeaderMapExtended for HeaderMap {
self.insert(header::ETAG, etag.parse().unwrap());
}
fn check_if_modified_since(
&self,
path: &Path,
) -> color_eyre::Result<(ModifiedState, DateTime)> {
fn check_if_modified_since(&self, path: &Path) -> Result<(ModifiedState, DateTime)> {
self.check_if_modified_since_(
path.metadata()?
.modified()?
@@ -102,10 +96,7 @@ impl HeaderMapExtended for HeaderMap {
)
}
fn check_if_modified_since_(
&self,
duration: Duration,
) -> color_eyre::Result<(ModifiedState, DateTime)> {
fn check_if_modified_since_(&self, duration: Duration) -> Result<(ModifiedState, DateTime)> {
let date = Timestamp::new(duration.as_secs() as i64, 0)
.unwrap()
.to_zoned(TimeZone::UTC)