Skip to content

Commit 6e9cba1

Browse files
committed
for dylan
1 parent 0d7b9a7 commit 6e9cba1

File tree

6 files changed

+78
-39
lines changed

6 files changed

+78
-39
lines changed

api/run.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const express = require('express')
55
* @returns {Object} - The router object.
66
*/
77
const init = (node) => {
8-
8+
99
const runKey = async (req, res) => {
1010
console.log("run called");
1111
let body = req.body;
@@ -38,7 +38,7 @@ const init = (node) => {
3838
res.status(500).end();
3939
}
4040
};
41-
41+
4242
const findKey = async (req, res) => {
4343
console.log("find called", req.params);
4444
try {
@@ -66,9 +66,5 @@ const init = (node) => {
6666
router.post("/key/:pk", runKey);
6767
return router;
6868
};
69-
process.on('unhandledException', (reason, promise) => {
70-
console.log('Unhandled Rejection at:', reason.stack || reason)
71-
// Recommended: send the information to sentry.io
72-
// or whatever crash reporting service you use
73-
})
69+
7470
module.exports = init;

api/server.js

+38-24
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,60 @@
11
/**
22
* Loads the environment variables and starts the server.
33
*/
4-
require("dotenv").config();
5-
const express = require("express");
6-
const tasks = require("./tasks.js");
7-
const run = require("./run.js");
8-
const nodes = require("./nodes.js");
9-
const fs = require("fs");
10-
const node = require("hyper-ipc-secure")();
4+
require("dotenv").config()
5+
const express = require("express")
6+
const tasks = require("./tasks.js")
7+
const run = require("./run.js")
8+
const nodes = require("./nodes.js")
9+
const stores = require("./stores.js")
10+
const fs = require("fs")
11+
12+
13+
let node = require("hyper-ipc-secure")()
14+
15+
async function exitHandler(options, exitCode) {
16+
console.log('restarted node')
17+
node = require("hyper-ipc-secure")()
18+
}
19+
20+
process.on('exit', exitHandler.bind(null, 'cleanup'))
21+
process.on('uncaughtException', exitHandler.bind(null, 'uncaughtException'))
22+
process.on('unhandledRejection', exitHandler.bind(null, 'unhandledRejection'))
23+
1124
/**
1225
* Creates a folder if it doesn't exist.
1326
* @param {string} folderPath - The path of the folder.
1427
*/
1528
function createFolderIfNotExists(folderPath) {
1629
if (!fs.existsSync(folderPath)) {
1730
// If the folder doesn't exist, create it
18-
fs.mkdirSync(folderPath);
31+
fs.mkdirSync(folderPath)
1932
console.log(`Folder "${folderPath}" created.`)
2033
} else {
2134
console.log(`Folder "${folderPath}" already exists.`)
2235
}
2336
}
24-
createFolderIfNotExists("saves");
25-
const app = express();
26-
const port = process.env.PORT || 3011;
27-
app.use(express.json());
37+
createFolderIfNotExists("saves")
38+
const app = express()
39+
const port = process.env.PORT || 3011
40+
app.use(express.json())
2841
app.use(express.urlencoded({
2942
extended: true
30-
}));
43+
}))
3144
app.use((function (req, res, next) {
32-
res.setHeader("Access-Control-Allow-Origin", "*");
33-
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE");
34-
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With, content-type");
35-
res.setHeader("Access-Control-Allow-Credentials", true);
45+
res.setHeader("Access-Control-Allow-Origin", "*")
46+
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE")
47+
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With, content-type")
48+
res.setHeader("Access-Control-Allow-Credentials", true)
3649
next()
37-
}));
38-
const vault = nodes(node);
50+
}))
51+
const vault = nodes(node)
3952
console.log(vault)
40-
app.use("/vault", vault);
41-
app.use("/task", tasks(node));
42-
app.use("/run", run(node));
43-
app.use(express.static('public'));
53+
app.use("/vault", vault)
54+
app.use("/task", tasks(node))
55+
app.use("/stores", stores)
56+
app.use("/run", run(node))
57+
app.use(express.static('public'))
4458
app.listen(port, (() => {
4559
console.log(`Example app listening on port ${port}`)
46-
}));
60+
}))

api/stores.js

+33-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const Hyperswarm = require('hyperswarm')
55
const dbs = []
66

77
const prepDb = async (name) => {
8-
if(dbs[name]) return dbs[name];
8+
if (dbs[name]) return dbs[name];
99
const core = new Hypercore('./' + name)
1010
await core.ready()
1111
const db = new Hyperbee(core, { keyEncoding: 'utf-8', valueEncoding: 'binary' })
@@ -16,4 +16,35 @@ const prepDb = async (name) => {
1616
return db
1717
}
1818

19-
module.exports = { prepDb }
19+
const init = (node) => {
20+
21+
const save = async (req, res) => {
22+
const db = await prepDb('./' + req.params.db)
23+
console.log(req.body, req.params)
24+
await db.put(req.params.name, JSON.stringify(req.body))
25+
res.write("{success:true}")
26+
res.status(200).end()
27+
};
28+
29+
const load = async (req, res) => {
30+
const db = await prepDb('./' + req.params.db)
31+
try {
32+
const lookup = await db.get(req.params.name);
33+
console.log(lookup.value.toString('utf-8'))
34+
res.write(lookup.value)
35+
res.status(200).end();
36+
} catch (e) {
37+
console.error(e);
38+
res.write(JSON.stringify({
39+
error: e
40+
}));
41+
res.status(500).end();
42+
}
43+
};
44+
router.get("/load/:name", load);
45+
router.post("/save/:name", save);
46+
console.log('stores')
47+
return router;
48+
}
49+
50+
module.exports = init

api/tasks.js

-2
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ const init = (node) => {
6767
};
6868
console.log('tasks')
6969
const router = express.Router();
70-
router.get("/load/:name", loadTask);
7170
router.post("/run/:pk/:actionname", runTask);
72-
router.post("/save/:name", saveTask);
7371
return router;
7472
};
7573

ui/src/routes/flow/+page.svelte

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
);
2323
const key = await keyFetch.json();
2424
const subFetch = await fetch(
25-
`http://localhost:3011/vault/getSub/${newName}`,
25+
`http://localhost:3011/vault/getSub/call-${newName}`,
2626
{
2727
method: "POST",
2828
body: JSON.stringify({ key }),
@@ -94,7 +94,7 @@
9494
};
9595
9696
const save = async (incalls, taskName) => {
97-
const url = `http://localhost:3011/task/save/${taskName}`
97+
const url = `http://localhost:3011/store/save/task/${taskName}`
9898
const fetched = await fetch(url, {
9999
headers: { "Content-Type": "application/json" },
100100
method: "POST",
@@ -103,7 +103,7 @@
103103
console.log(calls)
104104
};
105105
const load = async (taskName) => {
106-
const url = `http://localhost:3011/task/load/${taskName}`
106+
const url = `http://localhost:3011/store/load/task/${taskName}`
107107
const fetched = await fetch(url, { method: "GET" })
108108
const json = await fetched.json();
109109
console.log({json});

ui/src/routes/keys/components/Host.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
})
3030
const key = await keyFetch.json();
3131
32-
const subFetch = await fetch(`http://localhost:3011/vault/getSub/${keyName}`, {
32+
const subFetch = await fetch(`http://localhost:3011/vault/getSub/call-${keyName}`, {
3333
method: "POST",
3434
body: JSON.stringify({ key }),
3535
headers: {

0 commit comments

Comments
 (0)