aboutsummaryrefslogtreecommitdiff
path: root/src/server/ApiManagers/UtilManager.ts
blob: a4b4b14a9a2ca9aa6256886a4fbaba6450ad3779 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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";

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: "/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();
                });
            }
        });

    }

}