Hello everybody,
I’m stuck on a litlle problem
Before showing code, I must say that i’m a beginners in meteor and web developpement ( I’m in network and system ) and sorry for my bad english I’m speak french.
Now to the problem
I’m making a little application to monitor my openvpn connection.
To do this i must open and parse a text file.
When I use the Assets.getText(“file”), my client browser get an exception but code does what it should.
Here is the error
ReferenceError: Assets is not defined
var linesGet = Assets.getText('openvpn-status.log');
Here is the code from openvpn.js
connectionList = new MysqlSubscription('allInfo');
vpnUserList = new MysqlSubscription('vpnInfo');
vpnUserHistory = new MysqlSubscription('oneUserHistory','TEST');
fileContent = new Mongo.Collection('fileContent');
Accounts.config({
forbidClientAccountCreation: true
});
if (Meteor.isServer) {
// This code only runs on the server
var liveDb = new LiveMysql({
host: 'localhost',
// Port 3407 as specified in leaderboard.mysql.json
// If using external MySQL server, the default port is 3306
port: 3306,
user: 'test',
password: 'test',
database: 'connection'
});
var closeAndExit = function() {
liveDb.end();
process.exit();
};
// Close connections on hot code push
process.on('SIGTERM', closeAndExit);
// Close connections on exit (ctrl + c)
process.on('SIGINT', closeAndExit);
Meteor.publish('allInfo', function() {
return liveDb.select(
'SELECT * FROM connection ORDER BY time DESC limit 5',
[ { table: 'connection' } ]
);
});
Meteor.publish('vpnInfo', function() {
return liveDb.select(
'SELECT DISTINCT common_name FROM connection ORDER BY common_name ASC',
[ { table: 'connection' } ]
);
});
Meteor.publish('oneUserHistory',function(vpnUser) {
return liveDb.select(
'SELECT * FROM connection where common_name = ' + liveDb.db.escape(vpnUser)+ ' ORDER BY time DESC',
[ { table: 'connection' } ]
);
});
Meteor.publish("fileContent", function () {
return fileContent.find();
});
}
if (Meteor.isClient) {
// This code only runs on the client
accountsUIBootstrap3.setLanguage('fr');
Meteor.subscribe("fileContent");
Template.registerHelper('formatDate', function(date) {
return moment(date).format('DD/MM/YYYY HH:mm:ss');
});
Template.registerHelper('vpnUserList', function() {
return vpnUserList.reactive();
});
Template.registerHelper('connectionList', function() {
return connectionList.reactive();
});
Template.registerHelper('vpnUserHistory', function() {
return vpnUserHistory.reactive();
});
Accounts.ui.config({
passwordSignupFields: "USERNAME_ONLY"
});
Template.body.created = function() {
Session.set("activeTemplate", "lastHistory");
Session.set('selectedUser', "none");
}
Template.body.helpers({
activeTemplate: function() {
return Session.get('activeTemplate');
}
});
Template.body.events({
'click .real-time': function(){
if ( Session.get("activeTemplate") != "realTime") {
Session.set("activeTemplate", "realTime");
Meteor.call('readFile');
Session.set('intervalId',Meteor.setInterval(function(){ Meteor.call('readFile');},60*1000));
}
else {
Session.set('selectedUser', "none");
Session.set("activeTemplate","lastHistory");
Meteor.clearInterval(Session.get("intervalId"))
}
}
});
Template.userTmplList.helpers({
vpnUserNumber: function() {
if (vpnUserList.length > 0 ) {
return true;
}
else {
return false;
}
}
});
Template.userVpnItem.events({
'click a': function(){
var nameUser = this.common_name;
if ( nameUser == Session.get('selectedUser')) {
Session.set('selectedUser', "none");
Session.set("activeTemplate", "lastHistory");
}
else {
Session.set('selectedUser', nameUser);
vpnUserHistory.change(nameUser);
Session.set("activeTemplate","userHistory");
}
}
});
Template.userVpnItem.helpers({
'isSelected': function(){
var userName = this.common_name;
var selectedUser = Session.get('selectedUser');
if(userName == selectedUser){
return "selected"
}
}
});
Template.realTime.helpers({
fileContent: function() {
return fileContent.find();
}
});
}
Meteor.methods({
readFile : function() {
var linesGet = Assets.getText('openvpn-status.log');
fileContent.remove({});
var lines= linesGet.split(/\r\n|\n/);
var l = lines.length - 1;
var i = 3;
while( lines[i] != "ROUTING TABLE" && i < l ) {
var line = lines[i];
var line_parts = line.split(',');
fileContent.insert ({
common_name : line_parts[0],
real_address: line_parts[1],
bytes_received: line_parts[2],
bytes_send: line_parts[3],
connected_since: line_parts[4]
});
console.log("Comming from insert : "+line_parts[0]);
i++;
return true;
}
}
});
Thanks for your help.