Enhanced API Server interface

- Used restful patterns for handling server calls (GET/PUT/DELETE)

- Utilized Server interface for saves rather than reimplementing code

- Exposed operations for getting codebase of scripts on the home server and deleting files.

- Added a common response structure
This commit is contained in:
Xi-Lin Yeh
2022-01-20 00:03:07 -08:00
parent 34f18a6000
commit 4a3ee95b63
2 changed files with 120 additions and 28 deletions

View File

@ -12,10 +12,12 @@ async function initialize(win) {
window = win; window = win;
server = http.createServer(async function (req, res) { server = http.createServer(async function (req, res) {
let body = ""; let body = "";
res.setHeader('Content-Type', 'application/json');
req.on("data", (chunk) => { req.on("data", (chunk) => {
body += chunk.toString(); // convert Buffer to string body += chunk.toString(); // convert Buffer to string
}); });
req.on("end", () => { req.on("end", () => {
const providedToken = req.headers?.authorization?.replace('Bearer ', '') ?? ''; const providedToken = req.headers?.authorization?.replace('Bearer ', '') ?? '';
const isValid = providedToken === getAuthenticationToken(); const isValid = providedToken === getAuthenticationToken();
@ -24,8 +26,11 @@ async function initialize(win) {
} else { } else {
log.log('Invalid authentication token'); log.log('Invalid authentication token');
res.writeHead(401); res.writeHead(401);
res.write('Invalid authentication token');
res.end(); res.end(JSON.stringify({
success: false,
msg: 'Invalid authentication token'
}));
return; return;
} }
@ -35,17 +40,68 @@ async function initialize(win) {
} catch (error) { } catch (error) {
log.warn(`Invalid body data`); log.warn(`Invalid body data`);
res.writeHead(400); res.writeHead(400);
res.write('Invalid body data'); res.end(JSON.stringify({
res.end(); success: false,
msg: 'Invalid body data'
}));
return; return;
} }
if (data) {
switch(req.method) {
// Request files
case "GET":
window.webContents.executeJavaScript(`document.getFiles("${data.filename}", "${data.code}")`).then((result) => {
res.end(JSON.stringify({
success: result.res,
msg: result.msg,
data: result.data
}));
});
break;
// Create or update files
// Support POST for VScode implementation
case "POST":
case "PUT":
if (!data) {
log.warn(`Invalid script update request - No data`);
res.writeHead(400);
res.end(JSON.stringify({
success: false,
msg: 'Invalid script update request - No data'
}));
}
window.webContents.executeJavaScript(`document.saveFile("${data.filename}", "${data.code}")`).then((result) => { window.webContents.executeJavaScript(`document.saveFile("${data.filename}", "${data.code}")`).then((result) => {
res.write(result); res.write(result);
if (!result.res) {
//We've encountered an error
res.writeHead(JSON.stringify({
success: result.res,
msg: result.msg,
data: result.data
}));
}
res.end(); res.end();
}); });
break;
// Delete files
case "DELETE":
window.webContents.executeJavaScript(`document.deleteFiles("${data.filename}", "${data.code}")`).then((result) => {
res.end(JSON.stringify({
success: result.res,
msg: result.msg,
data: result.data
}));
});
break;
} }
}); });
}); });

View File

@ -1,10 +1,8 @@
import { Player } from "./Player"; import { Player } from "./Player";
import { isScriptFilename } from "./Script/isScriptFilename";
import { Script } from "./Script/Script";
import { removeLeadingSlash } from "./Terminal/DirectoryHelpers"; import { removeLeadingSlash } from "./Terminal/DirectoryHelpers";
import { Terminal } from "./Terminal"; import { Terminal } from "./Terminal";
import { SnackbarEvents } from "./ui/React/Snackbar"; import { SnackbarEvents } from "./ui/React/Snackbar";
import { IMap } from "./types"; import { IMap, IReturnStatus } from "./types";
import { GetServer } from "./Server/AllServers"; import { GetServer } from "./Server/AllServers";
export function initElectron(): void { export function initElectron(): void {
@ -18,32 +16,70 @@ export function initElectron(): void {
} }
function initWebserver(): void { function initWebserver(): void {
(document as any).saveFile = function (filename: string, code: string): string { interface IReturnWebStatus extends IReturnStatus {
data?: {
[propName: string]: any;
};
}
function normalizeFileName(filename: string): string {
filename = filename.replace(/\/\/+/g, "/"); filename = filename.replace(/\/\/+/g, "/");
filename = removeLeadingSlash(filename); filename = removeLeadingSlash(filename);
if (filename.includes("/")) { if (filename.includes("/")) {
filename = "/" + removeLeadingSlash(filename); filename = "/" + removeLeadingSlash(filename);
} }
return filename;
}
(document as any).getFiles = function (): IReturnWebStatus {
const home = GetServer("home");
if (home === null) {
return {
res: false,
msg: "Home server does not exist."
}
}
return {
res: true,
data: {
files: home.scripts.map((script) => ({
filename: script.filename,
code: script.code
}))
}
}
};
(document as any).deleteFile = function (filename: string): IReturnWebStatus {
filename = normalizeFileName(filename);
const home = GetServer("home");
if (home === null) {
return {
res: false,
msg: "Home server does not exist."
}
}
return home.removeFile(filename);
};
(document as any).saveFile = function (filename: string, code: string): IReturnWebStatus {
filename = normalizeFileName(filename);
code = Buffer.from(code, "base64").toString(); code = Buffer.from(code, "base64").toString();
const home = GetServer("home"); const home = GetServer("home");
if (home === null) return "'home' server not found."; if (home === null) {
if (isScriptFilename(filename)) { return {
//If the current script already exists on the server, overwrite it res: false,
for (let i = 0; i < home.scripts.length; i++) { msg: "Home server does not exist."
if (filename == home.scripts[i].filename) {
home.scripts[i].saveScript(Player, filename, code, "home", home.scripts);
return "written";
} }
} }
const result = home.writeToScriptFile(Player, filename, code);
//If the current script does NOT exist, create a new one return {
const script = new Script(); res: result.success,
script.saveScript(Player, filename, code, "home", home.scripts); data: {
home.scripts.push(script); overwritten: result.overwritten
return "written";
} }
};
return "not a script file";
}; };
} }