aboutsummaryrefslogtreecommitdiff
path: root/src/server/ApiManagers/UtilManager.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/ApiManagers/UtilManager.ts')
-rw-r--r--src/server/ApiManagers/UtilManager.ts87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/server/ApiManagers/UtilManager.ts b/src/server/ApiManagers/UtilManager.ts
new file mode 100644
index 000000000..dbf274e93
--- /dev/null
+++ b/src/server/ApiManagers/UtilManager.ts
@@ -0,0 +1,87 @@
+import ApiManager, { Registration } from "./ApiManager";
+import { Method } from "../RouteManager";
+import { exec } from 'child_process';
+import { command_line } from "../ActionUtilities";
+import RouteSubscriber from "../RouteSubscriber";
+import { red } from "colors";
+import { main } from "../../scraping/buxton/node_scraper";
+
+export default class UtilManager extends ApiManager {
+
+ protected initialize(register: Registration): void {
+
+ register({
+ method: Method.GET,
+ subscription: new RouteSubscriber("environment").add("key"),
+ secureHandler: ({ req, res }) => {
+ const { key } = req.params;
+ const value = process.env[key];
+ if (!value) {
+ console.log(red(`process.env.${key} is not defined.`));
+ }
+ return res.send(value);
+ }
+ });
+
+ register({
+ method: Method.GET,
+ subscription: "/pull",
+ secureHandler: async ({ res }) => {
+ return new Promise<void>(resolve => {
+ exec('"C:\\Program Files\\Git\\git-bash.exe" -c "git pull"', err => {
+ if (err) {
+ res.send(err.message);
+ return;
+ }
+ res.redirect("/");
+ resolve();
+ });
+ });
+ }
+ });
+
+ register({
+ method: Method.GET,
+ subscription: "/buxton",
+ secureHandler: async ({ res }) => {
+ const cwd = './src/scraping/buxton';
+
+ const onResolved = (stdout: string) => { console.log(stdout); res.redirect("/"); };
+ const onRejected = (err: any) => { console.error(err.message); res.send(err); };
+ const tryPython3 = (reason: any) => {
+ console.log("Initial scraper failed for the following reason:");
+ console.log(red(reason.Error));
+ console.log("Falling back to python3...");
+ return command_line('python3 scraper.py', cwd).then(onResolved, onRejected);
+ };
+
+ return command_line('python scraper.py', cwd).then(onResolved, tryPython3);
+ },
+ });
+
+ register({
+ method: Method.GET,
+ subscription: "/newBuxton",
+ secureHandler: async ({ res }) => res.send(await main())
+ });
+
+ register({
+ method: Method.GET,
+ subscription: "/version",
+ secureHandler: ({ res }) => {
+ return new Promise<void>(resolve => {
+ exec('"C:\\Program Files\\Git\\bin\\git.exe" rev-parse HEAD', (err, stdout) => {
+ if (err) {
+ res.send(err.message);
+ return;
+ }
+ res.send(stdout);
+ });
+ resolve();
+ });
+ }
+ });
+
+ }
+
+} \ No newline at end of file