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 31, 2012 · 42 revisions

A pomelo project exists two app.js files, one is in game-server directory and the other is in web-server directory.They correspond to the game server and the web server respectively, and each of them is their server's entry. The following app.js configuration reference is only for the 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 project information. The main content is as follows:

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

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

app.start();

Firstly, it creates an application using pomelo; then it should make some basic configurations for the application, including setting application name and directory. After that it makes default configuration of pomelo. These configurations are necessary conditions to create a project, and finally you can start the application.

Variables and States Setting

Application variables can be accessed through the set and get methods. For example, to access the server object, the specific code is 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. Furthermore, users can call enabled and disabled methods to check states' existence, if exists returns true, otherwise returns false. For example, you want to activate or inactivate application's filter state and check its states' existence, and the specific code is as follows:

app.enable('filter');
app.enabled('filter'); //return true

app.disable('filter');
app.disabled('filter'); //return true

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

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

Rules of Configuration

The server's configuration is mainly done by the app.configure method, the parameters of the method are as follows:

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

The first two parameters are optional, the parameters' descriptions are as follows:

  • env: runtime environment, can be set as development, production or development | production.
  • serverType: server type, if set this parameter only this type of server do initialization function, otherwise all servers perform initialization function.
  • function: the specific initialization operation, it can be any js method.

Some configuration examples are as follows:

Example One

app.configure(function(){
});

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

Example Two

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

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

Example Three

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

This configuration only brings 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 configuration on a specific server:

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

Load Components

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

  • master: primarily responsible for starting the master server.
  • monitor: primarily responsible for starting each server's monitor service, the service is in charge of collecting information of servers and regularly pushing messages to the master, maintaining the master server's heartbeat connection.
  • proxy: 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: primarily mainly responsible for loading handlers in front-end server.
  • filter: primarily responsible for loading filters for requests service, including filters before and after the calling of rpc.
  • remote: primarily responsible for loading the back-end servers, and generates rpc server.
  • server: primarily responsible for starting the front-end server.
  • sync: primarily responsible for starting data synchronization service and provide external data synchronization service.
  • connection: primarily responsible for starting the service of user connection statistical data.

Most of components will be loaded by default, and users can have custom components depending on the application demands. Components loading can use the load method, for example:

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

Router Configuration

The router is mainly responsible for maintaining the routing information, routing calculation, routing result cache, switch routing strategies and update routing information according to the application demands. Users can customize different routing rules for different servers, and these can be configured in app.js. For example:

app.route('chat', routeUtil.chat);

Users can define routing rules for different servers, for example:

routeUtil.chat = function(session, msg, app, callback) {
    var chatServers = app.getServersByType('chat'); 
    if (!chatServers) {
     	callback(new Error('can not find chat servers.'));
		return;
    }
    var server = dispatcher.dispatch(session.rid, chatServers);
    callback(null, server.id);
};

The callback function returns the id of the server, and here uses the dispatcher on session.rid which just uses the hash processing to do server selection.

Filter Configuration

When a client request reaches the server, it is processed by filter chain and handlers, and finally generates a response back to the client. The handler implements the business logic, and the filter works for preparation and rehabilitation to facilitate code modularization and reuse. Pomelo itself provides some filters, and users can customize filters according to the application demands.

  • serial: mainly responsible for ensuring that all requests from the client to the server will be sent in order.
app.filter(pomelo.filters.serial());
  • time: mainly responsible for recording requests response time.
app.filter(pomelo.filters.time());
  • timeout: mainly responsible for monitoring requests response time, if the timeout happens it gives a warning.
app.filter(pomelo.filters.timeout());

Complete Reference Sample

var pomelo = require('pomelo');
var routeUtil = require('./app/util/routeUtil');
/**
 * Init app for client.
 */
var app = pomelo.createApp();
app.set('name', 'chatofpomelo');
app.defaultConfiguration();


// app configure
app.configure('production|development', function() {
	// route configures
	app.route('chat', routeUtil.chat);
	app.route('connector', routeUtil.connector);
        
        // remote configures
	app.set('remoteConfig', {
		cacheMsg: true, 
		interval: 30
	});

        // filter configures
	app.filter(pomelo.filters.timeout());	
       
       // mysql configures
        app.loadConfig('mysql', app.get('dirname') + '/config/mysql.json');
});

// start app
app.start();
Clone this wiki locally