2020-07-09 06:19:08 +03:00
|
|
|
import * as Middleware from "./common/middleware";
|
|
|
|
import * as Strings from "./common/strings";
|
|
|
|
import * as Utilities from "./node_common/utilities";
|
|
|
|
import * as Constants from "./node_common/constants";
|
2020-06-17 21:05:13 +03:00
|
|
|
|
2020-07-09 06:19:08 +03:00
|
|
|
import { createPow, ffs } from "@textile/powergate-client";
|
2020-07-01 09:41:54 +03:00
|
|
|
// NOTE(jim):
|
|
|
|
// https://github.com/textileio/js-powergate-client
|
2020-06-17 21:05:13 +03:00
|
|
|
const PowerGate = createPow({ host: Constants.POWERGATE_HOST });
|
2020-02-19 09:30:47 +03:00
|
|
|
|
2020-07-15 10:42:49 +03:00
|
|
|
import { v4 as uuid } from "uuid";
|
|
|
|
// TODO(jim):
|
|
|
|
// CUT THIS DURING THE POSTGRES MOVE.
|
2020-07-09 06:19:08 +03:00
|
|
|
import FS from "fs-extra";
|
|
|
|
import WebSocketServer from "ws";
|
|
|
|
import express from "express";
|
|
|
|
import formidable from "formidable";
|
|
|
|
import next from "next";
|
|
|
|
import bodyParser from "body-parser";
|
|
|
|
import compression from "compression";
|
2020-07-10 06:20:05 +03:00
|
|
|
import path from "path";
|
2020-06-09 21:00:36 +03:00
|
|
|
|
2020-07-15 10:42:49 +03:00
|
|
|
// TODO(jim):
|
|
|
|
// CUT THIS DURING THE POSTGRES MOVE.
|
2020-06-09 21:00:36 +03:00
|
|
|
let client = null;
|
2020-06-17 21:05:13 +03:00
|
|
|
let state = null;
|
2020-06-09 21:00:36 +03:00
|
|
|
|
2020-07-10 10:06:31 +03:00
|
|
|
const production =
|
|
|
|
process.env.NODE_ENV === "production" || process.env.NODE_ENV === "www";
|
|
|
|
const productionWeb = process.env.NODE_ENV === "www";
|
2020-06-17 21:05:13 +03:00
|
|
|
const port = process.env.PORT || 1337;
|
|
|
|
const wsPort = process.env.WS_PORT || 2448;
|
2020-07-10 06:20:05 +03:00
|
|
|
const resetData = process.env.npm_config_reset_data;
|
2020-07-09 06:10:18 +03:00
|
|
|
const app = next({ dev: !production, dir: __dirname, quiet: false });
|
2020-07-09 05:56:22 +03:00
|
|
|
const nextRequestHandler = app.getRequestHandler();
|
2020-07-04 19:30:32 +03:00
|
|
|
|
2020-06-17 21:05:13 +03:00
|
|
|
const setIntervalViewerUpdatesUnsafe = async () => {
|
2020-06-09 21:00:36 +03:00
|
|
|
if (client) {
|
|
|
|
try {
|
2020-07-09 06:19:08 +03:00
|
|
|
console.log("[ prototype ] polling: new viewer state.");
|
2020-06-17 21:05:13 +03:00
|
|
|
state = await Utilities.emitState({
|
|
|
|
state,
|
|
|
|
client,
|
|
|
|
PG: PowerGate,
|
|
|
|
});
|
|
|
|
|
2020-07-09 06:19:08 +03:00
|
|
|
console.log("[ prototype ] polling: new library state.");
|
2020-06-17 21:05:13 +03:00
|
|
|
state = await Utilities.refreshLibrary({
|
|
|
|
state,
|
|
|
|
PG: PowerGate,
|
|
|
|
FFS: ffs,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2020-06-17 12:40:23 +03:00
|
|
|
}
|
2020-06-09 21:00:36 +03:00
|
|
|
|
2020-06-17 21:05:13 +03:00
|
|
|
setTimeout(setIntervalViewerUpdatesUnsafe, Constants.POLLING_RATE);
|
2020-06-09 21:00:36 +03:00
|
|
|
};
|
|
|
|
|
2020-04-09 00:29:13 +03:00
|
|
|
app.prepare().then(async () => {
|
2020-07-09 06:19:08 +03:00
|
|
|
console.log("[ prototype ] initializing ");
|
2020-06-17 21:05:13 +03:00
|
|
|
|
2020-07-15 10:42:49 +03:00
|
|
|
// TODO(jim):
|
|
|
|
// CUT THIS DURING THE POSTGRES MOVE.
|
2020-06-17 21:05:13 +03:00
|
|
|
state = {
|
|
|
|
production,
|
|
|
|
port,
|
|
|
|
wsPort,
|
|
|
|
token: null,
|
|
|
|
library: null,
|
|
|
|
status: null,
|
|
|
|
messageList: null,
|
|
|
|
peersList: null,
|
|
|
|
addrsList: null,
|
|
|
|
info: null,
|
2020-07-01 09:41:54 +03:00
|
|
|
local: null,
|
2020-06-17 21:05:13 +03:00
|
|
|
};
|
2020-04-09 00:29:13 +03:00
|
|
|
|
2020-07-07 01:00:24 +03:00
|
|
|
try {
|
2020-07-15 10:42:49 +03:00
|
|
|
// TODO(jim):
|
|
|
|
// CUT THIS DURING THE POSTGRES MOVE.
|
2020-07-10 06:20:05 +03:00
|
|
|
// NOTE(daniel): Wipe all of the local data when --reset-data flag is added to npm run dev.
|
|
|
|
if (resetData) {
|
|
|
|
await Utilities.resetFileSystem();
|
|
|
|
}
|
2020-07-10 10:06:31 +03:00
|
|
|
|
2020-07-07 01:00:24 +03:00
|
|
|
const updates = await Utilities.refresh({ PG: PowerGate });
|
|
|
|
state = await Utilities.updateStateData(state, updates);
|
2020-07-09 06:19:08 +03:00
|
|
|
console.log("[ prototype ] updated without token");
|
2020-04-09 00:29:13 +03:00
|
|
|
|
2020-07-07 01:00:24 +03:00
|
|
|
// NOTE(jim): This is a configuration folder with all of the client tokens.
|
|
|
|
// TODO(jim): Unnecessary if we use a local and remote postgres.
|
2020-07-09 06:19:08 +03:00
|
|
|
const dirnameData = path.join(__dirname, "/.data");
|
2020-07-07 01:00:24 +03:00
|
|
|
if (!FS.existsSync(dirnameData)) {
|
|
|
|
FS.mkdirSync(dirnameData, { recursive: true });
|
|
|
|
}
|
2020-06-09 21:00:36 +03:00
|
|
|
|
2020-07-07 01:00:24 +03:00
|
|
|
// NOTE(jim): This will create a token for authentication with powergate.
|
|
|
|
// TODO(jim): Roll this up into Postgres instead.
|
2020-07-09 06:19:08 +03:00
|
|
|
const dirnamePowergate = path.join(__dirname, "/.data/powergate-token");
|
2020-07-07 01:00:24 +03:00
|
|
|
if (!FS.existsSync(dirnamePowergate)) {
|
|
|
|
const FFS = await PowerGate.ffs.create();
|
|
|
|
state.token = FFS.token ? FFS.token : null;
|
2020-04-09 00:29:13 +03:00
|
|
|
|
2020-07-07 01:00:24 +03:00
|
|
|
// NOTE(jim): Write a new token file.
|
2020-06-22 09:22:22 +03:00
|
|
|
if (state.token) {
|
2020-07-07 01:00:24 +03:00
|
|
|
FS.writeFileSync(dirnamePowergate, state.token);
|
2020-06-22 09:22:22 +03:00
|
|
|
}
|
2020-07-07 01:00:24 +03:00
|
|
|
} else {
|
2020-07-09 06:19:08 +03:00
|
|
|
state.token = FS.readFileSync(dirnamePowergate, "utf8");
|
2020-07-07 01:00:24 +03:00
|
|
|
}
|
2020-04-09 00:29:13 +03:00
|
|
|
|
2020-07-07 01:00:24 +03:00
|
|
|
if (state.token) {
|
2020-07-09 06:19:08 +03:00
|
|
|
console.log("[ prototype ] powergate token:", state.token);
|
2020-07-07 01:00:24 +03:00
|
|
|
PowerGate.setToken(state.token);
|
|
|
|
}
|
2020-07-01 09:41:54 +03:00
|
|
|
|
2020-07-07 01:00:24 +03:00
|
|
|
const tokenUpdates = await Utilities.refreshWithToken({
|
|
|
|
PG: PowerGate,
|
|
|
|
});
|
|
|
|
state = await Utilities.updateStateData(state, tokenUpdates);
|
2020-07-09 06:19:08 +03:00
|
|
|
console.log("[ prototype ] updated with token");
|
2020-07-07 01:00:24 +03:00
|
|
|
|
|
|
|
// NOTE(jim): Local library retrieval or creation
|
|
|
|
// TODO(jim): Needs to support nested folders in the future.
|
|
|
|
// TODO(jim): May consider a move to buckets.
|
2020-07-09 06:19:08 +03:00
|
|
|
const dirnameLibrary = path.join(__dirname, "/.data/library.json");
|
2020-07-07 01:00:24 +03:00
|
|
|
if (!FS.existsSync(dirnameLibrary)) {
|
|
|
|
const librarySchema = {
|
|
|
|
library: [
|
|
|
|
{
|
|
|
|
...Utilities.createFolder({ id: Constants.FILE_STORAGE_URL }),
|
2020-07-09 06:19:08 +03:00
|
|
|
file: "Files",
|
|
|
|
name: "Files",
|
2020-07-07 03:49:15 +03:00
|
|
|
},
|
2020-07-07 01:00:24 +03:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
FS.writeFileSync(dirnameLibrary, JSON.stringify(librarySchema));
|
|
|
|
state.library = librarySchema.library;
|
|
|
|
} else {
|
2020-07-09 06:19:08 +03:00
|
|
|
const parsedLibrary = FS.readFileSync(dirnameLibrary, "utf8");
|
2020-07-07 01:00:24 +03:00
|
|
|
state.library = JSON.parse(parsedLibrary).library;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE(jim): Local settings retrieval or creation
|
|
|
|
// TODO(jim): Move this to postgres later.
|
2020-07-07 19:54:44 +03:00
|
|
|
const dirnameLocalSettings = path.join(
|
2020-07-07 01:00:24 +03:00
|
|
|
__dirname,
|
2020-07-09 06:19:08 +03:00
|
|
|
"/.data/local-settings.json"
|
2020-07-07 01:00:24 +03:00
|
|
|
);
|
2020-07-07 19:54:44 +03:00
|
|
|
if (!FS.existsSync(dirnameLocalSettings)) {
|
2020-07-07 01:00:24 +03:00
|
|
|
const localSettingsSchema = {
|
|
|
|
local: {
|
|
|
|
photo: null,
|
|
|
|
name: `node-${uuid()}`,
|
|
|
|
settings_deals_auto_approve: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
FS.writeFileSync(
|
2020-07-07 19:54:44 +03:00
|
|
|
dirnameLocalSettings,
|
2020-07-07 01:00:24 +03:00
|
|
|
JSON.stringify(localSettingsSchema)
|
2020-07-07 03:49:15 +03:00
|
|
|
);
|
2020-07-07 01:00:24 +03:00
|
|
|
state.local = localSettingsSchema.local;
|
|
|
|
} else {
|
2020-07-09 06:19:08 +03:00
|
|
|
const parsedLocal = FS.readFileSync(dirnameLocalSettings, "utf8");
|
2020-07-07 01:00:24 +03:00
|
|
|
state.local = JSON.parse(parsedLocal).local;
|
2020-06-09 21:00:36 +03:00
|
|
|
}
|
2020-07-07 01:00:24 +03:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
console.log('[ prototype ] "/" -- WILL REDIRECT TO /SYSTEM ');
|
|
|
|
console.log(
|
2020-07-09 06:19:08 +03:00
|
|
|
"[ prototype ] SLATE WILL NOT RUN LOCALLY UNTIL YOU HAVE "
|
2020-07-07 01:00:24 +03:00
|
|
|
);
|
2020-07-09 06:19:08 +03:00
|
|
|
console.log("[ prototype ] PROPERLY CONFIGURED POWERGATE AND ");
|
2020-07-07 01:00:24 +03:00
|
|
|
console.log(
|
2020-07-09 06:19:08 +03:00
|
|
|
"[ prototype ] CONNECTED TO THE FILECOIN NETWORK (DEVNET/TESTNET) "
|
2020-07-07 01:00:24 +03:00
|
|
|
);
|
2020-06-08 09:45:53 +03:00
|
|
|
}
|
|
|
|
|
2020-02-19 09:30:47 +03:00
|
|
|
const server = express();
|
2020-07-10 10:06:31 +03:00
|
|
|
// TODO(jim): Temporarily disable web sockets for web production
|
|
|
|
// since we have no web version of Slate yet.
|
|
|
|
if (!productionWeb) {
|
|
|
|
const WSS = new WebSocketServer.Server({ port: wsPort });
|
2020-06-09 21:00:36 +03:00
|
|
|
|
2020-07-10 10:06:31 +03:00
|
|
|
WSS.on("connection", (s) => {
|
|
|
|
// TODO(jim): Suppport more than one client.
|
|
|
|
client = s;
|
2020-06-09 21:00:36 +03:00
|
|
|
|
2020-07-10 10:06:31 +03:00
|
|
|
s.on("close", function() {
|
|
|
|
s.send(JSON.stringify({ action: null, data: "closed" }));
|
|
|
|
});
|
2020-06-09 21:00:36 +03:00
|
|
|
|
2020-07-10 10:06:31 +03:00
|
|
|
s.send(JSON.stringify({ action: null, data: "connected" }));
|
|
|
|
});
|
|
|
|
}
|
2020-02-19 09:30:47 +03:00
|
|
|
|
2020-07-10 10:06:31 +03:00
|
|
|
if (productionWeb) {
|
2020-02-19 09:30:47 +03:00
|
|
|
server.use(compression());
|
|
|
|
}
|
|
|
|
|
|
|
|
server.use(Middleware.CORS);
|
2020-07-09 06:19:08 +03:00
|
|
|
server.use("/public", express.static("public"));
|
2020-02-19 09:30:47 +03:00
|
|
|
server.use(bodyParser.json());
|
|
|
|
server.use(
|
|
|
|
bodyParser.urlencoded({
|
|
|
|
extended: false,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2020-07-09 06:19:08 +03:00
|
|
|
server.post("/_/viewer", async (req, res) => {
|
2020-06-17 21:05:13 +03:00
|
|
|
let data = state;
|
|
|
|
|
2020-07-10 10:06:31 +03:00
|
|
|
if (!productionWeb) {
|
2020-06-17 21:05:13 +03:00
|
|
|
const updates = await Utilities.refresh({ PG: PowerGate });
|
|
|
|
const updatesWithToken = await Utilities.refreshWithToken({
|
|
|
|
PG: PowerGate,
|
|
|
|
});
|
|
|
|
data = await Utilities.updateStateData(data, {
|
|
|
|
...updates,
|
|
|
|
...updatesWithToken,
|
|
|
|
});
|
2020-06-09 21:00:36 +03:00
|
|
|
}
|
|
|
|
|
2020-06-17 21:05:13 +03:00
|
|
|
return res.status(200).send({ success: true, data });
|
2020-02-19 09:30:47 +03:00
|
|
|
});
|
|
|
|
|
2020-07-09 06:19:08 +03:00
|
|
|
server.post("/_/deals/storage", async (req, res) => {
|
2020-06-09 21:00:36 +03:00
|
|
|
if (Strings.isEmpty(req.body.src)) {
|
|
|
|
return res.status(500).send({ success: false });
|
|
|
|
}
|
|
|
|
|
2020-06-17 21:05:13 +03:00
|
|
|
const localPath = `.${req.body.src}`;
|
2020-06-09 21:00:36 +03:00
|
|
|
const buffer = FS.readFileSync(localPath);
|
|
|
|
const { cid } = await PowerGate.ffs.addToHot(buffer);
|
|
|
|
const { jobId } = await PowerGate.ffs.pushConfig(cid);
|
|
|
|
|
|
|
|
// TODO(jim): Refactor this so we repeat this less often.
|
|
|
|
let write = false;
|
2020-06-17 21:05:13 +03:00
|
|
|
for (let i = 0; i < state.library.length; i++) {
|
|
|
|
for (let j = 0; j < state.library[i].children.length; j++) {
|
|
|
|
if (localPath === state.library[i].children[j].id) {
|
|
|
|
state.library[i].children[j].job_id = jobId;
|
|
|
|
state.library[i].children[j].cid = cid;
|
|
|
|
state.library[i].children[j].storage_status = 1;
|
2020-06-09 21:00:36 +03:00
|
|
|
write = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 09:41:54 +03:00
|
|
|
// NOTE(jim): Writes the updated deal state.
|
2020-06-09 21:00:36 +03:00
|
|
|
if (write) {
|
2020-07-09 06:19:08 +03:00
|
|
|
const dirnameLibrary = path.join(__dirname, "/.data/library.json");
|
2020-07-07 03:49:15 +03:00
|
|
|
FS.writeFileSync(
|
2020-07-07 19:54:44 +03:00
|
|
|
dirnameLibrary,
|
2020-07-07 03:49:15 +03:00
|
|
|
JSON.stringify({ library: state.library })
|
|
|
|
);
|
2020-06-09 21:00:36 +03:00
|
|
|
}
|
|
|
|
|
2020-06-17 21:05:13 +03:00
|
|
|
state = await Utilities.emitState({ state, client, PG: PowerGate });
|
2020-06-09 21:00:36 +03:00
|
|
|
return res.status(200).send({ success: true, cid, jobId });
|
|
|
|
});
|
|
|
|
|
2020-07-09 06:19:08 +03:00
|
|
|
server.post("/_/storage/:file", async (req, res) => {
|
2020-06-17 21:05:13 +03:00
|
|
|
const form = formidable({
|
|
|
|
multiples: true,
|
|
|
|
uploadDir: Constants.FILE_STORAGE_URL,
|
2020-06-08 09:45:53 +03:00
|
|
|
});
|
|
|
|
|
2020-06-09 21:00:36 +03:00
|
|
|
form.parse(req, async (error, fields, files) => {
|
|
|
|
if (error) {
|
|
|
|
return res.status(500).send({ error });
|
|
|
|
} else {
|
2020-06-17 21:05:13 +03:00
|
|
|
// TODO(jim): Need to support other file types.
|
2020-06-09 21:00:36 +03:00
|
|
|
if (!files.image) {
|
2020-07-09 06:19:08 +03:00
|
|
|
console.error("[ prototype ] File type unspported", files);
|
2020-07-07 03:49:15 +03:00
|
|
|
return res
|
|
|
|
.status(500)
|
2020-07-09 06:19:08 +03:00
|
|
|
.send({ error: "File type unsupported", files });
|
2020-06-09 21:00:36 +03:00
|
|
|
}
|
2020-06-08 09:45:53 +03:00
|
|
|
|
2020-06-09 21:00:36 +03:00
|
|
|
const newPath = form.uploadDir + req.params.file;
|
2020-07-10 10:06:31 +03:00
|
|
|
FS.rename(files.image.path, newPath, function(err) {});
|
2020-06-09 21:00:36 +03:00
|
|
|
|
2020-06-17 21:05:13 +03:00
|
|
|
const localFile = Utilities.createFile({
|
|
|
|
id: newPath,
|
|
|
|
data: files.image,
|
|
|
|
});
|
2020-06-08 09:45:53 +03:00
|
|
|
|
2020-06-17 21:05:13 +03:00
|
|
|
// TODO(jim): Messy, refactor.
|
2020-06-09 21:00:36 +03:00
|
|
|
let pushed = false;
|
2020-06-17 21:05:13 +03:00
|
|
|
for (let i = 0; i < state.library.length; i++) {
|
2020-06-09 21:00:36 +03:00
|
|
|
if (!pushed) {
|
2020-06-17 21:05:13 +03:00
|
|
|
state.library[i].children.push(localFile);
|
2020-06-09 21:00:36 +03:00
|
|
|
pushed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 09:41:54 +03:00
|
|
|
// NOTE(jim): Writes the added file.
|
2020-06-09 21:00:36 +03:00
|
|
|
if (pushed) {
|
2020-07-09 06:19:08 +03:00
|
|
|
const dirnameLibrary = path.join(__dirname, "/.data/library.json");
|
2020-07-07 03:49:15 +03:00
|
|
|
FS.writeFileSync(
|
2020-07-07 19:54:44 +03:00
|
|
|
dirnameLibrary,
|
2020-07-07 03:49:15 +03:00
|
|
|
JSON.stringify({ library: state.library })
|
|
|
|
);
|
2020-06-09 21:00:36 +03:00
|
|
|
}
|
|
|
|
|
2020-06-17 21:05:13 +03:00
|
|
|
state = await Utilities.emitState({
|
|
|
|
state,
|
|
|
|
client,
|
|
|
|
PG: PowerGate,
|
|
|
|
});
|
|
|
|
|
2020-06-09 21:00:36 +03:00
|
|
|
return res.status(200).send({ success: true, file: localFile });
|
|
|
|
}
|
2020-06-08 09:45:53 +03:00
|
|
|
});
|
2020-06-09 21:00:36 +03:00
|
|
|
});
|
|
|
|
|
2020-07-09 06:19:08 +03:00
|
|
|
server.post("/_/upload/avatar", async (req, res) => {
|
2020-06-17 21:05:13 +03:00
|
|
|
const form = formidable({
|
|
|
|
multiples: true,
|
|
|
|
uploadDir: Constants.AVATAR_STORAGE_URL,
|
2020-06-08 09:45:53 +03:00
|
|
|
});
|
|
|
|
|
2020-06-09 21:00:36 +03:00
|
|
|
form.parse(req, async (error, fields, files) => {
|
2020-06-08 09:45:53 +03:00
|
|
|
if (error) {
|
|
|
|
return res.status(500).send({ error });
|
|
|
|
} else {
|
2020-07-01 09:41:54 +03:00
|
|
|
const newName = `avatar-${uuid()}.png`;
|
|
|
|
const newPath = form.uploadDir + newName;
|
2020-07-10 10:06:31 +03:00
|
|
|
FS.rename(files.image.path, newPath, function(err) {});
|
2020-06-08 09:45:53 +03:00
|
|
|
|
2020-07-01 09:41:54 +03:00
|
|
|
// NOTE(jim): updates avatar photo.
|
2020-07-09 06:10:18 +03:00
|
|
|
state.local.photo = __dirname + `/static/system/${newName}`;
|
2020-07-07 19:54:44 +03:00
|
|
|
const dirnameLocalSettings = path.join(
|
|
|
|
__dirname,
|
2020-07-09 06:19:08 +03:00
|
|
|
"/.data/local-settings.json"
|
2020-07-07 19:54:44 +03:00
|
|
|
);
|
2020-07-07 03:49:15 +03:00
|
|
|
FS.writeFileSync(
|
2020-07-07 19:54:44 +03:00
|
|
|
dirnameLocalSettings,
|
2020-07-07 03:49:15 +03:00
|
|
|
JSON.stringify({ local: { ...state.local } })
|
|
|
|
);
|
2020-07-01 09:41:54 +03:00
|
|
|
|
2020-06-17 21:05:13 +03:00
|
|
|
state = await Utilities.emitState({
|
|
|
|
state,
|
|
|
|
client,
|
|
|
|
PG: PowerGate,
|
|
|
|
});
|
2020-07-01 09:41:54 +03:00
|
|
|
|
2020-06-08 09:45:53 +03:00
|
|
|
return res.status(200).send({ success: true });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-09 06:19:08 +03:00
|
|
|
server.post("/_/settings", async (req, res) => {
|
2020-06-08 09:45:53 +03:00
|
|
|
let data;
|
|
|
|
try {
|
2020-06-09 21:00:36 +03:00
|
|
|
data = await PowerGate.ffs.setDefaultConfig(req.body.config);
|
2020-06-08 09:45:53 +03:00
|
|
|
} catch (e) {
|
|
|
|
return res.status(500).send({ error: e.message });
|
|
|
|
}
|
2020-06-04 09:26:20 +03:00
|
|
|
|
2020-06-17 21:05:13 +03:00
|
|
|
state = await Utilities.emitState({ state, client, PG: PowerGate });
|
2020-06-04 09:26:20 +03:00
|
|
|
return res.status(200).send({ success: true, data });
|
|
|
|
});
|
|
|
|
|
2020-07-09 06:19:08 +03:00
|
|
|
server.post("/_/local-settings", async (req, res) => {
|
2020-07-01 09:41:54 +03:00
|
|
|
state.local = { ...state.local, ...req.body.local };
|
2020-07-07 19:54:44 +03:00
|
|
|
const dirnameLocalSettings = path.join(
|
|
|
|
__dirname,
|
2020-07-09 06:19:08 +03:00
|
|
|
"/.data/local-settings.json"
|
2020-07-07 19:54:44 +03:00
|
|
|
);
|
2020-07-07 03:49:15 +03:00
|
|
|
FS.writeFileSync(
|
2020-07-07 19:54:44 +03:00
|
|
|
dirnameLocalSettings,
|
2020-07-07 03:49:15 +03:00
|
|
|
JSON.stringify({ local: { ...state.local } })
|
|
|
|
);
|
2020-07-01 09:41:54 +03:00
|
|
|
state = await Utilities.emitState({ state, client, PG: PowerGate });
|
|
|
|
return res.status(200).send({ success: true });
|
|
|
|
});
|
|
|
|
|
2020-07-09 06:19:08 +03:00
|
|
|
server.post("/_/wallet/create", async (req, res) => {
|
2020-04-09 00:29:13 +03:00
|
|
|
let data;
|
|
|
|
try {
|
2020-07-07 03:49:15 +03:00
|
|
|
data = await PowerGate.ffs.newAddr(
|
|
|
|
req.body.name,
|
|
|
|
req.body.type,
|
|
|
|
req.body.makeDefault
|
|
|
|
);
|
2020-04-09 00:29:13 +03:00
|
|
|
} catch (e) {
|
|
|
|
return res.status(500).send({ error: e.message });
|
|
|
|
}
|
2020-02-19 09:30:47 +03:00
|
|
|
|
2020-06-17 21:05:13 +03:00
|
|
|
state = await Utilities.emitState({ state, client, PG: PowerGate });
|
2020-04-09 00:29:13 +03:00
|
|
|
return res.status(200).send({ success: true, data });
|
2020-02-19 09:30:47 +03:00
|
|
|
});
|
|
|
|
|
2020-07-09 06:19:08 +03:00
|
|
|
server.post("/_/wallet/send", async (req, res) => {
|
2020-04-09 00:29:13 +03:00
|
|
|
let data;
|
|
|
|
try {
|
2020-07-07 03:49:15 +03:00
|
|
|
data = await PowerGate.ffs.sendFil(
|
|
|
|
req.body.source,
|
|
|
|
req.body.target,
|
|
|
|
req.body.amount
|
|
|
|
);
|
2020-04-09 00:29:13 +03:00
|
|
|
} catch (e) {
|
|
|
|
return res.status(500).send({ error: e.message });
|
2020-02-19 09:30:47 +03:00
|
|
|
}
|
|
|
|
|
2020-06-17 21:05:13 +03:00
|
|
|
state = await Utilities.emitState({ state, client, PG: PowerGate });
|
2020-07-07 03:49:15 +03:00
|
|
|
return res
|
|
|
|
.status(200)
|
|
|
|
.send({ success: true, data: { ...data, ...req.body } });
|
2020-02-19 09:30:47 +03:00
|
|
|
});
|
|
|
|
|
2020-07-16 08:48:51 +03:00
|
|
|
server.get("/application", async (req, res) => {
|
|
|
|
return app.render(req, res, "/application", {
|
2020-06-17 21:05:13 +03:00
|
|
|
wsPort,
|
|
|
|
});
|
2020-02-19 09:30:47 +03:00
|
|
|
});
|
|
|
|
|
2020-07-16 08:48:51 +03:00
|
|
|
server.get("/", async (req, res) => {
|
|
|
|
return app.render(req, res, "/", { hide: productionWeb });
|
|
|
|
});
|
|
|
|
|
2020-07-09 06:19:08 +03:00
|
|
|
server.get("*", async (req, res) => {
|
2020-02-19 09:30:47 +03:00
|
|
|
return nextRequestHandler(req, res, req.url);
|
|
|
|
});
|
|
|
|
|
2020-06-09 21:00:36 +03:00
|
|
|
server.listen(port, async (err) => {
|
2020-02-19 09:30:47 +03:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2020-06-17 21:05:13 +03:00
|
|
|
console.log(`[ prototype ] client: http://localhost:${port}`);
|
|
|
|
console.log(`[ prototype ] constants:`, Constants);
|
2020-06-09 21:00:36 +03:00
|
|
|
|
2020-07-10 10:06:31 +03:00
|
|
|
if (!productionWeb) {
|
2020-06-22 14:44:28 +03:00
|
|
|
await setIntervalViewerUpdatesUnsafe();
|
|
|
|
}
|
2020-02-19 09:30:47 +03:00
|
|
|
});
|
|
|
|
});
|