diff options
Diffstat (limited to 'src/server/ApiManagers')
-rw-r--r-- | src/server/ApiManagers/SearchManager.ts | 26 | ||||
-rw-r--r-- | src/server/ApiManagers/SessionManager.ts | 62 |
2 files changed, 79 insertions, 9 deletions
diff --git a/src/server/ApiManagers/SearchManager.ts b/src/server/ApiManagers/SearchManager.ts index c1c908088..4ce12f9f3 100644 --- a/src/server/ApiManagers/SearchManager.ts +++ b/src/server/ApiManagers/SearchManager.ts @@ -4,11 +4,11 @@ import { Search } from "../Search"; const findInFiles = require('find-in-files'); import * as path from 'path'; import { pathToDirectory, Directory } from "./UploadManager"; -import { command_line } from "../ActionUtilities"; -import request = require('request-promise'); -import { red } from "colors"; +import { red, cyan, yellow } from "colors"; import RouteSubscriber from "../RouteSubscriber"; -import { execSync } from "child_process"; +import { exec } from "child_process"; +import { onWindows } from ".."; +import { get } from "request-promise"; export class SearchManager extends ApiManager { @@ -69,15 +69,23 @@ export class SearchManager extends ApiManager { export namespace SolrManager { + const command = onWindows ? "solr.cmd" : "solr"; + export async function SetRunning(status: boolean): Promise<boolean> { const args = status ? "start" : "stop -p 8983"; + console.log(`solr management: trying to ${args}`); + exec(`${command} ${args}`, { cwd: "./solr-8.3.1/bin" }, (error, stdout, stderr) => { + if (error) { + console.log(red(`solr management error: unable to ${args} server`)); + console.log(red(error.message)); + } + console.log(cyan(stdout)); + console.log(yellow(stderr)); + }); try { - console.log(`Solr management: trying to ${args}`); - console.log(execSync(`./solr.cmd ${args}`, { cwd: "./solr-8.3.1/bin" })); + await get("http://localhost:8983"); return true; - } catch (e) { - console.log(red(`Solr management error: unable to ${args}`)); - console.log(e); + } catch { return false; } } diff --git a/src/server/ApiManagers/SessionManager.ts b/src/server/ApiManagers/SessionManager.ts new file mode 100644 index 000000000..0290b578c --- /dev/null +++ b/src/server/ApiManagers/SessionManager.ts @@ -0,0 +1,62 @@ +import ApiManager, { Registration } from "./ApiManager"; +import { Method, _permission_denied, AuthorizedCore, SecureHandler } from "../RouteManager"; +import RouteSubscriber from "../RouteSubscriber"; +import { sessionAgent } from ".."; + +const permissionError = "You are not authorized!"; + +export default class SessionManager extends ApiManager { + + private secureSubscriber = (root: string, ...params: string[]) => new RouteSubscriber(root).add("password", ...params); + + private authorizedAction = (handler: SecureHandler) => { + return (core: AuthorizedCore) => { + const { req, res, isRelease } = core; + const { password } = req.params; + if (!isRelease) { + return res.send("This can be run only on the release server."); + } + if (password !== process.env.session_key) { + return _permission_denied(res, permissionError); + } + handler(core); + }; + } + + protected initialize(register: Registration): void { + + register({ + method: Method.GET, + subscription: this.secureSubscriber("debug", "mode", "recipient"), + secureHandler: this.authorizedAction(({ req, res }) => { + const { mode, recipient } = req.params; + if (["passive", "active"].includes(mode)) { + sessionAgent.serverWorker.sendMonitorAction("debug", { mode, recipient }); + res.send(`Your request was successful: the server is ${mode === "active" ? "creating and compressing a new" : "retrieving and compressing the most recent"} back up. It will be sent to ${recipient}.`); + } else { + res.send(`Your request failed. '${mode}' is not a valid mode: please choose either 'active' or 'passive'`); + } + }) + }); + + register({ + method: Method.GET, + subscription: this.secureSubscriber("backup"), + secureHandler: this.authorizedAction(({ res }) => { + sessionAgent.serverWorker.sendMonitorAction("backup"); + res.send(`Your request was successful: the server is creating a new back up.`); + }) + }); + + register({ + method: Method.GET, + subscription: this.secureSubscriber("kill"), + secureHandler: this.authorizedAction(({ res }) => { + res.send("Your request was successful: the server and its session have been killed."); + sessionAgent.killSession("an authorized user has manually ended the server session via the /kill route"); + }) + }); + + } + +}
\ No newline at end of file |