mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-06-21 03:58:24 -07:00
global: snapshot
This commit is contained in:
@@ -31,6 +31,7 @@ pub async fn handler(
|
||||
|
||||
let format = resolved.format();
|
||||
let etag = resolved.etag();
|
||||
let csv_filename = resolved.csv_filename();
|
||||
|
||||
if headers.has_etag(etag.as_str()) {
|
||||
return Ok((StatusCode::NOT_MODIFIED, "").into_response());
|
||||
@@ -55,8 +56,8 @@ pub async fn handler(
|
||||
h.insert_cache_control(CACHE_CONTROL);
|
||||
match format {
|
||||
Format::CSV => {
|
||||
h.insert_content_disposition_attachment();
|
||||
h.insert_content_type_text_csv()
|
||||
h.insert_content_disposition_attachment(&csv_filename);
|
||||
h.insert_content_type_text_csv();
|
||||
}
|
||||
Format::JSON => h.insert_content_type_application_json(),
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ pub async fn handler(
|
||||
|
||||
let format = resolved.format();
|
||||
let etag = resolved.etag();
|
||||
let csv_filename = resolved.csv_filename();
|
||||
|
||||
if headers.has_etag(etag.as_str()) {
|
||||
return Ok((StatusCode::NOT_MODIFIED, "").into_response());
|
||||
@@ -55,8 +56,8 @@ pub async fn handler(
|
||||
h.insert_cache_control(CACHE_CONTROL);
|
||||
match format {
|
||||
Format::CSV => {
|
||||
h.insert_content_disposition_attachment();
|
||||
h.insert_content_type_text_csv()
|
||||
h.insert_content_disposition_attachment(&csv_filename);
|
||||
h.insert_content_type_text_csv();
|
||||
}
|
||||
Format::JSON => h.insert_content_type_application_json(),
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ use crate::{
|
||||
extended::HeaderMapExtended,
|
||||
};
|
||||
|
||||
const SUNSET: &str = "2027-01-01T00:00:00Z";
|
||||
|
||||
use super::AppState;
|
||||
|
||||
pub async fn handler(
|
||||
@@ -31,6 +33,7 @@ pub async fn handler(
|
||||
|
||||
let format = resolved.format();
|
||||
let etag = resolved.etag();
|
||||
let csv_filename = resolved.csv_filename();
|
||||
|
||||
if headers.has_etag(etag.as_str()) {
|
||||
return Ok((StatusCode::NOT_MODIFIED, "").into_response());
|
||||
@@ -55,11 +58,12 @@ pub async fn handler(
|
||||
h.insert_cache_control(CACHE_CONTROL);
|
||||
match format {
|
||||
Format::CSV => {
|
||||
h.insert_content_disposition_attachment();
|
||||
h.insert_content_type_text_csv()
|
||||
h.insert_content_disposition_attachment(&csv_filename);
|
||||
h.insert_content_type_text_csv();
|
||||
}
|
||||
Format::JSON => h.insert_content_type_application_json(),
|
||||
}
|
||||
h.insert_deprecation(SUNSET);
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ use brk_types::{
|
||||
MetricSelection, MetricSelectionLegacy, MetricWithIndex, Metrics, PaginatedMetrics, Pagination,
|
||||
};
|
||||
|
||||
use crate::{CacheStrategy, extended::TransformResponseExtended};
|
||||
use crate::{CacheStrategy, Error, extended::TransformResponseExtended};
|
||||
|
||||
use super::AppState;
|
||||
|
||||
@@ -244,7 +244,9 @@ impl ApiMetricsRoutes for ApiRouter<AppState> {
|
||||
|
||||
let ser_index = split.next().unwrap();
|
||||
let Ok(index) = Index::try_from(ser_index) else {
|
||||
return format!("Index {ser_index} doesn't exist").into_response();
|
||||
return Error::not_found(
|
||||
format!("Index '{ser_index}' doesn't exist")
|
||||
).into_response();
|
||||
};
|
||||
|
||||
let params = MetricSelection::from((
|
||||
|
||||
@@ -14,7 +14,7 @@ mod compact;
|
||||
|
||||
pub use compact::ApiJson;
|
||||
|
||||
use aide::openapi::{Contact, Info, License, OpenApi, Tag};
|
||||
use aide::openapi::{Contact, Info, License, OpenApi, Server, ServerVariable, Tag};
|
||||
|
||||
use crate::VERSION;
|
||||
|
||||
@@ -31,6 +31,34 @@ pub fn create_openapi() -> OpenApi {
|
||||
- **Multiple formats**: JSON and CSV output
|
||||
- **LLM-optimized**: [`/llms.txt`](/llms.txt) for discovery, [`/api.json`](/api.json) compact OpenAPI spec for tool use (full spec at [`/openapi.json`](/openapi.json))
|
||||
|
||||
### Quick start
|
||||
|
||||
```bash
|
||||
curl -s https://bitview.space/api/block-height/0
|
||||
curl -s https://bitview.space/api/metrics/search/price
|
||||
curl -s https://bitview.space/api/metric/price/day1
|
||||
```
|
||||
|
||||
### Errors
|
||||
|
||||
All errors return structured JSON with a consistent format:
|
||||
|
||||
```json
|
||||
{
|
||||
"error": {
|
||||
"type": "not_found",
|
||||
"code": "metric_not_found",
|
||||
"message": "'foo' not found, did you mean 'bar'?",
|
||||
"doc_url": "https://bitcoinresearchkit.org/api"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- **`type`**: Error category — `invalid_request` (400), `forbidden` (403), `not_found` (404), `unavailable` (503), or `internal` (500)
|
||||
- **`code`**: Machine-readable error code (e.g. `invalid_address`, `metric_not_found`, `weight_exceeded`)
|
||||
- **`message`**: Human-readable description
|
||||
- **`doc_url`**: Link to API documentation
|
||||
|
||||
### Client Libraries
|
||||
|
||||
- [JavaScript](https://www.npmjs.com/package/brk-client)
|
||||
@@ -130,9 +158,38 @@ pub fn create_openapi() -> OpenApi {
|
||||
},
|
||||
];
|
||||
|
||||
let servers = vec![Server {
|
||||
url: "{scheme}://{host}".into(),
|
||||
description: Some("BRK server".into()),
|
||||
variables: [
|
||||
(
|
||||
"scheme".into(),
|
||||
ServerVariable {
|
||||
enumeration: vec!["https".into(), "http".into()],
|
||||
default: "https".into(),
|
||||
description: Some("Protocol".into()),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
"host".into(),
|
||||
ServerVariable {
|
||||
default: "bitview.space".into(),
|
||||
description: Some(
|
||||
"Server address (e.g. bitview.space or localhost:3110)".into(),
|
||||
),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
]
|
||||
.into(),
|
||||
..Default::default()
|
||||
}];
|
||||
|
||||
OpenApi {
|
||||
info,
|
||||
tags,
|
||||
servers,
|
||||
..OpenApi::default()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,9 @@ use axum::{
|
||||
extract::State,
|
||||
http::{HeaderMap, Uri},
|
||||
};
|
||||
use brk_types::{DiskUsage, Health, Height, SyncStatus};
|
||||
use vecdb::ReadableVec;
|
||||
use brk_types::{DiskUsage, Health, SyncStatus};
|
||||
|
||||
use crate::{CacheStrategy, extended::TransformResponseExtended};
|
||||
use crate::{CacheStrategy, VERSION, extended::TransformResponseExtended};
|
||||
|
||||
use super::AppState;
|
||||
|
||||
@@ -26,23 +25,7 @@ impl ServerRoutes for ApiRouter<AppState> {
|
||||
|
||||
state
|
||||
.cached_json(&headers, CacheStrategy::Height, &uri, move |q| {
|
||||
let indexed_height = q.height();
|
||||
let tip_height = tip_height?;
|
||||
let blocks_behind = Height::from(tip_height.saturating_sub(*indexed_height));
|
||||
let last_indexed_at_unix = q
|
||||
.indexer()
|
||||
.vecs
|
||||
.blocks
|
||||
.timestamp
|
||||
.collect_one(indexed_height).unwrap();
|
||||
|
||||
Ok(SyncStatus {
|
||||
indexed_height,
|
||||
tip_height,
|
||||
blocks_behind,
|
||||
last_indexed_at: last_indexed_at_unix.to_iso8601(),
|
||||
last_indexed_at_unix,
|
||||
})
|
||||
Ok(q.sync_status(tip_height?))
|
||||
})
|
||||
.await
|
||||
},
|
||||
@@ -89,12 +72,19 @@ impl ServerRoutes for ApiRouter<AppState> {
|
||||
get_with(
|
||||
async |State(state): State<AppState>| -> axum::Json<Health> {
|
||||
let uptime = state.started_instant.elapsed();
|
||||
let tip_height = state.client.get_last_height();
|
||||
let sync = state.sync(|q| {
|
||||
let tip_height = tip_height.unwrap_or(q.indexed_height());
|
||||
q.sync_status(tip_height)
|
||||
});
|
||||
axum::Json(Health {
|
||||
status: Cow::Borrowed("healthy"),
|
||||
service: Cow::Borrowed("brk"),
|
||||
version: Cow::Borrowed(VERSION),
|
||||
timestamp: jiff::Timestamp::now().to_string(),
|
||||
started_at: state.started_at.to_string(),
|
||||
uptime_seconds: uptime.as_secs(),
|
||||
sync,
|
||||
})
|
||||
},
|
||||
|op| {
|
||||
|
||||
Reference in New Issue
Block a user