Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: maintenance mode websocket #469

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
"typeorm": "^0.3.20",
"uuid": "^10.0.0",
"validator": "^13.12.0",
"vitepress-theme-openapi": "^0.0.3-alpha.30"
"vitepress-theme-openapi": "^0.0.3-alpha.30",
"ws": "^8.18.0"
},
"devDependencies": {
"@apidevtools/swagger-cli": "^4.0.4",
Expand All @@ -81,6 +82,7 @@
"@types/swagger-ui-express": "^4.1.7",
"@types/uuid": "^10.0.0",
"@types/validator": "^13.12.2",
"@types/ws": "^8.5.14",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/parser": "^7.18.0",
"chai": "^4.3.7",
Expand Down
5 changes: 5 additions & 0 deletions src/controller/server-settings-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import log4js, { Logger } from 'log4js';
import Policy from './policy';
import { RequestWithToken } from '../middleware/token-middleware';
import ServerSettingsStore from '../server-settings/server-settings-store';
import WebSocketService from '../service/websocket-service';

/**
* @typedef {object} UpdateMaintenanceModeRequest
Expand All @@ -45,6 +46,7 @@ export default class ServerSettingsController extends BaseController {
public constructor(options: BaseControllerOptions) {
super(options);
this.logger.level = process.env.LOG_LEVEL;
WebSocketService.initiateWebSocket();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe move this to the index?

}

public getPolicy(): Policy {
Expand Down Expand Up @@ -79,6 +81,9 @@ export default class ServerSettingsController extends BaseController {
const store = ServerSettingsStore.getInstance();
await store.setSetting('maintenanceMode', body.enabled);

// Send websocket message to POS
WebSocketService.sendMaintenanceMode(body.enabled);

res.status(204).send();
} catch (error) {
this.logger.error('Could not update maintenance mode:', error);
Expand Down
58 changes: 58 additions & 0 deletions src/service/websocket-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* SudoSOS back-end API service.
* Copyright (C) 2024 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license
*/


import http from 'http';
import express from 'express';
import WebSocket from 'ws';
import log4js, { Logger } from 'log4js';

/**
* This is the module page of the websocket-service.
*
* @module websocket
*/

export default class WebSocketService {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest a singleton class like the ServerSettings

private static readonly httpServer = http.createServer(express());

private static webSocketServer = new WebSocket.Server({ server: this.httpServer });

public static initiateWebSocket(): void {
const logger: Logger = log4js.getLogger('LDAP');
RubenLWF marked this conversation as resolved.
Show resolved Hide resolved
logger.level = process.env.LOG_LEVEL;

const port = process.env.WEBSOCKET_PORT || 443;
this.httpServer.listen(port);
logger.info('Opened WebSocket server on port ' + port);

this.webSocketServer.on('maintenance-mode', (status) => {
this.webSocketServer.clients.forEach((client) => {
const message = 'maintenance-mode ' + status;
client.send(message);
logger.info('Sent \"' + message + '\" over WebSocket');
});
});
}

public static sendMaintenanceMode(enabled: boolean): void {
this.webSocketServer.emit('maintenance-mode', enabled);
}
}
Loading