<commit_before>/** Server.js
 *  Author: <NAME>
 *
 *  The Junction Server.
 */
var $o = require("./core.js");
var RouteMap = require('./Routing.js').RouteMap;
var Resource = require("./Resource.js").Resource;
var Http  = require("./Http.js");

var http  = require("http");
var https = require("https");
var path  = require("path");
var fs    = require("fs");
var EventEmitter = require("events").EventEmitter;

module.exports = (function($o) {

    // Server which serves the content
    var Server = $o.Object.extend({
        port: 8080, // port to listen to
        routemap: null, // Reference to the RouteMap
        root: "./scripts/", // root for all routes
        
        // For ssl either provide a cert and key or a pfx
        useHttps: false,
        tlsOptions: null, // options for tls server (https)
        
        isReady : false, // Ready to start?
        isPaused: false, // Boolean to keep track whenever the server is paused
        queue: null,  // When paused, queue all Contexts in a queue

        _root: "", // proccess cwd relative to the junction dir
        _routes: null, // Backup of all routes set via addRoute(a) 
        // handles
        onCreate: function() {},
        onStart: function() {},
        create: function(oPar) {
            EventEmitter.call(this);

            this._root = path.relative(__dirname, process.cwd());
            var r = oPar.root || this.root;
            this.root = (r[0] === ".") ? path.join(this._root, r) : path.relative(__dirname, r);

            this.port = oPar.port || this.port;
            this.configPort = oPar.configPort || this.configPort;
            this.localConfigOnly = oPar.localConfigOnly || this.localConfigOnly;
            
            this.useHttps = !!oPar.useHttps;
            this.tlsOptions = oPar.tlsOptions;

            this.createServer();
            
            this._routes = [];

            this.routemap = new RouteMap();
            this.queue = [];

            this.onCreate();
        },
        // Creates the http(s) server
        createServer: function() {
            var h = this.handleRequest.bind(this);
            var self = this;

            if(this.useHttps) {
                // Use https
                console.log("Using https");
                self.server = https.createServer(this.tlsOptions, h);
                self.isReady = true;
            } else {
                // Regular http server
                this.server = http.createServer(h);
                this.isReady = true;
            }
        },
        // Pause the server
        pause: function() { this.isPaused = true; },
        // Resume the paused server
        resume: function() {
            this.isPaused = false;

            for(var iX = 0; iX < this.queue.length; iX++)
                this.serve(this.queue[iX]);
            this.queue.length = 0;
        },
        /* Handles a incoming request 
         * req: NodeJs IncomingMessage object from the HttpServer
         * res: NodeJs OutgoingMessage object from the HttpServer
         */
        handleRequest: function(req, res) {
            var ctx = new Http.HttpContext(this, req, res);

            if(this.isPaused)
                this.queue.push(ctx);
            else
                this.serve(ctx);
        },
        /* Serves the given ctx
         * ctx: HttpContext to server */
        serve: function(ctx) {
            var found = ctx.resolveUrl();

            // 404 comment not found
            if(!found) {
                ctx.error(new Http.HttpError(404));
                return;
            }

            console.log("[Server] foundResource:" + ctx._foundResource);
            
            var res = Resource.create(this.root + ctx._foundResource);
            res.run(ctx);
        },
        // Starts the server
        start: function() { 

            console.log("Stated listening on port " + this.port);
            this.server.listen(this.port);

            this.onStart();
        },
        // Reloads both all resources and routemaps
        reload: function() {
            this.pause();

            this.routemap.clear();
            this.addRoutes(this._routes, true);

            Resource.reloadAll();
            
            this.resume();
        },
        /* Adds given routes to the routemap
         * a: array like with filenames or json Objects of the routes to incl
         * reload: set to true when reloading, causes the routes not to be added to the _route */
        addRoutes: function(a, reload) {
            var self = this;
            if(Array.isArray(a))
                return a.forEach(function(e) { self.addRoutes(e, reload); });

            // file
            if(typeof(a) === "string") {
                var fname = require.resolve(this._root + "\\" + a);
                if(!fname) console.log("[Routes] can't find file " + a);

                if(!reload) this._routes.push(a);
                else delete require.cache[fname];

                var json = require(fname);
    
                this.routemap.load(json);

            // json
            } else if(typeof(a) === "object") {
                if(!reload) this._routes.push(a);

                this.routemap.load(a);
            }
        },
        listResources: function() { return Resource.list(); },
        listRoutes: function() { return this.routemap.list(); }
    });


    return { Server: Server };
})($o);<commit_msg>Added function type of routes<commit_after>/** Server.js
 *  Author: <NAME>
 *
 *  The Junction Server.
 */
