Crate brk_server

Source
Expand description

§BRK Server

GitHub Repo stars kibo.money License Version Documentation Size Dependency status Discord Nostr Bluesky X

A crate that serves Bitcoin data and swappable front-ends, built on top of brk_indexer, brk_computer and brk_query.

The file handler, will serve the website specified by the user if any, which can be no website, kibo.money or custom (which is a blank folder for people to experiment). If a website is specified and the server is ran outside of the brk project and thus can’t find the requested website, it will download the whole project with the correct version from Github and store it in .brk to be able to serve to website. This is due to the crate size limit on crates.io and the various shenanigans that need to be done to have a website in a crate.

The API uses brk_query and so inherites all of its features including formats.

§Endpoints

§API

§GET /api/vecs/indexes

A list of all possible vec indexes and their accepted variants

§GET /api/vecs/ids

A list of all possible vec ids

§GET /api/vecs/id-to-indexes

A list of all possible vec ids and their supported vec indexes

§GET /api/vecs/index-to-ids

A list of all possible vec indexes and their supported vec ids

§GET /api/query

This endpoint retrieves data based on the specified vector index and values.

Parameters:

ParameterTypeRequiredDescription
indexVecIndexYesThe vector index to query.
valuesVecId[]YesA comma or space-separated list of vector IDs to retrieve.
fromunsigned intNoThe starting index for pagination (default is 0).
tounsigned intNoThe ending index for pagination (default is the total number of results).
formatstringNoThe format of the response. Options include json, csv, tsv, or md (default is json).

Examples:

GET /api/query?index=date&values=ohlc
GET /api/query?index=week&values=ohlc,block-interval-average&from=0&to=20&format=md

§Meta

§GET /version

The version of the server and thus BRK.

§Files

§GET /*

Catch all.

When no pattern is found, the server will look for a match inside the folder of the chosen website, if any.

§Example

use std::{path::Path, thread::sleep, time::Duration};

use brk_computer::Computer;
use brk_core::default_bitcoin_path;
use brk_exit::Exit;
use brk_fetcher::Fetcher;
use brk_indexer::Indexer;
use brk_parser::{
    Parser,
    rpc::{self, RpcApi},
};
use brk_server::{Server, Website};
use brk_vec::Computation;

pub fn main() -> color_eyre::Result<()> {
    color_eyre::install()?;

    brk_logger::init(Some(Path::new(".log")));

    let process = true;

    let bitcoin_dir = default_bitcoin_path();

    let rpc = Box::leak(Box::new(rpc::Client::new(
        "http://localhost:8332",
        rpc::Auth::CookieFile(bitcoin_dir.join(".cookie")),
    )?));
    let exit = Exit::new();

    let parser = Parser::new(bitcoin_dir.join("blocks"), rpc);

    let outputs_dir = Path::new("../../_outputs");

    let compressed = true;

    let mut indexer = Indexer::new(outputs_dir, compressed, true)?;
    indexer.import_stores()?;
    indexer.import_vecs()?;

    let fetcher = Some(Fetcher::import(None)?);

    let mut computer = Computer::new(outputs_dir, fetcher, compressed);
    computer.import_stores(&indexer)?;
    computer.import_vecs(&indexer, Computation::Lazy)?;

    tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()?
        .block_on(async {
            let served_indexer = indexer.clone();
            let served_computer = computer.clone();

            let server = Server::new(served_indexer, served_computer, Website::KiboMoney)?;

            let server = tokio::spawn(async move {
                server.serve().await.unwrap();
            });

            if process {
                loop {
                    let block_count = rpc.get_block_count()?;

                    let starting_indexes = indexer.index(&parser, rpc, &exit)?;

                    computer.compute(&mut indexer, starting_indexes, &exit)?;

                    while block_count == rpc.get_block_count()? {
                        sleep(Duration::from_secs(1))
                    }
                }
            }

            #[allow(unreachable_code)]
            server.await.unwrap();

            Ok(())
        }) as color_eyre::Result<()>
}

Re-exports§

pub use tokio;

Structs§

AppState
Server

Enums§

Website