-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,445 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
2-3-4-Exercises/lib/controllers/FrameworkInfoController.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
Oops, something went wrong.