var $o = require("./core.js");
var RouteMap = require('./Routing.js').RouteMap;
var Resource = require("./Resource.js").Resource;
var Http  = require("./Http.js");

var http  = require("http");
var https = require("https");
var path  = require("path");
var fs    = require("fs");
var EventEmitter = require("events").EventEmitter;

module.exports = (function($o) {

    // Server which serves the content
    var Server = $o.Object.extend({
        port: 8080, // port to listen to
        routemap: null, // Reference to the RouteMap
        root: "./scripts/", // root for all routes
        
        // For ssl either provide a cert and key or a pfx
        useHttps: false,
        tlsOptions: null, // options for tls server (https)
        
        isReady : false, // Ready to start?
        isPaused: false, // Boolean to keep track whenever the server is paused
        queue: null,  // When paused, queue all Contexts in a queue

        _root: "", // proccess cwd relative to the junction dir
        _routes: null, // Backup of all routes set via addRoute(a) 
        // handles
        onCreate: function() {},
        onStart: function() {},
        create: function(oPar) {
            EventEmitter.call(this);

            this._root = path.relative(__dirname, process.cwd());
            var r = oPar.root || this.root;
            this.root = (r[0] === ".") ? path.join(this._root, r) : path.relative(__dirname, r);

            this.port = oPar.port || this.port;
            this.configPort = oPar.configPort || this.configPort;
            this.localConfigOnly = oPar.localConfigOnly || this.localConfigOnly;
            
            this.useHttps = !!oPar.useHttps;
            this.tlsOptions = oPar.tlsOptions;

            this.createServer();
            
            this._routes = [];

            this.routemap = new RouteMap();
            this.queue = [];

            this.onCreate();
        },
        // Creates the http(s) server
        createServer: function() {
            var h = this.handleRequest.bind(this);
            var self = this;

            if(this.useHttps) {
                // Use https
                console.log("Using https");
                self.server = https.createServer(this.tlsOptions, h);
                self.isReady = true;
            } else {
                // Regular http server
                this.server = http.createServer(h);
                this.isReady = true;
            }
        },
        // Pause the server
        pause: function() { this.isPaused = true; },
        // Resume the paused server
        resume: function() {
            this.isPaused = false;

            for(var iX = 0; iX < this.queue.length; iX++)
                this.serve(this.queue[iX]);
            this.queue.length = 0;
        },
        /* Handles a incoming request 
         * req: NodeJs IncomingMessage object from the HttpServer
         * res: NodeJs OutgoingMessage object from the HttpServer
         */
        handleRequest: function(req, res) {
            var ctx = new Http.HttpContext(this, req, res);

            if(this.isPaused)
                this.queue.push(ctx);
            else
                this.serve(ctx);
        },
        /* Serves the given ctx
         * ctx: HttpContext to server */
        serve: function(ctx) {
            var found = ctx.resolveUrl();

            // 404 comment not found
            if(!found) {
                ctx.error(new Http.HttpError(404));
                return;
            }

            console.log("[Server] foundResource:" + ctx._foundResource);
            
            var res = Resource.create(this.root + ctx._foundResource);
            res.run(ctx);
        },
        // Starts the server
        start: function() { 

            console.log("Stated listening on port " + this.port);
            this.server.listen(this.port);

            this.onStart();
        },
        // Reloads both all resources and routemaps
        reload: function() {
            this.pause();

            this.routemap.clear();
            this.addRoutes(this._routes, true);

            Resource.reloadAll();
            
            this.resume();
        },
        /* Adds given routes to the routemap
         * a: array like with filenames or json Objects of the routes to incl
         * reload: set to true when reloading, causes the routes not to be added to the _route */
        addRoutes: function(a, reload) {
            var self = this;
            if(Array.isArray(a))
                return a.forEach(function(e) { self.addRoutes(e, reload); });

            // file
            if(typeof(a) === "string") {
                var fname = require.resolve(this._root + "\\" + a);
                if(!fname) console.log("[Routes] can't find file " + a);

                if(!reload) this._routes.push(a);
                else delete require.cache[fname];

                var json = require(fname);
    
                this.routemap.load(json);

            // json
            } else if(typeof(a) === "object") {
                if(!reload) this._routes.push(a);

                this.routemap.load(a);

            // function
            } else if(typeof(a) === "function") {
                this.routemap.load(a());
            }
        },
        listResources: function() { return Resource.list(); },
        listRoutes: function() { return this.routemap.list(); }
    });


    return { Server: Server };
})($o);
{'fraction_non_alphanumeric': 0.12345679012345678, 'fraction_numerical': 0.0023602033405954975, 'mean_word_length': 1.5686640242480765, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7162284', 'n_tokens_mistral': 3099, 'n_tokens_neox': 2971, 'n_words': 794}