Skip to content

Commit

Permalink
Update code based on demo session
Browse files Browse the repository at this point in the history
  • Loading branch information
graduta committed Nov 24, 2023
1 parent 929f4da commit 35b9380
Show file tree
Hide file tree
Showing 18 changed files with 1,445 additions and 200 deletions.
15 changes: 9 additions & 6 deletions 2-3-4-Exercises/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
const path = require('path');
const ApplicationService = require('./lib/ApplicationService.js');
const config = require('./config.js');

const {HttpServer, WebSocket, WebSocketMessage} = require('@aliceo2/web-ui');

const application = new ApplicationService();
const {FrameworkInfoController} = require('./lib/controllers/FrameworkInfoController.js');
const {FrameworkInfoService} = require('./lib/services/FrameworkInfoService.js');

const http = new HttpServer(config.http);
http.addStaticPath(path.join(__dirname, 'public'));
const ws = new WebSocket(http);

http.get('/getData', (req, res) => application.getData(req, res));
const infoService = new FrameworkInfoService();
const infoController = new FrameworkInfoController(infoService);

const ws = new WebSocket(http);
http.addStaticPath(path.join(__dirname, 'public'));
http.get('/info/:name', infoController.getDataHandler.bind(infoController));

setInterval(function() {

console.log('Sending data')
const message = new WebSocketMessage();
message.command = 'random';
message.payload = (Math.floor(Math.random() * 100)).toString();
ws.broadcast(message);

}, 5000);
28 changes: 0 additions & 28 deletions 2-3-4-Exercises/lib/ApplicationService.js

This file was deleted.

39 changes: 39 additions & 0 deletions 2-3-4-Exercises/lib/controllers/FrameworkInfoController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @class
* Controller class for retrieving information about the framework that is being developed
*/
class FrameworkInfoController {

/**
* @constructor
* Constructor for the FrameworkInfoController
* @param {FrameworkInfoService} infoService - service to be used to retrieve data of the framework
*/
constructor(infoService) {

this._infoService = infoService;
}

/**
* Handler to verify request and respond to the user
* @param {Request} req - HTTP request ExpressJS object
* @param {Response} res - HTTP response ExpressJS object
* @return {function}
*/
async getDataHandler(req, res) {
const {name} = req.params;
if (!name) {
res.status(422).json({message: 'Invalid name provided'});
return;
}
try {
const data = await this._infoService.getData(name);
res.status(200).json(data);
} catch (error) {
console.error(error);
res.status(502).json({message: 'service unavailable'});
}
}
}

module.exports = {FrameworkInfoController};
31 changes: 31 additions & 0 deletions 2-3-4-Exercises/lib/services/FrameworkInfoService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const WEB_UI = 'WEB-UI';
const BOOKKEEPING = 'BOOKKEEPING';

/**
* @class
* Service for returning information on the framework service
*/
class FrameworkInfoService {
/**
* Method to send back data about the project
* @param {String} name - name of the service to send data about
* @return {Promise.<String, Error>}
*/
async getData(name) {
if (name === WEB_UI) {
return {
name: 'MyWebUI Project',
version: 0.1,
};
} else if (name === BOOKKEEPING) {
return {
name: 'My Bookkeeping Project',
version: 3.1,
};
} else {
throw new Error('Unknown name');
}
}
}

module.exports = {FrameworkInfoService};
Loading

0 comments on commit 35b9380

Please sign in to comment.