Skip to content

Commit

Permalink
Fix gateway config
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagobustamante committed Oct 30, 2017
1 parent 145f6b9 commit c0d6487
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 159 deletions.
52 changes: 33 additions & 19 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class Configuration extends EventEmitter {
}
}
if (!config) {
config = this.loadDefaultConfig();
config = this.loadDefaultServerConfig();
}
let serverConfig: ServerConfig = validateServerConfig(config);
serverConfig = _.defaults(serverConfig, {
Expand All @@ -116,20 +116,22 @@ export class Configuration extends EventEmitter {
this.config = serverConfig;
this.loadContainerConfigurations();
return new Promise<void>((resolve, reject) => {
if (this.config.gateway && this.config.gateway.protocol) {
if (this.config.gateway.protocol.https) {
if (_.startsWith(this.config.gateway.protocol.https.privateKey, '.')) {
this.config.gateway.protocol.https.privateKey =
path.join(this.config.rootPath, this.config.gateway.protocol.https.privateKey);
}
if (_.startsWith(this.config.gateway.protocol.https.certificate, '.')) {
this.config.gateway.protocol.https.certificate =
path.join(this.config.rootPath, this.config.gateway.protocol.https.certificate);
}
}
}
this.loadDatabaseConfig()
.then(resolve)
.then(() => {
if (this.config.gateway && this.config.gateway.protocol) {
if (this.config.gateway.protocol.https) {
if (_.startsWith(this.config.gateway.protocol.https.privateKey, '.')) {
this.config.gateway.protocol.https.privateKey =
path.join(this.config.rootPath, this.config.gateway.protocol.https.privateKey);
}
if (_.startsWith(this.config.gateway.protocol.https.certificate, '.')) {
this.config.gateway.protocol.https.certificate =
path.join(this.config.rootPath, this.config.gateway.protocol.https.certificate);
}
}
}
resolve();
})
.catch(reject);
});
}
Expand Down Expand Up @@ -194,8 +196,14 @@ export class Configuration extends EventEmitter {
} catch (e) {
return reject(e);
}
} else if (!this.config.gateway) {
this.config.gateway = this.loadDefaultGatewayConfig();
gatewayService.save(this.config.gateway)
.then(resolve)
.catch(reject);
} else {
resolve();
}
resolve();
})
.catch(reject);
});
Expand All @@ -221,12 +229,18 @@ export class Configuration extends EventEmitter {
return fileName;
}

private loadDefaultConfig() {
private loadDefaultServerConfig(): ServerConfig {
const filePath = path.join(process.cwd(), 'tree-gateway.yaml');
console.info(`No gateway configuration file was found. Using default configuration and saving it on '${filePath}'`);
const config = YAML.load(require.resolve('./tree-gateway-default.yaml'));
config.gateway.admin.userService.jwtSecret = uuid();
console.info(`No server configuration file was found. Using default configuration and saving it on '${filePath}'`);
const config: ServerConfig = YAML.load(require.resolve('./tree-gateway-server-default.yaml'));
fs.writeFileSync(filePath, YAML.stringify(config, 15));
return config;
}

private loadDefaultGatewayConfig(): GatewayConfig {
const gateway: GatewayConfig = YAML.load(require.resolve('./tree-gateway-default.yaml'));
console.info(`No configuration for gateway was found. Using default configuration and saving it on database.`);
gateway.admin.userService.jwtSecret = uuid();
return gateway;
}
}
84 changes: 44 additions & 40 deletions src/tree-gateway-default.yaml
Original file line number Diff line number Diff line change
@@ -1,41 +1,11 @@
rootPath: .
database:
redis:
standalone:
host: localhost
port: 6379
gateway:
underProxy: false
underProxy: false
protocol:
http:
listenPort: 8000
admin:
protocol:
http:
listenPort: 8000
admin:
protocol:
http:
listenPort: 8001
accessLogger:
msg: 'HTTP {{req.method}} - {{res.statusCode}} - {{req.url}} ({{res.responseTime}}ms)'
console:
timestamp: true
colorize: true
file:
timestamp: true
json: false
prettyPrint: true
outputDir: ./logs
userService:
jwtSecret: secret
apiDocs:
path: api-docs
logger:
level: info
console:
colorize: true
file:
timestamp: true
outputDir: ./logs
json: false
prettyPrint: true
listenPort: 8001
accessLogger:
msg: 'HTTP {{req.method}} - {{res.statusCode}} - {{req.url}} ({{res.responseTime}}ms)'
console:
Expand All @@ -46,8 +16,42 @@ gateway:
json: false
prettyPrint: true
outputDir: ./logs
statsConfig:
userService:
jwtSecret: secret
apiDocs:
path: api-docs
logger:
level: info
console:
colorize: true
file:
timestamp: true
outputDir: ./logs
json: false
prettyPrint: true
accessLogger:
msg: 'HTTP {{req.method}} - {{res.statusCode}} - {{req.url}} ({{res.responseTime}}ms)'
console:
timestamp: true
colorize: true
file:
timestamp: true
json: false
prettyPrint: true
outputDir: ./logs
statsConfig:
granularity:
duration: '1 hour'
ttl: '1 day'
monitor:
- name: cpu
statsConfig:
granularity:
duration: 1 hour
ttl: 1 day
- name: mem
statsConfig:
granularity:
duration: '1 hour'
ttl: '1 day'
healthcheck: /healthcheck
duration: 1 hour
ttl: 1 day
healthcheck: /healthcheck
6 changes: 6 additions & 0 deletions src/tree-gateway-server-default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rootPath: .
database:
redis:
standalone:
host: localhost
port: 6379
7 changes: 3 additions & 4 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
'use strict';

