<commit_before>'use strict';

import cluster from 'cluster';
import fs from 'fs';
import domain from 'domain';
import os from 'os';
import http from 'http';

let websocket = think.require('websocket');

export default class extends think.base {
  /**
   * dispath route
   * @return {} []
   */
  async dispatcher(){
    await this.hook('resource_check');
    await this.hook('route_parse');
  }
  /**
   * exec logic
   * @return {Promise} []
   */
  execLogic(){
    let name = `${this.http.module}/${think.dirname.logic}/${this.http.controller}`;
    let cls = think.require(name, true);
    if (!cls) {
      return Promise.resolve();
    }
    let instance = new cls(this.http);
    let action = this.http.action;
    if (think.isFunction(instance[`${action}Action`])) {
      return this.action(instance, action);
    }
    //call action
    else if (think.isFunction(instance.__call)) {
      return this.action(instance, '__call');
    }
    //only has before method
    else if(think.isFunction(instance.__before)){
      return instance.__before(instance);
    }
  }
  /**
   * exec controller
   * @return {Promise} []
   */
  execController(){
    let http = this.http;
    let name = `${http.module}/${think.dirname.controller}/${http.controller}`;
    let cls = think.require(name, true);
    if (cls) {
      return this.execAction(new cls(http));
    }
    http.error = new Error(think.locale('CONTROLLER_NOT_FOUND', http.controller, http.url));
    return think.statusAction(404, http);
  }
  /**
   * exec action
   * @param  {Object} controller [controller instance]
   * @param  {Boolean} call       [is call controller]
   * @return {Promise}            []
   */
  execAction(controller, isCall){
    if(isCall){
      return this.action(controller, think.config('call_action'));
    }
    let http = this.http;
    //if is rest api, rewrite action
    if(controller.__isRest){
      http.action = http.method.toLowerCase();
    }
    let action = http.action;
    let actionWithSuffix = `${action}Action`;
    //action is exist
    if(think.isFunction(controller[actionWithSuffix])){
      return this.action(controller, action);
    }
    //call action
    if(think.isFunction(controller.__call)){
      return this.action(controller, '__call');
    }
    http.error = new Error(think.locale('ACTION_NOT_FOUND', action, http.url));
    return this.statusAction(404, http);
  }
  /**
   * exec 
   * @return {Promise} []
   */
  async exec(){
    await this.execLogic();
    //http is end
    if (this.http._isEnd) {
      return think.prevent();
    }
    return this.execController();
  }
  /**
   * run
   * @return {} []
   */
  run(){
    let http = this.http;
    http.header('X-Powered-By', `thinkjs-${think.version}`);
    //service off
    if(!think.config('service_on')){
      http.error = new Error(think.locale('SERVICE_UNAVAILABLE'));
      return think.statusAction(502, http);
    }
    //deny access by ip + port
    if (think.config('proxy_on') && http.host !== http.hostname && !http.websocket) {
      http.error = new Error(think.locale('DISLLOW_PORT'));
      return think.statusAction(403, http);
    }
    let instance = domain.create();
    instance.on('error', err => think.statusAction(500, http));
    instance.run(async () => {
      try{
        await this.dispatcher();
        //set module config
        this.http._config = think.extend({}, think.getModuleConfig(this.http.module));
        await this.hook('app_begin');
        await this.exec();
        await this.hook('app_end');
      }catch(err){
        http.error = err;
        think.statusAction(500, http);
      }
    });
  }
  /**
   * create server
   * @return {} []
   */
  static createServer(){
    let handle = think.config('create_server');
    let host = think.config('host');
    let port = think.port || think.config('port'); 
    //createServer callback
    let callback = async (req, res) => {
      let http = await think.http(req, res);
      try{
        return new this(http).run();
      }catch(e){
        think.log(e);
      }
    };
    //define createServer in application
    if (handle) {
      handle(callback, port, host, this);
    }else{
      //create server
      let server = http.createServer(callback);
      if (think.config('websocket.on')) {
        let instance = new websocket(server, this);
        instance.run();
      }
      server.listen(port, host);
      this.logPid(port);
    }
    think.log('Server running at http://' + (host || '127.0.0.1') + ':' + port + '/', 'THINK');
    think.log('ThinkJS Version: ' + think.version, 'THINK');
    think.log('App Env: ' + think.env, 'THINK');
  }
  /**
   * cli mode
   * @return {} []
   */
  static async cli(){
    let http = await think.http(think.cli);
    return new this(http).run();
  }
  /**
   * load process id
   * @return {} []
   */
  static logPid(port){
    if (!cluster.isMaster || !think.config('log_pid')) {
      return;<commit_msg>add magenta color for log app env<commit_after>'use strict';

import cluster from 'cluster';
import fs from 'fs';
import domain from 'domain';
import os from 'os';
import http from 'http';

let websocket = think.require('websocket');

export default class extends think.base {
  /**
   * dispath route
   * @return {} []
   */
  async dispatcher(){
    await this.hook('resource_check');
    await this.hook('route_parse');
  }
  /**
   * exec logic
   * @return {Promise} []
   */
  execLogic(){
    let name = `${this.http.module}/${think.dirname.logic}/${this.http.controller}`;
    let cls = think.require(name, true);
    if (!cls) {
      return Promise.resolve();
    }
    let instance = new cls(this.http);
    let action = this.http.action;
    if (think.isFunction(instance[`${action}Action`])) {
      return this.action(instance, action);
    }
    //call action
    else if (think.isFunction(instance.__call)) {
      return this.action(instance, '__call');
    }
    //only has before method
    else if(think.isFunction(instance.__before)){
      return instance.__before(instance);
    }
  }
  /**
   * exec controller
   * @return {Promise} []
   */
  execController(){
    let http = this.http;
    let name = `${http.module}/${think.dirname.controller}/${http.controller}`;
    let cls = think.require(name, true);
    if (cls) {
      return this.execAction(new cls(http));
    }
    http.error = new Error(think.locale('CONTROLLER_NOT_FOUND', http.controller, http.url));
    return think.statusAction(404, http);
  }
  /**
   * exec action
   * @param  {Object} controller [controller instance]
   * @param  {Boolean} call       [is call controller]
   * @return {Promise}            []
   */
  execAction(controller, isCall){
    if(isCall){
      return this.action(controller, think.config('call_action'));
    }
    let http = this.http;
    //if is rest api, rewrite action
    if(controller.__isRest){
      http.action = http.method.toLowerCase();
    }
    let action = http.action;
    let actionWithSuffix = `${action}Action`;
    //action is exist
    if(think.isFunction(controller[actionWithSuffix])){
      return this.action(controller, action);
    }
    //call action
    if(think.isFunction(controller.__call)){
      return this.action(controller, '__call');
    }
    http.error = new Error(think.locale('ACTION_NOT_FOUND', action, http.url));
    return this.statusAction(404, http);
  }
  /**
   * exec 
   * @return {Promise} []
   */
  async exec(){
    await this.execLogic();
    //http is end
    if (this.http._isEnd) {
      return think.prevent();
    }
    return this.execController();
  }
  /**
   * run
   * @return {} []
   */
  run(){
    let http = this.http;
    http.header('X-Powered-By', `thinkjs-${think.version}`);
    //service off
    if(!think.config('service_on')){
      http.error = new Error(think.locale('SERVICE_UNAVAILABLE'));
      return think.statusAction(502, http);
    }
    //deny access by ip + port
    if (think.config('proxy_on') && http.host !== http.hostname && !http.websocket) {
      http.error = new Error(think.locale('DISLLOW_PORT'));
      return think.statusAction(403, http);
    }
    let instance = domain.create();
    instance.on('error', err => think.statusAction(500, http));
    instance.run(async () => {
      try{
        await this.dispatcher();
        //set module config
        this.http._config = think.extend({}, think.getModuleConfig(this.http.module));
        await this.hook('app_begin');
        await this.exec();
        await this.hook('app_end');
      }catch(err){
        http.error = err;
        think.statusAction(500, http);
      }
    });
  }
  /**
   * create server
   * @return {} []
   */
  static createServer(){
    let handle = think.config('create_server');
    let host = think.config('host');
    let port = think.port || think.config('port'); 
    //createServer callback
    let callback = async (req, res) => {
      let http = await think.http(req, res);
      try{
        return new this(http).run();
      }catch(e){
        think.log(e);
      }
    };
    //define createServer in application
    if (handle) {
      handle(callback, port, host, this);
    }else{
      //create server
      let server = http.createServer(callback);
      if (think.config('websocket.on')) {
        let instance = new websocket(server, this);
        instance.run();
      }
      server.listen(port, host);
      this.logPid(port);
    }
    think.log('Server running at http://' + (host || '127.0.0.1') + ':' + port + '/', 'THINK');
    think.log('ThinkJS Version: ' + think.version, 'THINK');
    think.log(colors => {
      return 'App Env: ' + colors.magenta(think.env);
    }, 'THINK');
  }
  /**
   * cli mode
   * @return {} []
   */
  static async cli(){
    let http = await think.http(think.cli);
    return new this(http).run();
  }
  /**
   * load process id
   * @return {} []
   */
  static logPid(port){
    if (!cluster.isMaster || !think.config('log_pid')) {
      return;
{'fraction_non_alphanumeric': 0.16366185897435898, 'fraction_numerical': 0.004807692307692308, 'mean_word_length': 2.61121157323689, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '58756', 'n_tokens_mistral': 3173, 'n_tokens_neox': 3170, 'n_words': 701}