Browse Source

add example

master
zeus 3 years ago
parent
commit
873b6624de
  1. 1
      install/usr/share/swarmlab.io/sec/project/courses/README
  2. 31
      install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/app/client.js
  3. 90
      install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/app/helloworld.js
  4. 24
      install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/app/server.js
  5. 8
      install/usr/share/swarmlab.io/sec/project/courses/example-helloworld/package.json
  6. 3
      install/usr/share/swarmlab.io/sec/swarmlab-sec

1
install/usr/share/swarmlab.io/sec/project/courses/README

@ -0,0 +1 @@
Course examples

31
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('<h1>send</h1>');
});
http.listen(8085, () => {
console.log('listening on *:8085');
});
*/

90
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('<h1>Hello world!</h1>');
});
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 "<cleartext password>"
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('<h1>test!</h1>');
});
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');
});

24
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');

8
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"
}
}

3
install/usr/share/swarmlab.io/sec/swarmlab-sec

@ -34,7 +34,7 @@ PACKAGES=$(cat <<EOF
vim
EOF
)
# PACKAGES enabled ths with apt-get update && apt-get $APTPARAM install -y openssh-server $PACKAGES in in RUN export DEBIAN_FRONTEND=noninteractive
APTPARAM=" --no-install-recommends "
IMAGE_local="hybrid-nodevuejs"
HYBRID_NETWORK="hybrid-nodevuejs"
@ -298,6 +298,7 @@ fi
/bin/mkdir -p $Wdir/project
/bin/cp -rf $DIR/project/bin $Wdir/project
/bin/cp -rf $DIR/project/courses $Wdir/project
/bin/cp -f $DIR/project/hello_world.sh $Wdir/project
/bin/cp -f $DIR/$bootstrap $Wdir/$bootstrap
/bin/cp -f $DIR/$hostnames $Wdir/$hostnames

Loading…
Cancel
Save