<commit_before>/*
Copyright 2014 Vytroncs.com and <NAME>

This file is part of "Vytroncs HMI, the 100% Free, Open-Source SCADA/HMI Initiative"
herein referred to as "Vytronics HMI".

Vytronics HMI is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Vytronics HMI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with Vytronics HMI.  If not, see <http://www.gnu.org/licenses/>.
*/


/*JSlint allowed warning suppression*/
/*global console,require*/

var http = require('http');
var path = require('path');
var socketio = require('socket.io');
var express = require('express');

var vyutil = require('./vytronics/vyutil');
var project = require('./vytronics/project');

var self = this;

var datadir;

//Let apps access db
var db = require('./vytronics/db');
module.exports.db = db;

//Shortcut to logger object
var log = module.exports.db.log;

module.exports.log = log;

//Default logging level is set to WARN or let it be changed by
//env var VYTRONICS_LOG_LEVEL at startup or by modifying the log config file at runtime
log.setLevel(vyutil.getenv('VYTRONICS_LOG_LEVEL', 'WARN'));

module.exports.start = function() {
    log.info("Vytronics server.js started with node versions",process.versions );

    //Get project directory from env or default
    var projectdir = vyutil.getenv('VYTRONICS_PROJDIR');
    if (!projectdir) {
        projectdir = path.resolve(process.cwd(), './project');
    }
    log.info('VYTRONICS_PROJDIR set to:' + projectdir);

    //Resolve the project path relative to process directory
    projectdir = path.resolve(__dirname, projectdir);

    //
    // Creates a new instance of an http server with the following options:
    //  * `port` - The HTTP port to listen on. If `process.env.PORT` is set, _it overrides this value_.
    //
    var router = express();
    var server = http.createServer(router);

    //IO between server and clients
    var io = socketio.listen(server);

    //To turn off debug messages
    io.set('log level', 1);

    //Webserver. Root of the scripts that get included in project specific HTML
    router.use(express.static(path.resolve(__dirname,'client')));

    //This is the actual client directory and takes 2nd precedence.
    router.use(express.static(path.resolve(projectdir,'hmi')));

    //Listen for client connections and create clients
    io.on('connection', function (socket) {

        //Note that there is no real utility in logging the client IP address. There
        //Is no method to reliabily get it especially considering NAT and also that
        //any hosted node.js server will be double proxying. Putting this note here
        //so that nobody questions why it is not taken seriously, just FYI'd

        var addr = socket.handshake.address;

        //Always send this to the console
        console.log("Connection opened url:"+socket.handshake.url +
            " address:" + addr.address + ":" + addr.port);
        
        log.info("Connection opened url:"+socket.handshake.url +
            " address:" + addr.address + ":" + addr.port);

        //Note that query params can be extracted from
        //socket.handshake.query.myParam if needed for the application

        //Create a new client
        project.createClient(socket);
    });

    project.load(projectdir, function(err) {
    
        if (err){
            log.error('error loading project.yml - ' + err);
        }
        else {
    
            console.log('##### db.serverdb:',db.serverdb);

            server.listen(db.serverdb.listen_port, db.serverdb.listen_ip, function(){
              var addr = server.address();
              log.info("HMI server listening at", addr.address + ":" + addr.port);
            });
        }
    });
};
<commit_msg>Allow project.yml to override index.html default page<commit_after>/*
Copyright 2014 Vytroncs.com and <NAME>

This file is part of "Vytroncs HMI, the 100% Free, Open-Source SCADA/HMI Initiative"
herein referred to as "Vytronics HMI".

Vytronics HMI is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Vytronics HMI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with Vytronics HMI.  If not, see <http://www.gnu.org/licenses/>.
*/


/*JSlint allowed warning suppression*/
/*global console,require*/

var http = require('http');
var path = require('path');
var socketio = require('socket.io');
var express = require('express');

var vyutil = require('./vytronics/vyutil');
var project = require('./vytronics/project');

var self = this;

var datadir;

//Let apps access db
var db = require('./vytronics/db');
module.exports.db = db;

//Shortcut to logger object
var log = module.exports.db.log;

module.exports.log = log;

//Default logging level is set to WARN or let it be changed by
//env var VYTRONICS_LOG_LEVEL at startup or by modifying the log config file at runtime
log.setLevel(vyutil.getenv('VYTRONICS_LOG_LEVEL', 'WARN'));

module.exports.start = function() {
    log.info("Vytronics server.js started with node versions",process.versions );

    //Get project directory from env or default
    var projectdir = vyutil.getenv('VYTRONICS_PROJDIR');
    if (!projectdir) {
        projectdir = path.resolve(process.cwd(), './project');
    }
    log.info('VYTRONICS_PROJDIR set to:' + projectdir);

    //Resolve the project path relative to process directory
    projectdir = path.resolve(__dirname, projectdir);

    //
    // Creates a new instance of an http server with the following options:
    //  * `port` - The HTTP port to listen on. If `process.env.PORT` is set, _it overrides this value_.
    //
    var router = express();
    var server = http.createServer(router);

    //IO between server and clients
    var io = socketio.listen(server);

    //To turn off debug messages
    io.set('log level', 1);

    //Listen for client connections and create clients
    io.on('connection', function (socket) {

        //Note that there is no real utility in logging the client IP address. There
        //Is no method to reliabily get it especially considering NAT and also that
        //any hosted node.js server will be double proxying. Putting this note here
        //so that nobody questions why it is not taken seriously, just FYI'd

        var addr = socket.handshake.address;

        //Always send this to the console
        console.log("Connection opened url:"+socket.handshake.url +
            " address:" + addr.address + ":" + addr.port);
        
        log.info("Connection opened url:"+socket.handshake.url +
            " address:" + addr.address + ":" + addr.port);

        //Note that query params can be extracted from
        //socket.handshake.query.myParam if needed for the application

        //Create a new client
        project.createClient(socket);
    });

    project.load(projectdir, function(err) {
    
        if (err){
            log.error('error loading project.yml - ' + err);
        }
        else {
    
            //Webserver. Root of the scripts that get included in project specific HTML
            router.use(express.static(path.resolve(__dirname,'client')));

            //This is the actual client application directory and takes 2nd precedence.
            router.use(express.static(path.resolve(projectdir,'hmi'),
                                      //Override default index.html if project.yml defines server.home_page
                                      {'index': db.serverdb.home_page || 'index.html'}
                                     ));

            /*//Override index.html default page if a home page is defined for the server
            if ( db.serverdb.home_page ) {
                router.use(express.static(path.resolve(projectdir,'hmi')));
            }*/

            server.listen(db.serverdb.listen_port, db.serverdb.listen_ip, function(){
              var addr = server.address();
              log.info("HMI server listening at", addr.address + ":" + addr.port);
            });
        }
    });
};

{'fraction_non_alphanumeric': 0.10297456857402361, 'fraction_numerical': 0.002270663033605813, 'mean_word_length': 2.78393470790378, 'pattern_counts': {'":': 6, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '439007', 'n_tokens_mistral': 2481, 'n_tokens_neox': 2375, 'n_words': 907}