godprotocol 1.2.15 → 1.2.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/utils/create_server.js +62 -60
package/package.json
CHANGED
package/utils/create_server.js
CHANGED
|
@@ -3,11 +3,9 @@ import { generate_random_string } from 'generalised-datastore/utils/functions.js
|
|
|
3
3
|
|
|
4
4
|
let PORT = Number(process.env.PORT) || 1408, account_uids = {};
|
|
5
5
|
|
|
6
|
-
export const handle_routes =
|
|
7
|
-
let body = '';
|
|
8
|
-
|
|
6
|
+
export const handle_routes = async (req, res, manager, app) => {
|
|
9
7
|
try {
|
|
10
|
-
//
|
|
8
|
+
// CORS
|
|
11
9
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
12
10
|
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
13
11
|
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
|
|
@@ -17,7 +15,7 @@ export const handle_routes = async (req, res, manager, app) => {
|
|
|
17
15
|
return res.end();
|
|
18
16
|
}
|
|
19
17
|
|
|
20
|
-
// GET
|
|
18
|
+
// GET pages
|
|
21
19
|
if (req.method === 'GET') {
|
|
22
20
|
if (req.url === '/') {
|
|
23
21
|
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
@@ -29,70 +27,74 @@ export const handle_routes = async (req, res, manager, app) => {
|
|
|
29
27
|
return res.end(set_page(req.url.slice(1), manager));
|
|
30
28
|
}
|
|
31
29
|
|
|
32
|
-
|
|
30
|
+
// hand over untouched
|
|
31
|
+
return app ? app(req, res) : res.writeHead(404).end('Not Found');
|
|
33
32
|
}
|
|
34
33
|
|
|
35
|
-
//
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
// Only custom POST endpoints handled here
|
|
35
|
+
if (['/create_account', '/load', '/run', '/parse', '/oracle'].includes(req.url)) {
|
|
36
|
+
let body = '';
|
|
37
|
+
req.on('data', chunk => { body += chunk; });
|
|
38
|
+
await new Promise(resolve => req.on('end', resolve));
|
|
40
39
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
if (!body) {
|
|
41
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
42
|
+
return res.end(JSON.stringify({ error: 'Request body cannot be empty' }));
|
|
43
|
+
}
|
|
45
44
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
if (
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
let newAcc = await manager.add_account(data.name, { password: data.password });
|
|
61
|
-
if (newAcc) {
|
|
62
|
-
const uid = generate_random_string(16, 'alnum');
|
|
63
|
-
account_uids[uid] = data.name;
|
|
64
|
-
result = { uid, message: 'Account created successfully.' };
|
|
45
|
+
const data = JSON.parse(body);
|
|
46
|
+
let result;
|
|
47
|
+
|
|
48
|
+
switch (req.url) {
|
|
49
|
+
case '/create_account': {
|
|
50
|
+
let acc = await manager.get_account(data.name);
|
|
51
|
+
if (acc) {
|
|
52
|
+
if (!await acc.verify(data.password)) {
|
|
53
|
+
result = { message: 'Incorrect password.' };
|
|
54
|
+
} else {
|
|
55
|
+
const uid = generate_random_string(16, 'alnum');
|
|
56
|
+
account_uids[uid] = data.name;
|
|
57
|
+
result = { uid, message: 'Account signed-in successfully.' };
|
|
58
|
+
}
|
|
65
59
|
} else {
|
|
66
|
-
|
|
60
|
+
let newAcc = await manager.add_account(data.name, { password: data.password });
|
|
61
|
+
if (newAcc) {
|
|
62
|
+
const uid = generate_random_string(16, 'alnum');
|
|
63
|
+
account_uids[uid] = data.name;
|
|
64
|
+
result = { uid, message: 'Account created successfully.' };
|
|
65
|
+
} else {
|
|
66
|
+
result = { message: 'Account creation failed.' };
|
|
67
|
+
}
|
|
67
68
|
}
|
|
69
|
+
break;
|
|
68
70
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
71
|
+
|
|
72
|
+
case '/oracle':
|
|
73
|
+
result = await manager.oracle.call(data);
|
|
74
|
+
break;
|
|
75
|
+
|
|
76
|
+
case '/load':
|
|
77
|
+
case '/run':
|
|
78
|
+
case '/parse': {
|
|
79
|
+
const accountName = account_uids[data.account];
|
|
80
|
+
if (!accountName) {
|
|
81
|
+
result = { message: `Account ${data.account} not found` };
|
|
82
|
+
} else {
|
|
83
|
+
const accountObj = await manager.get_account(accountName);
|
|
84
|
+
if (req.url === '/load') result = await accountObj.load(data);
|
|
85
|
+
if (req.url === '/run') result = await accountObj.run(data);
|
|
86
|
+
if (req.url === '/parse') result = await accountObj.parse(data);
|
|
79
87
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (req.url === '/load'){
|
|
83
|
-
result = await account_obj.load(data);
|
|
84
|
-
}else if (req.url === '/run'){
|
|
85
|
-
result = await account_obj.run(data);
|
|
86
|
-
}else if (req.url === '/parse'){
|
|
87
|
-
result = await account_obj.parse(data);
|
|
88
|
-
}else {
|
|
89
|
-
return app? app(req,res): res.writeHead(404).end('Not Found');
|
|
88
|
+
break;
|
|
90
89
|
}
|
|
91
|
-
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
93
|
+
return res.end(JSON.stringify(result));
|
|
92
94
|
}
|
|
93
95
|
|
|
94
|
-
|
|
95
|
-
return res.
|
|
96
|
+
// Everything else → forward to express app
|
|
97
|
+
return app ? app(req, res) : res.writeHead(404).end('Not Found');
|
|
96
98
|
|
|
97
99
|
} catch (err) {
|
|
98
100
|
console.error('Handler error:', err);
|
|
@@ -108,10 +110,10 @@ const create_server = (settings) => {
|
|
|
108
110
|
|
|
109
111
|
port = port || PORT;
|
|
110
112
|
|
|
111
|
-
let handler = async (req, res) => await handle_routes(req, res, manager, manager.options.app)
|
|
113
|
+
let handler = async (req, res) => await handle_routes(req, res, manager, manager.options.app);
|
|
112
114
|
|
|
113
115
|
return handler;
|
|
114
116
|
};
|
|
115
117
|
|
|
116
118
|
export default create_server;
|
|
117
|
-
export { PORT };
|
|
119
|
+
export { PORT };
|