From 873b6624def914780891a27b654738cf2a7f771d Mon Sep 17 00:00:00 2001 From: zeus Date: Sun, 28 Mar 2021 17:31:47 +0300 Subject: [PATCH] add example --- .../swarmlab.io/sec/project/courses/README | 1 + .../courses/example-helloworld/app/client.js | 31 +++++++ .../example-helloworld/app/helloworld.js | 90 +++++++++++++++++++ .../courses/example-helloworld/app/server.js | 24 +++++ .../courses/example-helloworld/package.json | 8 ++ .../usr/share/swarmlab.io/sec/swarmlab-sec | 3 +- 6 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 install/usr/share/swarmlab.io/sec/project/courses/README create mode 100644 install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/app/client.js create mode 100644 install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/app/helloworld.js create mode 100644 install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/app/server.js create mode 100644 install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/package.json diff --git a/install/usr/share/swarmlab.io/sec/project/courses/README b/install/usr/share/swarmlab.io/sec/project/courses/README new file mode 100644 index 0000000..50cc7af --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/README @@ -0,0 +1 @@ +Course examples diff --git a/install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/app/client.js b/install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/app/client.js new file mode 100644 index 0000000..0be0921 --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/app/client.js @@ -0,0 +1,31 @@ + +const app = require('express')(); +const http = require('http').Server(app); +var path = require('path'); + +options = { + secure:true, + reconnect: true, + rejectUnauthorized : false +}; + + +var io2 = require('socket.io-client'); +var socket = io2.connect('http://localhost:8084', options); + +var msg2 = "c= 120"; +socket.emit('log', msg2); + +/* +var io = require('socket.io')(http); + +app.get('/log', (req, res) => { + socket.emit('log', 'send from get'); + res.send('

send

'); +}); + +http.listen(8085, () => { + console.log('listening on *:8085'); +}); +*/ + diff --git a/install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/app/helloworld.js b/install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/app/helloworld.js new file mode 100644 index 0000000..14b15f3 --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/app/helloworld.js @@ -0,0 +1,90 @@ +const app = require('express')(); +const http = require('http').Server(app); +var path = require('path'); +var io = require('socket.io')(http); +const MongoClient = require('mongodb').MongoClient; + + +app.get('/', (req, res) => { + res.send('

Hello world!

'); +}); + +app.get('/test', (req, res) => { + + + var user="test1" + var pass="newpass" + //mongo “mongodb://localhost:30001,localhost:30002,localhost:30003/$MONGO_INITDB_DATABASE” -u $MONGO_INITDB_USERNAME mongo “mongodb://localhost:30001,localhost:30002,localhost:30003/app_swarmlab” -u app_swarmlab + //mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myRs + //var mongourl = "mongodb://"+user+":"+pass+"@localhost:30001,localhost:30002,localhost:30003/app_swarmlab?replicaSet=rs0&authSource=admin" + /* + use admin +db.createUser( + { + user: "test1", + pwd: 'newpass', // Or "" + roles: [ { role: "readWrite", db: "app_swarmlab" } ], + authenticationRestrictions: [ { + clientSource: ["192.168.1.7"], + serverAddress: ["192.168.80.2", "192.168.80.3", "192.168.80.4"] + } ] + } +) +*/ + + var mongourl = "mongodb://"+user+":"+pass+"@192.168.80.2:27017/app_swarmlab?replicaSet=rs0&authSource=admin" + const OPTS = { + useNewUrlParser: true, + useUnifiedTopology: true + }; +var options = { + useNewUrlParser: true, + useUnifiedTopology: true +} + + + MongoClient.connect(mongourl, options, function(err, client){ + if(err){ + console.log(err); + } else { + console.log(JSON.stringify('mongo ----------------connected')) + const db = client.db('app_swarmlab'); + db.collection('logs').find({}).toArray() + .then(item => { + console.log('item '+JSON.stringify(item)) + for (let i in item) { + console.log(JSON.stringify('items' + item[i])) + } + }) + + } + }) + res.send('

test!

'); + +}); + + + + + +io.on('connection', s => { + console.error('socket connection'); + + s.on('log', (data, room) => { + s.to('iot').emit('message', data); + console.log('broadcast', data); + + }); + //s.emit('message', 'message from server'); + + + +}); + + + + + +http.listen(8084, () => { + console.log('listening on *:8084'); +}); diff --git a/install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/app/server.js b/install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/app/server.js new file mode 100644 index 0000000..66f9c8b --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/app/server.js @@ -0,0 +1,24 @@ +var path = require('path'); +var app = require('express')(); +var http = require('http').Server(app); +var io = require('socket.io')(http); + + + +io.on('connection', s => { + console.error('socket connection'); + + s.on('log', (data, room) => { + s.to('iot').emit('message', data); + console.log('broadcast', data); + + }); + //s.emit('message', 'message from server'); + + + +}); + + +http.listen(8084, () => console.error('listening on http://localhost:8084/')); +console.error('socket.io example'); diff --git a/install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/package.json b/install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/package.json new file mode 100644 index 0000000..45af24c --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/package.json @@ -0,0 +1,8 @@ +{ + "dependencies": { + "express": "^4.17.1", + "mongodb": "^3.6.5", + "socket.io": "^4.0.0", + "socket.io-client": "^4.0.0" + } +} diff --git a/install/usr/share/swarmlab.io/sec/swarmlab-sec b/install/usr/share/swarmlab.io/sec/swarmlab-sec index da7950d..2cfa828 100755 --- a/install/usr/share/swarmlab.io/sec/swarmlab-sec +++ b/install/usr/share/swarmlab.io/sec/swarmlab-sec @@ -34,7 +34,7 @@ PACKAGES=$(cat <