embed JS console

main
Ziyang Hu 2 years ago
parent 5cc5ce763b
commit f336b24cde

8
Cargo.lock generated

@ -938,9 +938,9 @@ dependencies = [
[[package]]
name = "nalgebra"
version = "0.31.1"
version = "0.31.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9e0a04ce089f9401aac565c740ed30c46291260f27d4911fdbaa6ca65fa3044"
checksum = "da388c517f7c8540918d00f7aed9864c1ead01a14a0cc21513b171857119de12"
dependencies = [
"approx",
"matrixmultiply",
@ -1567,9 +1567,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.85"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44"
checksum = "41feea4228a6f1cd09ec7a3593a682276702cd67b5273544757dae23c096f074"
dependencies = [
"itoa 1.0.4",
"ryu",

@ -1,57 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Makeshift Cozo database client</title>
</head>
<body>
<p>This page is meant to be used with your browser's developer tools.</p>
<script>
function Client(options) {
options.username ||= '';
options.password ||= '';
options.host ||= 'http://127.0.0.1:9070';
const url = options.host + '/text-query';
const headers = {
'Content-Type': 'application/json',
'x-cozo-username': options.username,
'x-cozo-password': options.password
}
async function run(script, params) {
params ||= {};
const resp = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify({
script,
params
})
});
if (resp.ok) {
return await resp.json()
} else {
console.error(await resp.text())
}
}
async function print(script, params) {
const res = await run(script, params);
if (res) {
res.headers ||= [];
console.table(res.rows.map(row => {
let ret = {};
for (let i = 0; i < row.length; ++i) {
ret[res.headers[i] || `(${i})`] = row[i];
}
return ret
}))
}
}
return {run, print}
}
</script>
</body>
</html>

@ -85,6 +85,61 @@ set the environment variable COZO_AUTH and configure clients appropriately."#,
Err(e) => Response::text(format!("{:?}", e)).with_status_code(400),
}
},
(GET) (/) => {
Response::html(r##"
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
<title>Cozo database</title>
</head>
<body>
<p>Cozo HTTP server is running.</p>
<script>
let COZO_AUTH = '';
let LAST_RESP = null;
async function run(script, params) {
const resp = await fetch('/text-query', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-cozo-auth': COZO_AUTH
},
body: JSON.stringify({
script,
params: params || {}
})
});
if (resp.ok) {
const json_resp = await resp.json();
LAST_RESP = json_resp;
if (json_resp) {
json_resp.headers ||= [];
console.table(json_resp.rows.map(row => {
let ret = {};
for (let i = 0; i < row.length; ++i) {
ret[json_resp.headers[i] || `(${i})`] = row[i];
}
return ret
}))
}
} else {
console.error(await resp.text())
}
}
console.log(
`Welcome to the Cozo Makeshift Javascript Console!
You can run your query like this:
await run("YOUR QUERY HERE", {param: value})
The global variables 'COZO_AUTH' and 'LAST_RESP' are available.`);
</script>
</body>
</html>
"##)
},
_ => Response::empty_404()
)
});

Loading…
Cancel
Save