LoginSignup
8
7

More than 5 years have passed since last update.

Meteor environmentの調査メモ

Posted at

概要

Meteorアプリケーションの実行時に設定できる環境変数について調べたメモです。
なお、いくつかの環境変数は動作確認が取れていません。

環境

  • Windows7 (64bit)
  • Meteor 1.2.1

参考

環境変数

環境変数はprocess.envオブジェクトで管理されます。(これはnode.jsの機能です。)
たとえば、アプリケーションの実行前に下記の環境変数を登録すると、アプリケーションからはprocess.env.MY_ENVとして参照することができます。

> set MY_ENV=foo
> meteor

process.envオブジェクトにユーザーの環境変数を追加する処理はrun-app.jsで行われています。

run-app.js
_computeEnvironment: function () {
  var self = this;
  var env = _.extend({}, process.env);

  env.PORT = self.port;
  env.ROOT_URL = self.rootUrl;
  env.MONGO_URL = self.mongoUrl;

...省略...

  env.METEOR_PRINT_ON_LISTEN = 'true';

  // use node's path module and not 'files.js' because NODE_PATH is an
  // environment variable passed to an external process and needs to be
  // constructed in the OS-style.
  var path = require('path');
  env.NODE_PATH =
    self.nodePath.join(path.delimiter);

  return env;
},

ROOT_URL

アプリケーションのURLをROOT_URL環境変数で指定します。

ROOT_URL
> set ROOT_URL=http://foo.example.com

Meteor.absoluteUrl

The server reads from the ROOT_URL environment variable to determine where it is running.

PORT

アプリケーションがリッスンするhttpポートをPORT環境変数で指定します。デフォルトは3000です。
(:exclamation: Windowsでは反映されませんでした。)

specifying the HTTP port for the application to listen on

PORT
> set PORT=8000

PORT環境変数の代わりに--portを使うことができます。
(こちらは反映されました。)

> meteor --port=8000

METEOR_SETTINGS

アプリケーション固有の設定情報をMETEOR_SETTINGS環境変数で指定します。
(:exclamation: Windowsでは反映されませんでした。)

この環境変数にはjson形式の値を設定する必要があります。

METEOR_SETTINGS
> set METEOR_SETTINGS="{\"public\":{\"for\":\"bar\"},\"private\":{\"hoge\":\"fuga\"}}"

linux系では下記のようにsettings.jsonファイルの内容を設定することもできます。

$ export METEOR_SETTINGS="$(cat settings.json)"

METEOR_SETTINGS環境変数の代わりに--settingsを使用することができます。
(こちらは反映されました。)

> meteor --settings=settings.json

Meteor.settings

When running your server directly (e.g. from a bundle), you instead specify settings by putting the JSON directly into the METEOR_SETTINGS environment variable.

参照箇所のソースコード(抜粋)

server_environment.js
Meteor = {
  isClient: false,
  isServer: true,
  isCordova: false
};

Meteor.settings = {};

if (process.env.METEOR_SETTINGS) {
  try {
    Meteor.settings = JSON.parse(process.env.METEOR_SETTINGS);
  } catch (e) {
    throw new Error("METEOR_SETTINGS are not valid JSON: " + process.env.METEOR_SETTINGS);
  }
}

// Make sure that there is always a public attribute
// to enable Meteor.settings.public on client
if (! Meteor.settings.public) {
    Meteor.settings.public = {};
}

HTTP_FORWARDED_COUNT

プロキシーサーバー経由でアクセスするクライアントのIPアドレスを取得したい場合に設定します。
(:exclamation: 動作未検証です。)

HTTP_FORWARDED_COUNT
> set HTTP_FORWARDED_COUNT=1

Meteor.onConnection

Set HTTP_FORWARDED_COUNT to an integer representing the number of proxies in front of your server. For example, you'd set it to "1" when your server was behind one proxy.

MAIL_URL

