Skip to content

Commit

Permalink
Added: Environment variables support
Browse files Browse the repository at this point in the history
  • Loading branch information
igormukhingmailcom committed Aug 24, 2015
1 parent d0fe915 commit 5c3e633
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 13 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,37 @@ secret: 8e0c5e97f91e1a8dde85702ffadff48e8488fda46c457712920aa835dabe25c8
In order to run the server you should execute this command:
```bash
uberproxy server [-c config.yml]
uberproxy server [-c|--config config.yml] [-w|--worker workers_count] [-p|--http-port 80] [-s|--https-port 443]
```

You can override config variables:

- `UBERPROXY_CONFIG` overrides config filename
- `UBERPROXY_DYNAMIC` overrides dynamic config filename - `dynamic` section of `config.yml`
- `UBERPROXY_SSL_CERTS` overrides ssl path - `ssl.certs` section of `config.yml`
- `UBERPROXY_SECRET` overrides secret - `secret` section of `config.yml`
- `UBERPROXY_CLUSTER` overrides workers count - `cluster` section of `config.yml`

If you run `uberproxy` with empty config it will be initialized with default values just like you prepared next config:

```
ssl:
port: 443
certs: /config/ssl
dynamic: /config/dynamic.yml
cluster: 4
port: 80
```

Config's secret key is mandatory and haven't default value.
So if you want to run `uberproxy` with empty config - you need to pass `secret` via environment variable:

```
UBERPROXY_SECRET=123 uberproxy -c empty.yml
```

Otherwise error will be raised.

### Parts

1. **ssl**
Expand Down
60 changes: 48 additions & 12 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,13 @@ Store.prototype.get = function(key) {
return this._data[key] || null;
};

var config = process.cwd() + "/config.yml";
var config = process.env.UBERPROXY_CONFIG || process.cwd() + "/config.yml";
if (process.configPath) {
config = process.configPath;
}

// Make sure config it exists
var Config = FUtil.read(config);
['ssl', 'dynamic', 'cluster', 'port', 'secret'].forEach(function(v) {
if (!Config.hasOwnProperty(v)) {
throw new Error("Missing property " + v + " in " + config + " file");
}
});

['port', 'certs'].forEach(function(v) {
if (!Config.ssl.hasOwnProperty(v)) {
throw new Error("Missing property ssl['" + v + "'] in " + config + " file");
}
});

Config.parseArgv = function(argv) {
var map = {
Expand All @@ -84,6 +73,53 @@ Config.parseArgv = function(argv) {
}
};

Config.applyDefaults = function() {
var map = {
'/config/dynamic.yml': ['dynamic'],
'/config/ssl': ['ssl', 'certs'],
'443': ['ssl', 'port'],
'80': ['port'],
'4': ['cluster'],
};

for (var i in map) {
var l = Config;
map[i].slice(0,-1).forEach(function(v) {
l = l[v];
});
var c = map[i].pop()
if (null == l[ c ]) {
l[ c ] = i;
}
}
};

Config.applyEnv = function() {
var map = {
'UBERPROXY_DYNAMIC': ['dynamic'],
'UBERPROXY_SSL_CERTS': ['ssl', 'certs'],
'UBERPROXY_SECRET': ['secret'],
'UBERPROXY_CLUSTER': ['cluster'],
};
for (var i in map) {
if (process.env[i] === undefined || process.env[i] === null) continue;
var l = Config;
map[i].slice(0,-1).forEach(function(v) {
l = l[v];
});
l[ map[i].pop() ] = process.env[i];
}
};

Config.applyDefaults();
Config.applyEnv();

['secret'].forEach(function(v) {
if (!Config.hasOwnProperty(v)) {
throw new Error("Missing property " + v + " in " + config + " file");
}
});

var mode = parseInt('0600',8)
Mkdir.sync(Path.dirname(Config.ssl.certs), {mode: mode});
Mkdir.sync(Path.dirname(Config.dynamic), {mode: mode});
Expand Down

0 comments on commit 5c3e633

Please sign in to comment.