diff options
author | Sam Wilkins <samwilkins333@gmail.com> | 2020-01-04 13:02:54 -0800 |
---|---|---|
committer | Sam Wilkins <samwilkins333@gmail.com> | 2020-01-04 13:02:54 -0800 |
commit | 987b512d2564710e5c5c7fd2eeff1914af8180dd (patch) | |
tree | 04862f06f5d410a316be7d3149e579e541b1ef52 | |
parent | 804e2f4f1d20551799a21e2fdf8e5b2b7fdebe02 (diff) |
factored out socket and server ports to config, added kill response ;)
-rw-r--r-- | session.config.json | 4 | ||||
-rw-r--r-- | src/server/Initialization.ts | 11 | ||||
-rw-r--r-- | src/server/Session/session.ts | 28 | ||||
-rw-r--r-- | src/server/Session/session_config_schema.ts | 6 | ||||
-rw-r--r-- | src/server/Websocket/Websocket.ts | 7 | ||||
-rw-r--r-- | src/server/index.ts | 12 |
6 files changed, 36 insertions, 32 deletions
diff --git a/session.config.json b/session.config.json index 7396e1135..d8f86d239 100644 --- a/session.config.json +++ b/session.config.json @@ -3,7 +3,9 @@ "recipients": [ "samuel_wilkins@brown.edu" ], - "heartbeat": "http://localhost:1050/serverHeartbeat", + "serverPort": 1050, + "socketPort": 4321, + "heartbeatRoute": "/serverHeartbeat", "pollingIntervalSeconds": 15, "masterIdentifier": "__master__", "workerIdentifier": "__worker__", diff --git a/src/server/Initialization.ts b/src/server/Initialization.ts index 465e7ea63..702339ca1 100644 --- a/src/server/Initialization.ts +++ b/src/server/Initialization.ts @@ -26,15 +26,9 @@ import { blue, yellow } from 'colors'; /* RouteSetter is a wrapper around the server that prevents the server from being exposed. */ export type RouteSetter = (server: RouteManager) => void; -export interface InitializationOptions { - serverPort: number; - routeSetter: RouteSetter; -} - export let disconnect: Function; -export default async function InitializeServer(options: InitializationOptions) { - const { serverPort, routeSetter } = options; +export default async function InitializeServer(routeSetter: RouteSetter) { const app = buildWithMiddleware(express()); app.use(express.static(publicDirectory)); @@ -63,8 +57,9 @@ export default async function InitializeServer(options: InitializationOptions) { routeSetter(new RouteManager(app, isRelease)); + const serverPort = Number(process.env.serverPort); const server = app.listen(serverPort, () => { - logPort("server", serverPort); + logPort("server", Number(serverPort)); console.log(); }); disconnect = async () => new Promise<Error>(resolve => server.close(resolve)); diff --git a/src/server/Session/session.ts b/src/server/Session/session.ts index e32811a18..d1d7aab87 100644 --- a/src/server/Session/session.ts +++ b/src/server/Session/session.ts @@ -40,7 +40,9 @@ export namespace Session { workerIdentifier, recipients, signature, - heartbeat, + heartbeatRoute, + serverPort, + socketPort, showServerOutput, pollingIntervalSeconds } = function loadConfiguration(): any { @@ -72,7 +74,7 @@ export namespace Session { }(); // this sends a pseudorandomly generated guid to the configuration's recipients, allowing them alone - // to kill the server via the /kill/:password route + // to kill the server via the /kill/:key route const key = Utils.GenerateGuid(); const timestamp = new Date().toUTCString(); const content = `The key for this session (started @ ${timestamp}) is ${key}.\n\n${signature}`; @@ -112,7 +114,9 @@ export namespace Session { const spawn = (): void => { tryKillActiveWorker(); activeWorker = fork({ - heartbeat, + heartbeatRoute, + serverPort, + socketPort, pollingIntervalSeconds, session_key: key }); @@ -212,18 +216,22 @@ export namespace Session { // one reason to exit, as the process might be in an inconsistent state after such an exception process.on('uncaughtException', activeExit); - const { pollingIntervalSeconds, heartbeat } = process.env; - + const { + pollingIntervalSeconds, + heartbeatRoute, + serverPort + } = process.env; // this monitors the health of the server by submitting a get request to whatever port / route specified // by the configuration every n seconds, where n is also given by the configuration. + const heartbeat = `http://localhost:${serverPort}${heartbeatRoute}`; const checkHeartbeat = async (): Promise<void> => { await new Promise<void>(resolve => { setTimeout(async () => { try { - await get(heartbeat!); + await get(heartbeat); if (!listening) { // notify master thread (which will log update in the console) via IPC that the server is up and running - process.send?.({ lifecycle: green("listening...") }); + process.send?.({ lifecycle: green(`listening on ${serverPort}...`) }); } listening = true; resolve(); @@ -239,12 +247,8 @@ export namespace Session { checkHeartbeat(); }; - // the actual work of the process, may be asynchronous - // for Dash, this is the code that launches the server work(); - - // begin polling - checkHeartbeat(); + checkHeartbeat(); // begin polling } }
\ No newline at end of file diff --git a/src/server/Session/session_config_schema.ts b/src/server/Session/session_config_schema.ts index a5010055a..03009a351 100644 --- a/src/server/Session/session_config_schema.ts +++ b/src/server/Session/session_config_schema.ts @@ -1,7 +1,7 @@ import { Schema } from "jsonschema"; const emailPattern = /^(([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+)?$/g; -const localPortPattern = /http\:\/\/localhost:\d+\/[a-zA-Z]+/g; +const localPortPattern = /\/[a-zA-Z]+/g; const properties = { recipients: { @@ -12,7 +12,9 @@ const properties = { }, minLength: 1 }, - heartbeat: { + serverPort: { type: "number" }, + socketPort: { type: "number" }, + heartbeatRoute: { type: "string", pattern: localPortPattern }, diff --git a/src/server/Websocket/Websocket.ts b/src/server/Websocket/Websocket.ts index 0b58ca344..e26a76107 100644 --- a/src/server/Websocket/Websocket.ts +++ b/src/server/Websocket/Websocket.ts @@ -18,15 +18,15 @@ export namespace WebSocket { export const socketMap = new Map<SocketIO.Socket, string>(); export let disconnect: Function; - export async function start(serverPort: number, isRelease: boolean) { + export async function start(isRelease: boolean) { await preliminaryFunctions(); - initialize(serverPort, isRelease); + initialize(isRelease); } async function preliminaryFunctions() { } - export function initialize(socketPort: number, isRelease: boolean) { + function initialize(isRelease: boolean) { const endpoint = io(); endpoint.on("connection", function (socket: Socket) { _socket = socket; @@ -63,6 +63,7 @@ export namespace WebSocket { }; }); + const socketPort = Number(process.env.socketPort); endpoint.listen(socketPort); logPort("websocket", socketPort); } diff --git a/src/server/index.ts b/src/server/index.ts index 3c1839e4d..7eb8b12be 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -90,11 +90,11 @@ function routeSetter({ isRelease, addSupervisedRoute, logRegistrationOutcome }: addSupervisedRoute({ method: Method.GET, - subscription: new RouteSubscriber("kill").add("password"), + subscription: new RouteSubscriber("kill").add("key"), secureHandler: ({ req, res }) => { - if (req.params.password === process.env.session_key) { - process.send!({ action: { message: "kill" } }); - res.send("Server successfully killed."); + if (req.params.key === process.env.session_key) { + res.send("<img src='https://media.giphy.com/media/NGIfqtcS81qi4/giphy.gif' style='width:100%;height:100%;'/>"); + setTimeout(() => process.send!({ action: { message: "kill" } }), 1000 * 5); } else { res.redirect("/home"); } @@ -125,7 +125,7 @@ function routeSetter({ isRelease, addSupervisedRoute, logRegistrationOutcome }: // initialize the web socket (bidirectional communication: if a user changes // a field on one client, that change must be broadcast to all other clients) - WebSocket.initialize(serverPort, isRelease); + WebSocket.start(isRelease); } /** @@ -142,6 +142,6 @@ if (isMaster) { endMessage: "completed preliminary functions\n", action: preliminaryFunctions }); - await initializeServer({ serverPort: 1050, routeSetter }); + await initializeServer(routeSetter); }); }
\ No newline at end of file |