アプリケーションからメールを送信するにはMAIL_URL環境変数にsmtpサーバーを指定します。

MAIL_URL
> set MAIL_URL=smtp://your_email:password@smtp.gmail.com:25/

Email.send

The server reads from the MAIL_URL environment variable to determine how to send mail.
the MAIL_URL environment variable should be of the form smtp://USERNAME:PASSWORD@HOST:PORT/.

メールを送るためにemailパッケージを追加します。

add
> meteor add email

Changes to your project's package version selections:

email  added, version 1.0.8

email: Send email messages

メールの送信はEmail APIを使用します。

Email
Email.send({
    to: to,
    from: from,
    subject: subject,
    text: text
  });

NODE_ENV

nodejsの実行環境の切り替えをNODE_ENV環境変数で指定します。

NODE_ENV
> set NODE_ENV=production
NODE_ENV
> set NODE_ENV=development

NODE_OPTIONS

History.md
v0.5.3, 2013-Jan-07

The NODE_OPTIONS environment variable can be used to pass command-line flags to node (eg, --debug or --debug-brk to enable the debugger).

NODE_OPTIONS
> set NODE_OPTIONS=--debug-brk

Command line

To pass additional options to Node.js use the NODE_OPTIONS environment variable. For example: NODE_OPTIONS='--debug' or NODE_OPTIONS='--debug-brk'

Node Inspector

install
> npm install -g node-inspector
run
> node-inspector
Node Inspector v0.12.3
Visit http://127.0.0.1:8080/?ws=127.0.0.1:8080&port=5858 to start debugging.

Meteor 1.0以降は下記のdebugおよびshellコマンドでデバッグを行うことができます。

> meteor debug
> meteor shell

DDP_DEFAULT_CONNECTION_URL

DDP analyzerでserver/clientのメッセージをモニタリングする場合に指定するURLをDDP_DEFAULT_CONNECTION_URL環境変数に設定します。

DDP_DEFAULT_CONNECTION_URL
> set DDP_DEFAULT_CONNECTION_URL=http://localhost:3030

Introduction to DDP

DDP is the heart of MeteorJS and it’s the protocol Meteor uses to communicate between the client and the server.
DDP is an acronym for Distributed Data Protocol.

Discover Meteor DDP in Realtime

DDP is the protocol Meteor uses to communicate between client and the sever.
It’s a very simple and tiny protocol (learn DDP). But it’s somewhat tough to look at DDP messages being generated while you’re using your app.

Meteor DDP Analyzer

install
> npm install -g ddp-analyzer
run
> ddp-analyzer-proxy

DDP Proxy Started on port: 3030
===============================
Export following env. variables and start your meteor app
  export DDP_DEFAULT_CONNECTION_URL=http://localhost:3030
  meteor

DISABLE_WEBSOCKETS

Websocketsを使用しないよう設定することができます。
(:exclamation: 動作未検証です。)

History.md
v0.6.4, 2013-Jun-10

If you set the DISABLE_WEBSOCKETS environment variable, browsers will not attempt to connect to your app using Websockets.

DISABLE_WEBSOCKETS
> set DISABLE_WEBSOCKETS=1

Websockets

It's not documented, but you can disable websockets with the following environment variable

参照箇所のソースコード(抜粋)

stream_server.js
// If you know your server environment (eg, proxies) will prevent websockets
// from ever working, set $DISABLE_WEBSOCKETS and SockJS clients (ie,
// browsers) will not waste time attempting to use them.
// (Your server will still have a /websocket endpoint.)
if (process.env.DISABLE_WEBSOCKETS) {
  serverOptions.websocket = false;
} else {
  serverOptions.faye_server_options = {
    extensions: websocketExtensions()
  };
}

MONGO_URL

MongoDBのデータベースに接続するURLをMONGO_URL環境変数で指定します。

MONGO_URL
> set MONGO_URL=mongodb://localhost:27017/db_name

