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:
12
file server/example/README.md
Normal file
12
file server/example/README.md
Normal 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`
|
||||
|
||||

|
||||
25
file server/example/agents/index.js
Normal file
25
file server/example/agents/index.js
Normal 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);
|
||||
});
|
||||
});
|
||||
});
|
||||
30
file server/example/agents/index.xhtml
Normal file
30
file server/example/agents/index.xhtml
Normal 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>
|
||||
BIN
file server/example/example inventory contents.png
Normal file
BIN
file server/example/example inventory contents.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
22
file server/example/index.xhtml
Normal file
22
file server/example/index.xhtml
Normal 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>
|
||||
5
file server/example/javascript/index.js
Normal file
5
file server/example/javascript/index.js
Normal 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');
|
||||
}));
|
||||
});
|
||||
26
file server/example/javascript/index.xhtml
Normal file
26
file server/example/javascript/index.xhtml
Normal 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>
|
||||
63
file server/example/request handler.lsl
Normal file
63
file server/example/request handler.lsl
Normal 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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user