<commit_before>var express = require('express');
var genericRoutes = require('./engine');
var _ = require('underscore');
var path = require("path");
var multer = require('multer');

var fs = require('fs');
var mkdirp = require('mkdirp');

var bunyan = require('bunyan');

/*
Exemple:

var Eurekapi = require('eurekapi');

var server = new Eurekapi({
    name: 'projectName',
    version: 1,
    host: '0.0.0.0'
    port: 4000,
    enableCORS: false,
    database: {
        adapter: 'rdf',
        config: {
            store: 'virtuoso',
            graphURI: 'http://projectName.com'
        }
    },
    schemas: require('./schemas'),
    customRoutes: [
        {method: 'delete', url: '/nugget', func: deleteNugget}
    ],
    publicDirectory: '/dist'
});
server.start();
 */


var Server = function(config) {
    this.config = config;
    if (!this.config.name) {
        throw 'EurekaServer: name is required';
    }
    var logLevel = config.logLevel || 'info';
    this.logger = bunyan.createLogger({name: config.name, level: logLevel});
    this.apiVersion = this.config.version || 1;
    this.baseURI = this.config.baseURI || ("/api/" + config.version);
    this.applicationName = this.config.name;
    this.db = this.getDatabase();
    this.app = express();
    this.registerMiddlewares();

    var routesAdded = [];
    var routes = [];

    // add the custom routes
    if (this.config.customRoutes) {
        var customRoutes = _.sortBy(this.config.customRoutes, 'url').reverse();
        customRoutes.forEach(function(route) {
            routes.push(route);
            routesAdded.push(route.url);
        });
    }

    // add the generic routes only if the url hasn't already been added
    // useful to overwrite generic routes
    genericRoutes.forEach(function(route) {
        if (routesAdded.indexOf(route.url) === -1) {
            routes.push(route);
        }
    });

    // actually attach the routes to the app
    routes = _.sortBy(routes, 'url').reverse();
    var _this = this;
    var routeUrl;
    routes.forEach(function(route) {
        if (route.url instanceof RegExp) {
            routeUrl = route.url;
        } else {
            routeUrl = "" + _this.baseURI + route.url;
        }
        _this.app[route.method](routeUrl, route.func);
    });

    /** redirect all the non-api requests to the ember app **/
    _this.app['get']('/*', function(req, res) {
        return res.redirect('#'+req.url);
    });
};

// overload to add your owns middlewares
Server.prototype.registerMiddlewares = function() {
    // this.app.use("/app", express["static"]("" + (path.resolve('.')) + "/public"));
    if (this.config.publicDirectory) {
        var publicDirectoryPath = (path.resolve('.')) + '/' + this.config.publicDirectory;
        this.logger.info('serving public directory from ', publicDirectoryPath);
        this.app.use("/", express["static"]("" + publicDirectoryPath));
    }
    // this.app.use("/", express["static"]("" + (path.resolve('.')) + "/backend/public"));
    this.app.use(express.urlencoded());
    this.app.use(express.json());
    var _this = this;
    this.app.use(function(req, res, next) {
        req.db = _this.db;
        req.logger = _this.logger;
        req.applicationName = _this.applicationName;
        req.config = _this.config;
        return next();
    });

    // enable CORS if needed
    if (this.config.enableCORS) {
        var cors = require('cors');
        this.logger.info("CORS is enabled");
        this.app.use(cors());
    }

    // file handling
    this.config.uploadDirectory = this.config.uploadDirectory || 'uploads';
    var uploadDirectory = this.config.uploadDirectory;<commit_msg>prepare droppring CORS module<commit_after>var express = require('express');
var genericRoutes = require('./engine');
var _ = require('underscore');
var path = require("path");
var multer = require('multer');

var fs = require('fs');
var mkdirp = require('mkdirp');

var bunyan = require('bunyan');

/*
Exemple:

var Eurekapi = require('eurekapi');

var server = new Eurekapi({
    name: 'projectName',
    version: 1,
    host: '0.0.0.0'
    port: 4000,
    enableCORS: false,
    database: {
        adapter: 'rdf',
        config: {
            store: 'virtuoso',
            graphURI: 'http://projectName.com'
        }
    },
    schemas: require('./schemas'),
    customRoutes: [
        {method: 'delete', url: '/nugget', func: deleteNugget}
    ],
    publicDirectory: '/dist'
});
server.start();
 */


var Server = function(config) {
    this.config = config;
    if (!this.config.name) {
        throw 'EurekaServer: name is required';
    }
    var logLevel = config.logLevel || 'info';
    this.logger = bunyan.createLogger({name: config.name, level: logLevel});
    this.apiVersion = this.config.version || 1;
    this.baseURI = this.config.baseURI || ("/api/" + config.version);
    this.applicationName = this.config.name;
    this.db = this.getDatabase();
    this.app = express();
    this.registerMiddlewares();

    var routesAdded = [];
    var routes = [];

    // add the custom routes
    if (this.config.customRoutes) {
        var customRoutes = _.sortBy(this.config.customRoutes, 'url').reverse();
        customRoutes.forEach(function(route) {
            routes.push(route);
            routesAdded.push(route.url);
        });
    }

    // add the generic routes only if the url hasn't already been added
    // useful to overwrite generic routes
    genericRoutes.forEach(function(route) {
        if (routesAdded.indexOf(route.url) === -1) {
            routes.push(route);
        }
    });

    // actually attach the routes to the app
    routes = _.sortBy(routes, 'url').reverse();
    var _this = this;
    var routeUrl;
    routes.forEach(function(route) {
        if (route.url instanceof RegExp) {
            routeUrl = route.url;
        } else {
            routeUrl = "" + _this.baseURI + route.url;
        }
        _this.app[route.method](routeUrl, route.func);
    });

    /** redirect all the non-api requests to the ember app **/
    _this.app['get']('/*', function(req, res) {
        return res.redirect('#'+req.url);
    });
};

// overload to add your owns middlewares
Server.prototype.registerMiddlewares = function() {
    // this.app.use("/app", express["static"]("" + (path.resolve('.')) + "/public"));
    if (this.config.publicDirectory) {
        var publicDirectoryPath = (path.resolve('.')) + '/' + this.config.publicDirectory;
        this.logger.info('serving public directory from ', publicDirectoryPath);
        this.app.use("/", express["static"]("" + publicDirectoryPath));
    }
    // this.app.use("/", express["static"]("" + (path.resolve('.')) + "/backend/public"));
    this.app.use(express.urlencoded());
    this.app.use(express.json());
    var _this = this;
    this.app.use(function(req, res, next) {
        req.db = _this.db;
        req.logger = _this.logger;
        req.applicationName = _this.applicationName;
        req.config = _this.config;
        return next();
    });

    // enable CORS if needed
    if (this.config.enableCORS) {
        this.logger.info("CORS is enabled");
        var cors = require('cors'); // XXX remove
        this.app.use(cors()); // TODO replace by below
        // app.use(function(req, res, next) {
        //   res.header("Access-Control-Allow-Origin", "*");
        //   res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        //   next();
        // });
    }

    // file handling
    this.config.uploadDirectory = this.config.uploadDirectory || 'uploads';
    var uploadDirectory = this.config.uploadDirectory;
{'fraction_non_alphanumeric': 0.16281800391389434, 'fraction_numerical': 0.0028701891715590348, 'mean_word_length': 2.5788982259570497, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5570886', 'n_tokens_mistral': 2379, 'n_tokens_neox': 2339, 'n_words': 510}