Add a file server example (#8)

Added an example of a prim-dns file server setup, with more intricate pages that make use of other files (like including a Javascript script) and dynamic responses (getting a list of agents in the region).
This commit is contained in:
Anna Puddles
2024-12-04 14:31:07 -05:00
committed by GitHub
parent 775cbe7f45
commit af556cdb07
8 changed files with 183 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
# example prim-dns file server
This is an example setup of a prim-dns file server.
The directory structure seen here would actually be simulated by naming the notecards as follows:
- `/agents/index.js`
- `/agents/index.xhtml`
- `/index.xhtml`
- `/javascript/index.js`
- `/javascript/index.xhtml`
![Example inventory contents](example%20inventory%20contents.png)

View File

@@ -0,0 +1,25 @@
window.addEventListener('load', function() {
fetch('agents.json').then(resp => resp.json()).then(agents => {
document.getElementById('region-name').innerHTML = agents.region;
let table = document.getElementById('agents-table');
agents.agentList.forEach(agent => {
let row = document.createElement('tr');
let col1 = document.createElement('td');
col1.innerHTML = agent.key;
row.append(col1);
let col2 = document.createElement('td');
col2.innerHTML = agent.displayName;
row.append(col2);
let col3 = document.createElement('td');
col3.innerHTML = agent.username;
row.append(col3);
table.append(row);
});
});
});

View File

@@ -0,0 +1,30 @@
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Agents in region</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootswatch@4.5.2/dist/lumen/bootstrap.min.css" integrity="sha384-GzaBcW6yPIfhF+6VpKMjxbTx6tvR/yRd/yJub90CqoIn2Tz4rRXlSpTFYMKHCifX" crossorigin="anonymous"/>
<style>
body {
margin: 1em;
}
</style>
<script src="index.js"/>
</head>
<body>
<p><a href="..">Home</a></p>
<h1>Agents in region <span id="region-name"/>:</h1>
<table class="table table-hover">
<thead>
<tr class="table-primary">
<th>Key</th>
<th>Display name</th>
<th>Username</th>
</tr>
</thead>
<tbody id="agents-table">
</tbody>
</table>
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>prim-dns file server default page</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootswatch@4.5.2/dist/lumen/bootstrap.min.css" integrity="sha384-GzaBcW6yPIfhF+6VpKMjxbTx6tvR/yRd/yJub90CqoIn2Tz4rRXlSpTFYMKHCifX" crossorigin="anonymous"/>
<style>
body {
margin: 1em;
}
</style>
</head>
<body>
<h1>prim-dns file server examples</h1>
<p>These are some examples of what you can do using the prim-dns file server:</p>
<ul>
<li><a href="javascript">Javascript</a></li>
<li><a href="agents">Agents in region</a></li>
</ul>
</body>
</html>

View File

@@ -0,0 +1,5 @@
window.addEventListener('load', function() {
document.querySelectorAll('.color-button').forEach(btn => btn.addEventListener('click', function() {
document.getElementById('color-text').style.color = btn.getAttribute('data-color');
}));
});

View File

@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Javascript example</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootswatch@4.5.2/dist/lumen/bootstrap.min.css" integrity="sha384-GzaBcW6yPIfhF+6VpKMjxbTx6tvR/yRd/yJub90CqoIn2Tz4rRXlSpTFYMKHCifX" crossorigin="anonymous"/>
<style>
body {
margin: 1em;
}
#color-text {
font-size: large;
}
</style>
<script src="index.js"/>
</head>
<body>
<p><a href="..">Home</a></p>
<h1>Javascript example:</h1>
<div id="color-text">Color me!</div>
<button class="color-button btn btn-danger" data-color="red">Red</button>
<button class="color-button btn btn-success" data-color="green">Green</button>
<button class="color-button btn btn-primary" data-color="blue">Blue</button>
</body>
</html>

View File

@@ -0,0 +1,63 @@
/* This script handles requests to special paths which return dynamic responses using LSL. */
string jsonrpc_notification(string method, string params_type, list params)
{
return llList2Json(JSON_OBJECT, ["jsonrpc", "2.0", "method", method, "params", llList2Json(params_type, params)]);
}
jsonrpc_link_notification(integer link, string method, string params_type, list params)
{
llMessageLinked(link, 0, jsonrpc_notification(method, params_type, params), NULL_KEY);
}
default
{
state_entry()
{
/* Register the paths this script will handle instead of the file server. */
jsonrpc_link_notification(LINK_SET, "prim-dns:file-server:register-path", JSON_OBJECT, ["path", "/agents/agents.json"]);
}
link_message(integer sender, integer num, string str, key id)
{
string jsonrpc_method = llJsonGetValue(str, ["method"]);
if (jsonrpc_method == "prim-dns:request")
{
key request_id = (key) llJsonGetValue(str, ["params", "request-id"]);
string method = llJsonGetValue(str, ["params", "method"]);
string headers = llJsonGetValue(str, ["params", "headers"]);
string body = llJsonGetValue(str, ["params", "body"]);
/* Get the path from the headers and use it to determine the response. */
string path = llJsonGetValue(headers, ["x-path-info"]);
if (path == "/agents/agents.json")
{
list agents = llGetAgentList(AGENT_LIST_REGION, []);
integer total_agents = llGetListLength(agents);
list agentList;
integer i;
for (i = 0; i < total_agents; ++i)
{
key agent = llList2Key(agents, i);
agentList += llList2Json(JSON_OBJECT, [
"key", agent,
"username", llGetUsername(agent),
"displayName", llGetDisplayName(agent)
]);
}
string out = llList2Json(JSON_OBJECT, [
"region", llGetRegionName(),
"agentList", llList2Json(JSON_ARRAY, agentList)
]);
jsonrpc_link_notification(sender, "prim-dns:set-content-type", JSON_OBJECT, ["request-id", request_id, "content-type", CONTENT_TYPE_JSON]);
jsonrpc_link_notification(sender, "prim-dns:response", JSON_OBJECT, ["request-id", request_id, "status", 200, "body", out]);
}
}
}
}