Skip to content

Commit 444640b

Browse files
committed
Add initial server, client, and test setup for exercises
1 parent 60997e0 commit 444640b

File tree

18 files changed

+8913
-885
lines changed

18 files changed

+8913
-885
lines changed

.github/workflows/.gh-action.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Run tests on macOS and Ubuntu on Push and PR to main
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ${{ matrix.os }}
12+
strategy:
13+
matrix:
14+
os: [ubuntu-18.04, macOS-latest]
15+
16+
steps:
17+
- uses: actions/checkout@v3
18+
19+
- name: Setup Node.jsß
20+
uses: actions/setup-node@v3
21+
with:
22+
node-version: '22.x'
23+
- run: (cd 2-3-4-Exercises; npm ci)
24+
- run: (cd 2-3-4-Exercises; npm test)
25+
- run: (cd 5-6-7-Exercises; npm run coverage)

1-Introductory-Exercises/index.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,41 @@
1-
// Your code goes here
1+
// Include required modules
2+
const {HttpServer, WebSocket, WebSocketMessage, LogManager} = require('@aliceo2/web-ui');
3+
4+
const logger = LogManager.getLogger('Exercise1');
5+
6+
const jwtConfig = {
7+
expiration: 30,
8+
maxAge: 30
9+
};
10+
11+
const http = new HttpServer({
12+
port: 8080,
13+
}, jwtConfig);
14+
15+
// Server `public` folder
16+
http.addStaticPath('public');
17+
18+
http.get('/helloWorld', (req, res) => {
19+
res.status(200).json({message: 'Hello, World!'})
20+
}, { public: true});
21+
22+
http.get('/testJWT', (req, res) => {
23+
logger.debugMessage('Received token:' + req.query.token);
24+
res.status(200).json({message: 'JWT is valid!'});
25+
});
26+
27+
http.get('/addDefaultUser', (req, res) => {
28+
http.addDefaultUserData(req, res); // adds /api/userData endpoint
29+
}, { public: true});
30+
31+
// curl cmd to test it
32+
// curl localhost:8080/api/addDefaultUser
33+
// JWT=
34+
// curl -G "localhost:8080/api/testJWT" --data-urlencode "token=$JWT";
35+
36+
const ws = new WebSocket(http);
37+
38+
ws.bind('hello', (message) => {
39+
logger.debugMessage('Received hello message:' + message.payload.info);
40+
return new WebSocketMessage('200').setCommand('hello-back').setPayload({info: 'Hello from WebSocket server!'});
41+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!doctype html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8" />
6+
<meta name="viewport" content="width=device-width,initial-scale=1" />
7+
<title>Hello World</title>
8+
<link rel="shortcut icon" href="o2-favicon.png" />
9+
<script type="module" src="./webSocket.js"></script>
10+
11+
12+
</head>
13+
14+
<body>
15+
<h1>Hello, world!</h1>
16+
</body>
17+
18+
</html>
1.9 KB
Loading
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {h, mount, Observable, WebSocketClient, sessionService} from '/js/src/index.js';
2+
3+
4+
const model = new Observable();
5+
const view = (model) => h('h1.title', `${model.title}`);
6+
mount(document.body, view, model);
7+
model.title = 'Websocket Example';
8+
model.notify();
9+
10+
sessionService.loadAndHideParameters();
11+
12+
13+
const ws = new WebSocketClient();
14+
15+
ws.addListener('authed', () => {
16+
console.log('WebSocket connected and authenticated');
17+
ws.sendMessage({command: 'hello', payload: {info: 'Hello WebSocket Server!'}});
18+
});
19+
20+
ws.addListener('command', (message) => {
21+
if (message.command === 'hello-back') {
22+
console.log('Received message from server:', message.payload);
23+
}
24+
});

2-3-4-Exercises/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
const path = require('path');
22
const config = require('./config.js');
33

4-
const {HttpServer} = require('@aliceo2/web-ui');
4+
const {HttpServer, LogManager} = require('@aliceo2/web-ui');
5+
6+
const logger = LogManager.getLogger('Exercise2');
57

68
const http = new HttpServer(config.http, config.jwt, config.oAuth);
79
http.addStaticPath(path.join(__dirname, 'public'));
10+
11+
http.get('/applicationData', (req, res) => {
12+
logger.debugMessage('Request for application data');
13+
res.json({
14+
appName: "2-3-4 Exercises Application",
15+
});
16+
});
17+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class ApplicationService {
2+
constructor() {
3+
this.details = {
4+
appName: '2-3-4 Exercises Application',
5+
};
6+
7+
}
8+
9+
getDetails() {
10+
return this.details;
11+
}
12+
13+
}
14+
15+
export default ApplicationService;

0 commit comments

Comments
 (0)