Skip to content

Commit

Permalink
Add list form (ordinals#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
terror authored Mar 22, 2022
1 parent e056f6d commit d662e57
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ derive_more = "0.99.17"
dirs = "4.0.0"
env_logger = "0.9.0"
executable-path = "1.0.0"
http = "0.2.6"
integer-cbrt = "0.1.2"
integer-sqrt = "0.1.5"
jsonrpc = "0.12.1"
Expand All @@ -28,6 +29,7 @@ ord-lmdb-zero = "0.4.5"
rayon = "1.5.1"
redb = { version = "0.0.5", optional = true }
tokio = { version = "1.17.0", features = ["rt-multi-thread"] }
tower-http = { version = "0.2.5", features = ["cors"] }

[dev-dependencies]
criterion = "0.3.5"
Expand Down
6 changes: 6 additions & 0 deletions docs/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true
}
18 changes: 17 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
<h1>THERE WILL ONLY EVER BE 2,099,999,997,690,000 ORDINALS</h1>
<!DOCTYPE html>
<html>
<head>
<title>Ordinals</title>
</head>
<body>
<h1>THERE WILL ONLY EVER BE 2,099,999,997,690,000 ORDINALS</h1>
<h2>List</h2>
<form id="form">
<label for="outpoint">Outpoint:</label>
<input type="text" id="outpoint" name="outpoint" />
<input type="submit" value="Submit" />
<div id="result"></div>
</form>
<script src="index.js"></script>
</body>
</html>
30 changes: 30 additions & 0 deletions docs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
async function list(outpoint) {
document.getElementById('result').innerHTML = '';

try {
const response = await fetch(
`http://api.ordinals.com:8000/list/${outpoint}`
);

if (!response.ok) {
const text = await response.text();
document.getElementById(
'result'
).innerHTML = `${response.statusText}: ${text}`;
return;
}

const ranges = await response.json();

document.getElementById('result').innerHTML = ranges
.map((range) => `[${range[0]}, ${range[1]})<br>`)
.join('');
} catch (error) {
document.getElementById('result').innerHTML = `Exception: ${error}`;
}
}

document.getElementById('form').addEventListener('submit', (e) => {
e.preventDefault();
list(document.getElementById('outpoint').value);
});
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use {
time::{Duration, Instant},
},
tokio::runtime::Runtime,
tower_http::cors::{Any, CorsLayer},
};

#[cfg(feature = "redb")]
Expand Down
14 changes: 11 additions & 3 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ impl Server {
let app = Router::new()
.route("/list/:outpoint", get(Self::list))
.route("/status", get(Self::status))
.layer(extract::Extension(Arc::new(Mutex::new(index))));

let addr = (self.address, self.port).to_socket_addrs()?.next().unwrap();
.layer(extract::Extension(Arc::new(Mutex::new(index))))
.layer(
CorsLayer::new()
.allow_methods([http::Method::GET])
.allow_origin(Any),
);

let addr = (self.address, self.port)
.to_socket_addrs()?
.next()
.ok_or_else(|| anyhow!("Failed to get socket addrs"))?;

let handle = Handle::new();

Expand Down

0 comments on commit d662e57

Please sign in to comment.