Skip to content

Commit

Permalink
Allow configuring everything
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Mar 5, 2024
1 parent 3687138 commit d012069
Show file tree
Hide file tree
Showing 10 changed files with 442 additions and 372 deletions.
4 changes: 2 additions & 2 deletions ord.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
chain: mainnet

# use username `bar` and password `foo` for bitcoind RPC calls
bitcoin_rpc_user: bar
bitcoin_rpc_pass: foo
bitcoin-rpc-user: bar
bitcoin-rpc-pass: foo

# prevent `ord server` from serving the content of the inscriptions below
hidden:
Expand Down
6 changes: 1 addition & 5 deletions src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ impl Arguments {
);
}

let config = self.options.config()?;

self
.subcommand
.run(Settings::new(self.options, env, config)?)
self.subcommand.run(self.options.load_settings()?)
}
}
20 changes: 0 additions & 20 deletions src/config.rs

This file was deleted.

10 changes: 5 additions & 5 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,16 @@ impl Index {
);
}

let db_cache_size = match settings.db_cache_size {
Some(db_cache_size) => db_cache_size,
let index_cache_size = match settings.index_cache_size {
Some(index_cache_size) => index_cache_size,
None => {
let mut sys = System::new();
sys.refresh_memory();
usize::try_from(sys.total_memory() / 4)?
}
};

log::info!("Setting DB cache size to {} bytes", db_cache_size);
log::info!("Setting DB cache size to {} bytes", index_cache_size);

let durability = if cfg!(test) {
redb::Durability::None
Expand Down Expand Up @@ -284,7 +284,7 @@ impl Index {
};

let database = match Database::builder()
.set_cache_size(db_cache_size)
.set_cache_size(index_cache_size)
.set_repair_callback(repair_callback)
.open(&path)
{
Expand Down Expand Up @@ -319,7 +319,7 @@ impl Index {
if error.kind() == io::ErrorKind::NotFound =>
{
let database = Database::builder()
.set_cache_size(db_cache_size)
.set_cache_size(index_cache_size)
.create(&path)?;

let mut tx = database.begin_write()?;
Expand Down
2 changes: 1 addition & 1 deletion src/inscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use self::{envelope::Envelope, inscription::Inscription, inscription_id::Ins
mod charm;
mod envelope;
mod inscription;
mod inscription_id;
pub(crate) mod inscription_id;
pub(crate) mod media;
mod tag;
pub(crate) mod teleburn;
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use {
self::{
arguments::Arguments,
blocktime::Blocktime,
config::Config,
decimal::Decimal,
inscriptions::{
inscription_id,
media::{self, ImageRendering, Media},
teleburn, Charm, ParsedEnvelope,
},
Expand Down Expand Up @@ -107,7 +107,6 @@ pub mod api;
pub mod arguments;
mod blocktime;
pub mod chain;
mod config;
mod decimal;
mod fee_rate;
pub mod index;
Expand Down Expand Up @@ -186,7 +185,7 @@ pub fn parse_ord_server_args(args: &str) -> (Settings, subcommand::server::Serve
match Arguments::try_parse_from(args.split_whitespace()) {
Ok(arguments) => match arguments.subcommand {
Subcommand::Server(server) => (
Settings::new(
Settings::merge(
arguments.options,
vec![("INTEGRATION_TEST".into(), "1".into())]
.into_iter()
Expand Down
96 changes: 63 additions & 33 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ pub struct Options {
pub(crate) minify: bool,
#[arg(long, help = "Load Bitcoin Core data dir from <BITCOIN_DATA_DIR>.")]
pub(crate) bitcoin_data_dir: Option<PathBuf>,
#[arg(long, help = "Authenticate to Bitcoin Core RPC with <RPC_PASS>.")]
pub(crate) bitcoin_rpc_pass: Option<String>,
#[arg(long, help = "Authenticate to Bitcoin Core RPC as <RPC_USER>.")]
pub(crate) bitcoin_rpc_user: Option<String>,
#[arg(
long,
help = "Authenticate to Bitcoin Core RPC with <BITCOIN_RPC_PASSWORD>."
)]
pub(crate) bitcoin_rpc_password: Option<String>,
#[arg(
long,
help = "Authenticate to Bitcoin Core RPC as <BITCOIN_RPC_USERNAME>."
)]
pub(crate) bitcoin_rpc_username: Option<String>,
#[arg(long = "chain", value_enum, help = "Use <CHAIN>. [default: mainnet]")]
pub(crate) chain_argument: Option<Chain>,
#[arg(long, help = "Load configuration from <CONFIG>.")]
Expand All @@ -23,13 +29,8 @@ pub struct Options {
pub(crate) config_dir: Option<PathBuf>,
#[arg(long, help = "Load Bitcoin Core RPC cookie file from <COOKIE_FILE>.")]
pub(crate) cookie_file: Option<PathBuf>,
#[arg(long, help = "Store index in <DATA_DIR>.", default_value_os_t = Options::default_data_dir())]
pub(crate) data_dir: PathBuf,
#[arg(
long,
help = "Set index cache to <DB_CACHE_SIZE> bytes. By default takes 1/4 of available RAM."
)]
pub(crate) db_cache_size: Option<usize>,
#[arg(long, help = "Store index in <DATA_DIR>.")]
pub(crate) data_dir: Option<PathBuf>,
#[arg(
long,
default_value = "5000",
Expand All @@ -45,6 +46,11 @@ pub struct Options {
pub(crate) height_limit: Option<u32>,
#[arg(long, help = "Use index at <INDEX>.")]
pub(crate) index: Option<PathBuf>,
#[arg(
long,
help = "Set index cache to <INDEX_CACHE_SIZE> bytes. By default takes 1/4 of available RAM."
)]
pub(crate) index_cache_size: Option<usize>,
#[arg(
long,
help = "Track location of runes. RUNES ARE IN AN UNFINISHED PRE-ALPHA STATE AND SUBJECT TO CHANGE AT ANY TIME."
Expand All @@ -56,6 +62,8 @@ pub struct Options {
pub(crate) index_spent_sats: bool,
#[arg(long, help = "Store transactions in index.")]
pub(crate) index_transactions: bool,
#[arg(long, help = "Run in integration test mode.")]
pub(crate) integration_test: bool,
#[arg(
long,
short,
Expand All @@ -80,44 +88,66 @@ pub struct Options {
#[arg(
long,
requires = "password",
help = "Require basic HTTP authentication with <USERNAME>. Credentials are sent in cleartext. Consider using authentication in conjunction with HTTPS."
help = "Require requests to `ord server` use basic HTTP authentication with <USERNAME>. Credentials are sent in cleartext. Consider using authentication in conjunction with HTTPS."
)]
pub(crate) username: Option<String>,
pub(crate) server_username: Option<String>,
}

impl Options {
fn default_data_dir() -> PathBuf {
dirs::data_dir()
.map(|dir| dir.join("ord"))
.expect("failed to retrieve data dir")
}

pub(crate) fn config(&self) -> Result<Config> {
pub(crate) fn load_settings(self) -> Result<Settings> {
let path = match &self.config {
Some(path) => path.clone(),
Some(path) => Some(path.clone()),
None => match &self.config_dir {
Some(dir) => {
let path = dir.join("ord.yaml");
if !path.exists() {
return Ok(Default::default());
if path.exists() {
Some(path)
} else {
None
}
path
}
None => return Ok(Default::default()),
None => None,
},
};

serde_yaml::from_reader(
File::open(&path).context(anyhow!("failed to open config file `{}`", path.display()))?,
)
.context(anyhow!(
"failed to deserialize config file `{}`",
path.display()
))
let config = match path {
Some(path) => serde_yaml::from_reader(
File::open(&path).context(anyhow!("failed to open config file `{}`", path.display()))?,
)
.context(anyhow!(
"failed to deserialize config file `{}`",
path.display()
))?,
None => Settings::default(),
};

let mut env: BTreeMap<String, String> = BTreeMap::new();

for (var, value) in env::vars_os() {
let Some(var) = var.to_str() else {
continue;
};

let Some(key) = var.strip_prefix("ORD_") else {
continue;
};

env.insert(
key.into(),
value.into_string().map_err(|value| {
anyhow!(
"environment variable `{var}` not valid unicode: `{}`",
value.to_string_lossy()
)
})?,
);
}

Settings::merge(self, env, config)
}

#[cfg(test)]
pub(crate) fn settings(self) -> Result<Settings> {
Settings::new(self, Default::default(), Default::default())
Settings::default().merge(self, Default::default())
}
}
Loading

0 comments on commit d012069

Please sign in to comment.