ユーザー認証が必要な場合

MONGO_URL
> set MONGO_URL=mongodb://user:password@localhost:27017/db_name

レプリケーション環境では下記のようにカンマ区切りで指定します。
下記は同一ホストに3つのMongoDBインスタンスを立ち上げている場合の例です。

MONGO_URL
> set MONGO_URL=mongodb://user:password@localhost:30001,localhost:30002,localhost:30003/db_name

MONGO_OPLOG_URL

MongoDBのOPLOGを格納するデータベースに接続するURLをMONGO_OPLOG_URL環境変数で指定します。
(:exclamation: 動作未検証です。)

History.md
v0.7.0, 2013-Dec-17

Oplog tailing is automatically enabled in development mode with meteor run, and can be enabled in production with the MONGO_OPLOG_URL environment variable.

oplogを保存しているlocalデータベースを参照できる専用のユーザーを追加します。

cluster:PRIMARY> db.createUser({user: "oplogger", pwd: "oplogpass", roles: [{role: "read", db: "local"}]})
MONGO_OPLOG_URL
> set MONGO_OPLOG_URL=mongodb://oplogger:oplogpass@localhost:30001,localhost:30002,localhost:30003/local?authSource=admin

Oplog Observe Driver

Oplog tailing is automatically enabled in development mode with meteor run, and can be enabled in production with the MONGO_OPLOG_URL environment variable.

参照箇所のソースコード(抜粋)

remote_collection_driver.js
// Create the singleton RemoteCollectionDriver only on demand, so we
// only require Mongo configuration if it's actually used (eg, not if
// you're only trying to receive data from a remote DDP server.)
MongoInternals.defaultRemoteCollectionDriver = _.once(function () {
  var connectionOptions = {};

  var mongoUrl = process.env.MONGO_URL;

  if (process.env.MONGO_OPLOG_URL) {
    connectionOptions.oplogUrl = process.env.MONGO_OPLOG_URL;
  }

  if (! mongoUrl)
    throw new Error("MONGO_URL must be set in environment");

  return new MongoInternals.RemoteCollectionDriver(mongoUrl, connectionOptions);
});

OPLOG_URL

MeteorのSmart Collectionsというパッケージを使用する場合に設定する環境変数のようです。このパッケージは現在"This is a archived project"という状態です。
(:exclamation: 動作未検証です。)

Meteor Smart Collections

Smart Collection is now retired & Meteor's Collection implementation has fixes for most of the performance bottlenecks.

METEOR_WATCH_FORCE_POLLING

Meteorがファイルの変更を検知する間隔を変更することができます。間隔は後述するMETEOR_WATCH_POLLING_INTERVAL_MS環境変数で指定します。
通常、この設定は不要ですが、NFSやVagrantの共有フォルダを使用している場合などファイルシステムがファイル変更の検知をサポートしていない場合に使用するようです。
(:exclamation: 動作未検証です。)

History.md
v1.0.2, 2014-Dec-19

On file systems that do not support these events (NFS, Vagrant Virtualbox shared folders, etc), file changes will only be detected every 5 seconds; to detect changes more often in these cases (but use more CPU), set the METEOR_WATCH_FORCE_POLLING environment variable.

METEOR_WATCH_FORCE_POLLING
> set METEOR_WATCH_FORCE_POLLING=1

METEOR_WATCH_POLLING_INTERVAL_MS

METEOR_WATCH_POLLING_INTERVAL_MS
> set METEOR_WATCH_POLLING_INTERVAL_MS=500

参照箇所のソースコード(抜粋)

safe-pathwatcher.js
// Set METEOR_WATCH_FORCE_POLLING environment variable to a truthy value to
// force the use of files.watchFile instead of pathwatcher.watch.
// Enabled on Mac and Linux and disabled on Windows by default.
var PATHWATCHER_ENABLED = !process.env.METEOR_WATCH_FORCE_POLLING;
if (process.platform === "win32") {
  PATHWATCHER_ENABLED = false;
}