import * as os from 'os';
import { GatewayConfig } from '../config/gateway';

export function getSwaggerHost(gateway: GatewayConfig, isTest?: boolean) {
export function getSwaggerHost(gateway: GatewayConfig) {
let host;
if (gateway.admin.apiDocs.host) {
host = gateway.admin.apiDocs.host;
} else {
host = (gateway.admin.protocol.https ?
`${isTest ? 'localhost' : os.hostname()}:${gateway.admin.protocol.https.listenPort}` :
`${isTest ? 'localhost' : os.hostname()}:${gateway.admin.protocol.http.listenPort}`);
`localhost:${gateway.admin.protocol.https.listenPort}` :
`localhost:${gateway.admin.protocol.http.listenPort}`);
}
return host;
}
1 change: 0 additions & 1 deletion test/unit/admin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ describe('Gateway Admin Tasks', () => {
describe('gateway SDK', () => {
it('should be able to set configuration for gateway', () => {
const newConfig = fs.readJSONSync(path.join(process.cwd(), 'test/data/tree-gateway.json')).gateway;
newConfig.logger.level = 'debug';
return new Promise((resolve, reject) => {
sdk.gateway.updateConfig(newConfig)
.then(() => {
Expand Down
97 changes: 2 additions & 95 deletions tree-gateway.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,98 +7,5 @@
"port": "{REDIS_PORT_6379_TCP_PORT}"
}
}
},
"gateway": {
"underProxy": false,
"protocol": {
"http": {
"listenPort": 8000
}
},
"admin": {
"protocol": {
"http": {
"listenPort": 8001
}
},
"accessLogger": {
"msg": "HTTP {{req.method}} - {{res.statusCode}} - {{req.url}} ({{res.responseTime}}ms) ",
"console": {
"timestamp": true,
"colorize": true
},
"file": {
"timestamp": true,
"json": false,
"prettyPrint": true,
"outputDir": "./logs"
}
},
"userService": {
"jwtSecret": "secret"
},
"apiDocs": {
"path": "api-docs"
},
"cors" : {
"origin": {
"allow": [{
"value": "*"
}]
}
}
},
"logger": {
"level": "debug",
"console": {
"colorize": true
},
"file": {
"timestamp": true,
"outputDir": "./logs",
"json": false,
"prettyPrint": true
}
},
"accessLogger": {
"msg": "HTTP {{req.method}} - {{res.statusCode}} - {{req.url}} ({{res.responseTime}}ms) ",
"console": {
"timestamp": true,
"colorize": true
},
"file": {
"timestamp": true,
"json": false,
"prettyPrint": true,
"outputDir": "./logs"
}
},
"statsConfig": {
"granularity": {
"duration": "1 hour",
"ttl": "1 day"
}
},
"monitor": [
{
"name": "cpu",
"statsConfig": {
"granularity": {
"duration": "1 hour",
"ttl": "1 day"
}
}
},
{
"name": "mem",
"statsConfig": {
"granularity": {
"duration": "1 hour",
"ttl": "1 day"
}
}
}
],
"healthcheck": "/healthcheck"
}
}
}
}

0 comments on commit c0d6487

Please sign in to comment.