Skip to content
This repository has been archived by the owner on Sep 25, 2023. It is now read-only.

Reference configuration of app.js

py8765 edited this page Oct 25, 2012 · 42 revisions

In pomelo there are two app.js, one is in the game-server directory and the other is in web-server directory.They are corresponding to the game server and the web server, and each of them is server's entry. The following app.js configuration reference is just for game server.

Entrance of Application Setting

The app.js is the entrance of pomelo project.When using the pomelo command line to create a new project, a default app.js file will be generated according to the basic information of the project. Its main content is as follows:

var pomelo = require('pomelo');
var app = pomelo.createApp();

app.set('name', 'nameofproject');
app.set('dirname', __dirname);
app.defaultConfiguration();

app.start();

Variables and States Setting

Application variables can be accessed through the set and get methods, for example, to access the server object, and the specific code as follows:

app.set('server',server);
var server = app.get('server');

Application states information can be activated and inactivated by the enable and disable methods, for example, you want to activate or inactivate application's schedulerService state.Also, the user can use enabled and disabled methods to judge whether the state exists, if exists returns true, otherwise returns false. Specific code as follows:

app.enable('filter');
app.enabled('filter'); //返回true
app.disable('filter');
app.disabled('filter'); //返回true

Users can load configuration files and services through loadConfig and loadService methods, after loading the file parameters will be mounted directly to the application object. For example to load mysql.json file, the specific code as follows:

{
  "development":
    {
      "host":"127.0.0.1",
      "port":"3306",
      "database":"pomelo" 
    }
}
app.loadConfig('mysql.json');
var host = app.mysql.host; //返回 127.0.0.1
app.loadService('scheduleService');

Rules of Configuration

The server configuration is mainly done by the configure () method, a complete app.configure configuration parameters are as follows:

app.configure([env], [serverType], [function]);

The first two parameters are optional, the following parameter description:

  • env: Runtime environment, can be set to development, production or development | production.
  • serverType: Server type, if set this parameter only do initialization on this type of server, otherwise all servers perform initialization function.
  • function: The specific initialization operation, the internal can contain any js method.

Some configuration examples are as follows:

Example One

app.configure(function(){
});

This configuration will bring all servers under all modes (development / production) into effect.

Example Two

app.configure('development', function(){
});

This configuration will only bring all servers under a fixed mode into effect.

Example Three

app.configure('development', 'chat', function(){
});

This configuration will only bring chat servers under development mode into effect.

Initial Content Examples

Users can make different configurations for different servers according to various demands of the application, for example to configure the startup file of mysql in the global

app.configure('development|production', function(){
     app.set('mysql', app.get('dirname')+'/config/mysql.json');
});

Furthermore you can make configurations on a specific server, such as:

var initArea = function(){
   //area init
};
app.configure('development|production', 'area', function(){
     initArea();
});

Load Components

Each component has a life cycle, and it is usually loaded during the initialization of application. The components of pomelo are as follows: master, monitor, filter, proxy, handler, remote, server, sync and connection, and their main function are as follows:

  • master: master component is primarily responsible for starting the master server.
  • monitor: monitor component is primarily responsible for starting each server monitor service, the service is responsible for collecting information of servers and regularly pushing the message to the master, maintaining the master server heartbeat connection.
  • proxy: proxy component is primarily responsible for generating server rpc client, multiple server processes exist in the system, between different server processes to communicate with each other through rpc call(master server exclude).
  • handler: handler component is primarily mainly responsible for loading the file the handler directory of front-end server.
  • filter: filter component is primarily responsible for loading the filter for requests service, including the filter after the calling of rpc and pre-filter before the calling of rpc.
  • remote: remote component is primarily responsible for loading the back-end servers, and generates rpc server.
  • server: server component is primarily responsible for the start of the front-end server.
  • sync: sync component is primarilyresponsible for the start data synchronization module and provide external data synchronization service.
  • connection: connection component is primarily responsible for starting the service of user connection statistical data.

Most of components will be loaded by default.Components loading can use the load method, for example:

app.load(pomelo.proxy, [options]);  //[options] optional

Users can have custom components depending on the application needs, custom components design can refer to the Custom Component Development Reference.

Router Configuration

The router is mainly responsible for maintaining the routing information, route calculation, route result cache, switch routing strategies and update routing information according to the application demands. The user can customize different routing rules for different servers, and these can be configured in app.js. Such as chat server configuration routing rules:

Clone this wiki locally