var DEFAULT_POLLING_INTERVAL =
      ~~process.env.METEOR_WATCH_POLLING_INTERVAL_MS || 5000;


var NO_PATHWATCHER_POLLING_INTERVAL =
      ~~process.env.METEOR_WATCH_POLLING_INTERVAL_MS || 500;

HTTP_PROXY / HTTPS_PROXY

Using Meteor behind a proxy

Like a lot of other command-line software, the Meteor tool reads the proxy configuration from the HTTP_PROXY and HTTPS_PROXY environment variables (the lower case variants work, too).

METEOR_OFFLINE_CATALOG

パッケージカタログ(と呼ばれるデータベース)の更新を行わないように指定します。
(:exclamation: 動作未検証です。)

METEOR_OFFLINE_CATALOG
> set METEOR_OFFLINE_CATALOG=1

参照箇所のソースコード(抜粋)

main.js
// Initialize the server catalog. Among other things, this is where we get
// release information (used by springboarding). We do not at this point talk
// to the server and refresh it.
catalog.official.initialize({
  offline: !!process.env.METEOR_OFFLINE_CATALOG
});

METEOR_PROFILE

アプリケーションのビルドにかかる時間を計測します。METEOR_PROFILE環境変数には閾値をミリ秒で指定します。
デフォルトは100ミリ秒で、ビルドに100ミリ秒以上かかった項目が計上されます。

METEOR_PROFILE
> set METEOR_PROFILE=50

参照箇所のソースコード(抜粋)

profile.js
// Tiny profiler
//
// Enable by setting the environment variable `METEOR_PROFILE`.

var enabled = !!process.env['METEOR_PROFILE'];
var filter = ~ ~process.env['METEOR_PROFILE'] || 100; // ms

METEOR_LOG

METEOR_LOG環境変数に"debug"と指定すると標準出力にデバッグログが出力されます。

METEOR_LOG
> set METEOR_LOG=debug

参照箇所のソースコード(抜粋)

console.js
self._logThreshold = LEVEL_CODE_INFO;
var logspec = process.env.METEOR_LOG;
if (logspec) {
  logspec = logspec.trim().toLowerCase();
  if (logspec == 'debug') {
    self._logThreshold = LEVEL_CODE_DEBUG;
  }
}

METEOR_PRINT_CONSTRAINT_SOLVER_INPUT

デバッグ用の設定のようです。
(:exclamation: 動作未検証です。)

History.md
v1.1, 2015-Mar-31

Setting the METEOR_PRINT_CONSTRAINT_SOLVER_INPUT environment variable prints information useful for diagnosing constraint solver bugs.

METEOR_PRINT_CONSTRAINT_SOLVER_INPUT
> set METEOR_PRINT_CONSTRAINT_SOLVER_INPUT=1

参照箇所のソースコード(抜粋)

constraint-solver.js
if (Meteor.isServer &&
    process.env['METEOR_PRINT_CONSTRAINT_SOLVER_INPUT']) {
  console.log("CONSTRAINT_SOLVER_INPUT = ");
  console.log(JSON.stringify(input.toJSONable(), null, 2));
}

USE_JSESSIONID

スティッキーセッション機能のためにJSESSIONIDクッキーを有効にします。
(:exclamation: 動作未検証です。)

History.md
v0.6.6, 2013-Oct-10

Add support for JSESSIONID cookies for sticky sessions. Set the USE_JSESSIONID environment variable to enable placing a JSESSIONID cookie on sockjs requests.

USE_JSESSIONID
> set USE_JSESSIONID=1

参照箇所のソースコード(抜粋)

stream_server.js
// Set the USE_JSESSIONID environment variable to enable setting the
// JSESSIONID cookie. This is useful for setting up proxies with
// session affinity.
jsessionid: !!process.env.USE_JSESSIONID
8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7