<commit_before>'use strict'

var { ResourceLoader, ModelManager, Repo } = require('mzen');
var ServerRemoteObject = require('./remote-object');
var ServerAcl = require('./acl');
var ServerRepoMixin = require('./repo-mixin');
var express = require('express');
var bodyParser = require('body-parser');
var clone = require('clone');
var fs = require('fs');
var merge = require('merge');

class Server
{
  constructor(options = {})
  {
    this.modelManager = new ModelManager(options.model);
    this.config = options ? options : {};
    this.config.path = this.config.path ? this.config.path : '/api';
    this.config.port = this.config.port ? this.config.port : 3838;
    // Default appDir directory is the same directory as the executed script
    this.config.appDir = this.config.appDir ? this.config.appDir : '';
    this.config.initDirName = this.config.initDirName ? this.config.initDirName : '/init';
    this.config.aclDirName = this.config.aclDirName ? this.config.aclDirName : '/acl';

    this.app = express();
    this.server = null;
    this.router = express.Router();
    this.aclRoleAssessor = {};
    this.setLogger(console);
  }
  setLogger(logger)
  {
    this.logger = logger;
    return this;
  }
  async init()
  {
    // Add app model directory based on value of appDir if no model directories have been configured
    if (this.modelManager.config.modelDirs.length == 0) {
      this.modelManager.config.modelDirs.push(this.config.appDir + '/model');
    }
    // Add default model directory - this is model functionality provided by the mzen-server package
    this.modelManager.config.modelDirs.unshift(__dirname + '/model');

    await this.modelManager.init();
    await this.loadResources();
    await this.registerServiceEndpoints();
    await this.registerRepoApiEndpoints();
  }
  bootInitScripts()
  {
    const initDirectoryPath = this.config.appDir + '/' + this.config.initDirName;
    var initScripts = {};
    if (fs.existsSync(initDirectoryPath)) {
      var filenames = fs.readdirSync(initDirectoryPath);
      filenames.forEach(function(filename){
        if (fs.lstatSync(initDirectoryPath + '/' + filename).isDirectory() || filename[0] == '.') return;
        initScripts[filename] = initDirectoryPath + '/' + filename;
      });
    }
    var filenames = Object.keys(initScripts);

    // Sort by filename
    // - the order of execution is important so files should be named to give the correct order
    filenames.sort(function(a, b) {
      return (a > b) ? 1 : 0;
    });

    var filePaths = [];
    filenames.forEach(function(filename){
      filePaths.push(initScripts[filename]);
    });

    var promise = Promise.resolve();
    filePaths.forEach((filePath) => {
      promise.then(() => {
        let initFunction = require(filePath);
        promise = Promise.resolve();
        if (typeof initFunction == 'function') {
          let result = initFunction.call(this);
          if (result && result.constructor && result.constructor instanceof Promise) {
            promise = result;
          }
        }
        return promise;
      });
    });

    return promise;
  }
  async loadResources()
  {
    const loader = new ResourceLoader();

    const assessors = loader.getResources([this.config.appDir], this.config.aclDirName + '/assessor');
    for (let assessorName in assessors) {
      let roleAssessor = new assessors[assessorName]();
      this.aclRoleAssessor[roleAssessor.role] = roleAssessor;
    }

    return this;
  }
  registerServiceEndpoints()
  {
    const services = this.modelManager.services;
    for (let serviceName in services)
    {
      const apiConfig = services[serviceName].config.api ? services[serviceName].config.api : {};
      const repoAclConfig = apiConfig.acl ? apiConfig.acl : {};
      const endpointsEnable = apiConfig.endpointsEnable ? apiConfig.endpointsEnable : {};
      var endpoints = apiConfig.endpoints ? apiConfig.endpoints : {};

      // Remove any endpoints that have been disabled
      for (var endpointName in endpoints) {
        if (endpointsEnable[endpointName] === false) {
          delete endpoints[endpointName];
        }
      }
<commit_msg>App role assessor classes are located in /acl/role-assessor by default<commit_after>'use strict'

var { ResourceLoader, ModelManager, Repo } = require('mzen');
var ServerRemoteObject = require('./remote-object');
var ServerAcl = require('./acl');
var ServerRepoMixin = require('./repo-mixin');
var express = require('express');
var bodyParser = require('body-parser');
var clone = require('clone');
var fs = require('fs');
var merge = require('merge');

class Server
{
  constructor(options = {})
  {
    this.modelManager = new ModelManager(options.model);
    this.config = options ? options : {};
    this.config.path = this.config.path ? this.config.path : '/api';
    this.config.port = this.config.port ? this.config.port : 3838;
    // Default appDir directory is the same directory as the executed script
    this.config.appDir = this.config.appDir ? this.config.appDir : '';
    this.config.initDirName = this.config.initDirName ? this.config.initDirName : '/init';
    this.config.aclDirName = this.config.aclDirName ? this.config.aclDirName : '/acl';

    this.app = express();
    this.server = null;
    this.router = express.Router();
    this.aclRoleAssessor = {};
    this.setLogger(console);
  }
  setLogger(logger)
  {
    this.logger = logger;
    return this;
  }
  async init()
  {
    // Add app model directory based on value of appDir if no model directories have been configured
    if (this.modelManager.config.modelDirs.length == 0) {
      this.modelManager.config.modelDirs.push(this.config.appDir + '/model');
    }
    // Add default model directory - this is model functionality provided by the mzen-server package
    this.modelManager.config.modelDirs.unshift(__dirname + '/model');

    await this.modelManager.init();
    await this.loadResources();
    await this.registerServiceEndpoints();
    await this.registerRepoApiEndpoints();
  }
  bootInitScripts()
  {
    const initDirectoryPath = this.config.appDir + '/' + this.config.initDirName;
    var initScripts = {};
    if (fs.existsSync(initDirectoryPath)) {
      var filenames = fs.readdirSync(initDirectoryPath);
      filenames.forEach(function(filename){
        if (fs.lstatSync(initDirectoryPath + '/' + filename).isDirectory() || filename[0] == '.') return;
        initScripts[filename] = initDirectoryPath + '/' + filename;
      });
    }
    var filenames = Object.keys(initScripts);

    // Sort by filename
    // - the order of execution is important so files should be named to give the correct order
    filenames.sort(function(a, b) {
      return (a > b) ? 1 : 0;
    });

    var filePaths = [];
    filenames.forEach(function(filename){
      filePaths.push(initScripts[filename]);
    });

    var promise = Promise.resolve();
    filePaths.forEach((filePath) => {
      promise.then(() => {
        let initFunction = require(filePath);
        promise = Promise.resolve();
        if (typeof initFunction == 'function') {
          let result = initFunction.call(this);
          if (result && result.constructor && result.constructor instanceof Promise) {
            promise = result;
          }
        }
        return promise;
      });
    });

    return promise;
  }
  async loadResources()
  {
    const loader = new ResourceLoader();

    const assessors = loader.getResources([this.config.appDir], this.config.aclDirName + '/role-assessor');
    for (let assessorName in assessors) {
      let roleAssessor = new assessors[assessorName]();
      this.aclRoleAssessor[roleAssessor.role] = roleAssessor;
    }

    return this;
  }
  registerServiceEndpoints()
  {
    const services = this.modelManager.services;
    for (let serviceName in services)
    {
      const apiConfig = services[serviceName].config.api ? services[serviceName].config.api : {};
      const repoAclConfig = apiConfig.acl ? apiConfig.acl : {};
      const endpointsEnable = apiConfig.endpointsEnable ? apiConfig.endpointsEnable : {};
      var endpoints = apiConfig.endpoints ? apiConfig.endpoints : {};

      // Remove any endpoints that have been disabled
      for (var endpointName in endpoints) {
        if (endpointsEnable[endpointName] === false) {
          delete endpoints[endpointName];
        }
      }

{'fraction_non_alphanumeric': 0.1269689737470167, 'fraction_numerical': 0.0019093078758949881, 'mean_word_length': 3.100293542074364, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '6221611', 'n_tokens_mistral': 2422, 'n_tokens_neox': 2401, 'n_words': 565}