Skip to content

Commit

Permalink
Enable JSON API by default (#3047)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Jan 25, 2024
1 parent 5bdd723 commit 5d32e90
Show file tree
Hide file tree
Showing 23 changed files with 222 additions and 433 deletions.
3 changes: 2 additions & 1 deletion deploy/ord.service
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ ExecStart=/usr/local/bin/ord \
--acme-contact mailto:casey@rodarmor.com \
--csp-origin https://${CSP_ORIGIN} \
--http \
--https
--https \
--disable-json-api
Group=ord
LimitNOFILE=65536
MemoryDenyWriteExecute=true
Expand Down
8 changes: 4 additions & 4 deletions docs/src/guides/explorer.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ To specify a port add the `--http-port` flag:

`ord server --http-port 8080`

To enable the JSON-API endpoints add the `--enable-json-api` or `-j` flag (see
[here](#json-api) for more info):
The JSON-API endpoints are enabled by default, to disable them add the
`--disable-json-api` flag (see [here](#json-api) for more info):

`ord server --enable-json-api`
`ord server --disable-json-api`

Search
------
Expand Down Expand Up @@ -72,7 +72,7 @@ been issued when they are mined:
JSON-API
--------

You can run `ord server` with the `--enable-json-api` flag to access endpoints that
By default the `ord server` gives access to endpoints that
return JSON instead of HTML if you set the HTTP `Accept: application/json`
header. The structure of these objects closely follows
what is shown in the HTML. These endpoints are:
Expand Down
2 changes: 1 addition & 1 deletion src/server_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ pub(crate) struct ServerConfig {
pub(crate) decompress: bool,
pub(crate) domain: Option<String>,
pub(crate) index_sats: bool,
pub(crate) is_json_api_enabled: bool,
pub(crate) json_api_enabled: bool,
}
10 changes: 5 additions & 5 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ pub struct Server {
pub(crate) https: bool,
#[arg(long, help = "Redirect HTTP traffic to HTTPS.")]
pub(crate) redirect_http_to_https: bool,
#[arg(long, short = 'j', help = "Enable JSON API.")]
pub(crate) enable_json_api: bool,
#[arg(long, help = "Disable JSON API.")]
pub(crate) disable_json_api: bool,
#[arg(
long,
help = "Decompress encoded content. Currently only supports brotli. Be careful using this on production instances. A decompressed inscription may be arbitrarily large, making decompression a DoS vector."
Expand Down Expand Up @@ -206,7 +206,7 @@ impl Server {
csp_origin: self.csp_origin.clone(),
domain: acme_domains.first().cloned(),
index_sats: index.has_sat_index(),
is_json_api_enabled: self.enable_json_api,
json_api_enabled: !self.disable_json_api,
decompress: self.decompress,
});

Expand Down Expand Up @@ -1665,7 +1665,7 @@ mod tests {
.build(),
None,
&["--chain", "regtest"],
&["--enable-json-api"],
&[],
)
}

Expand All @@ -1687,7 +1687,7 @@ mod tests {
.build(),
None,
&["--chain", "regtest", "--index-runes"],
&["--enable-json-api"],
&[],
)
}

Expand Down
6 changes: 3 additions & 3 deletions src/subcommand/server/accept_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mod tests {
let encodings = AcceptEncoding::from_request_parts(
&mut req.into_parts().0,
&Arc::new(ServerConfig {
is_json_api_enabled: false,
json_api_enabled: false,
decompress: false,
..Default::default()
}),
Expand All @@ -78,7 +78,7 @@ mod tests {
let encodings = AcceptEncoding::from_request_parts(
&mut req.into_parts().0,
&Arc::new(ServerConfig {
is_json_api_enabled: false,
json_api_enabled: false,
decompress: false,
..Default::default()
}),
Expand Down Expand Up @@ -107,7 +107,7 @@ mod tests {
let encodings = AcceptEncoding::from_request_parts(
&mut req.into_parts().0,
&Arc::new(ServerConfig {
is_json_api_enabled: false,
json_api_enabled: false,
decompress: false,
..Default::default()
}),
Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/server/accept_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ where
state: &S,
) -> Result<Self, Self::Rejection> {
let state = Arc::from_ref(state);
let json_api_enabled = state.is_json_api_enabled;
let json_api_enabled = state.json_api_enabled;
let json_header = parts
.headers
.get("accept")
Expand All @@ -24,7 +24,7 @@ where
if json_header && json_api_enabled {
Ok(Self(true))
} else if json_header && !json_api_enabled {
Err((StatusCode::NOT_ACCEPTABLE, "JSON API not enabled"))
Err((StatusCode::NOT_ACCEPTABLE, "JSON API disabled"))
} else {
Ok(Self(false))
}
Expand Down
7 changes: 2 additions & 5 deletions tests/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ fn with_runes() {
.network(Network::Regtest)
.build();

let ord_rpc_server = TestServer::spawn_with_server_args(
&bitcoin_rpc_server,
&["--regtest", "--index-runes"],
&["--enable-json-api"],
);
let ord_rpc_server =
TestServer::spawn_with_server_args(&bitcoin_rpc_server, &["--regtest", "--index-runes"], &[]);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

Expand Down
2 changes: 1 addition & 1 deletion tests/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn from_stdin() {
#[test]
fn from_core() {
let bitcoin_rpc_server = test_bitcoincore_rpc::spawn();
let ord_rpc_server = TestServer::spawn_with_json_api(&bitcoin_rpc_server);
let ord_rpc_server = TestServer::spawn(&bitcoin_rpc_server);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

Expand Down
94 changes: 27 additions & 67 deletions tests/etch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ fn flag_is_required() {
.network(Network::Regtest)
.build();

let ord_rpc_server =
TestServer::spawn_with_server_args(&bitcoin_rpc_server, &["--regtest"], &["--enable-json-api"]);
let ord_rpc_server = TestServer::spawn_with_server_args(&bitcoin_rpc_server, &["--regtest"], &[]);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

Expand All @@ -34,11 +33,8 @@ fn divisibility_over_max_is_an_error() {
.network(Network::Regtest)
.build();

let ord_rpc_server = TestServer::spawn_with_server_args(
&bitcoin_rpc_server,
&["--regtest", "--index-runes"],
&["--enable-json-api"],
);
let ord_rpc_server =
TestServer::spawn_with_server_args(&bitcoin_rpc_server, &["--regtest", "--index-runes"], &[]);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

Expand All @@ -62,11 +58,8 @@ fn supply_over_max_is_an_error() {
.network(Network::Regtest)
.build();

let ord_rpc_server = TestServer::spawn_with_server_args(
&bitcoin_rpc_server,
&["--regtest", "--index-runes"],
&["--enable-json-api"],
);
let ord_rpc_server =
TestServer::spawn_with_server_args(&bitcoin_rpc_server, &["--regtest", "--index-runes"], &[]);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

Expand All @@ -90,11 +83,8 @@ fn rune_below_minimum_is_an_error() {
.network(Network::Regtest)
.build();

let ord_rpc_server = TestServer::spawn_with_server_args(
&bitcoin_rpc_server,
&["--regtest", "--index-runes"],
&["--enable-json-api"],
);
let ord_rpc_server =
TestServer::spawn_with_server_args(&bitcoin_rpc_server, &["--regtest", "--index-runes"], &[]);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

Expand All @@ -118,11 +108,8 @@ fn reserved_rune_is_an_error() {
.network(Network::Regtest)
.build();

let ord_rpc_server = TestServer::spawn_with_server_args(
&bitcoin_rpc_server,
&["--regtest", "--index-runes"],
&["--enable-json-api"],
);
let ord_rpc_server =
TestServer::spawn_with_server_args(&bitcoin_rpc_server, &["--regtest", "--index-runes"], &[]);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

Expand All @@ -144,11 +131,8 @@ fn trying_to_etch_an_existing_rune_is_an_error() {
.network(Network::Regtest)
.build();

let ord_rpc_server = TestServer::spawn_with_server_args(
&bitcoin_rpc_server,
&["--regtest", "--index-runes"],
&["--enable-json-api"],
);
let ord_rpc_server =
TestServer::spawn_with_server_args(&bitcoin_rpc_server, &["--regtest", "--index-runes"], &[]);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

Expand All @@ -174,11 +158,8 @@ fn runes_can_be_etched() {
.network(Network::Regtest)
.build();

let ord_rpc_server = TestServer::spawn_with_server_args(
&bitcoin_rpc_server,
&["--regtest", "--index-runes"],
&["--enable-json-api"],
);
let ord_rpc_server =
TestServer::spawn_with_server_args(&bitcoin_rpc_server, &["--regtest", "--index-runes"], &[]);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

Expand Down Expand Up @@ -237,11 +218,8 @@ fn etch_sets_integer_fee_rate_correctly() {
.network(Network::Regtest)
.build();

let ord_rpc_server = TestServer::spawn_with_server_args(
&bitcoin_rpc_server,
&["--regtest", "--index-runes"],
&["--enable-json-api"],
);
let ord_rpc_server =
TestServer::spawn_with_server_args(&bitcoin_rpc_server, &["--regtest", "--index-runes"], &[]);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

Expand Down Expand Up @@ -273,11 +251,8 @@ fn etch_sets_decimal_fee_rate_correctly() {
.network(Network::Regtest)
.build();

let ord_rpc_server = TestServer::spawn_with_server_args(
&bitcoin_rpc_server,
&["--regtest", "--index-runes"],
&["--enable-json-api"],
);
let ord_rpc_server =
TestServer::spawn_with_server_args(&bitcoin_rpc_server, &["--regtest", "--index-runes"], &[]);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

Expand Down Expand Up @@ -309,11 +284,8 @@ fn etch_does_not_select_inscribed_utxos() {
.network(Network::Regtest)
.build();

let ord_rpc_server = TestServer::spawn_with_server_args(
&bitcoin_rpc_server,
&["--regtest", "--index-runes"],
&["--enable-json-api"],
);
let ord_rpc_server =
TestServer::spawn_with_server_args(&bitcoin_rpc_server, &["--regtest", "--index-runes"], &[]);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

Expand Down Expand Up @@ -359,11 +331,8 @@ fn inscribe_does_not_select_runic_utxos() {
.network(Network::Regtest)
.build();

let ord_rpc_server = TestServer::spawn_with_server_args(
&bitcoin_rpc_server,
&["--regtest", "--index-runes"],
&["--enable-json-api"],
);
let ord_rpc_server =
TestServer::spawn_with_server_args(&bitcoin_rpc_server, &["--regtest", "--index-runes"], &[]);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

Expand Down Expand Up @@ -404,11 +373,8 @@ fn send_amount_does_not_select_runic_utxos() {
.network(Network::Regtest)
.build();

let ord_rpc_server = TestServer::spawn_with_server_args(
&bitcoin_rpc_server,
&["--regtest", "--index-runes"],
&["--enable-json-api"],
);
let ord_rpc_server =
TestServer::spawn_with_server_args(&bitcoin_rpc_server, &["--regtest", "--index-runes"], &[]);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

Expand Down Expand Up @@ -439,11 +405,8 @@ fn send_satpoint_does_not_send_runic_utxos() {
.network(Network::Regtest)
.build();

let ord_rpc_server = TestServer::spawn_with_server_args(
&bitcoin_rpc_server,
&["--regtest", "--index-runes"],
&["--enable-json-api"],
);
let ord_rpc_server =
TestServer::spawn_with_server_args(&bitcoin_rpc_server, &["--regtest", "--index-runes"], &[]);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

Expand Down Expand Up @@ -474,11 +437,8 @@ fn send_inscription_does_not_select_runic_utxos() {
.network(Network::Regtest)
.build();

let ord_rpc_server = TestServer::spawn_with_server_args(
&bitcoin_rpc_server,
&["--regtest", "--index-runes"],
&["--enable-json-api"],
);
let ord_rpc_server =
TestServer::spawn_with_server_args(&bitcoin_rpc_server, &["--regtest", "--index-runes"], &[]);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

Expand Down
2 changes: 1 addition & 1 deletion tests/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn index_runs_with_rpc_user_and_pass_as_env_vars() {
#[test]
fn export_inscription_number_to_id_tsv() {
let bitcoin_rpc_server = test_bitcoincore_rpc::spawn();
let ord_rpc_server = TestServer::spawn_with_json_api(&bitcoin_rpc_server);
let ord_rpc_server = TestServer::spawn(&bitcoin_rpc_server);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

Expand Down
Loading

0 comments on commit 5d32e90

Please sign in to comment.