Issue #54: Update config files to use env vars

Signed-off-by: Lucas Fernandes de Oliveira's avatarLucas Fernandes de Oliveira <lfo14@inf.ufpr.br>
parent 99896bbf
Pipeline #13887 passed with stage
in 59 seconds
......@@ -3,6 +3,8 @@
*.pid
*.map
*.swp
*.env
*.swo
/.trash
/pids
/logs
......
image: node:6.2
image: node:6.12.2
services:
- postgres:latest
......@@ -18,6 +18,7 @@ before_script:
run_tests:
script:
- mv config/ci_test.yaml.example config/test.yaml
- mv config/ci_test.env.example config/test.env
- npm test
- npm run lint
tags:
......
BLENDB_DB_USER=runner
BLENDB_DB_NAME=blendb_fixture
BLENDB_DB_PASSWORD=
BLENDB_DB_HOST=postgres
BLENDB_DB_PORT=5432
BLENDB_ST_CREATE=true
BLENDB_ST_INSERT=true
BLENDB_ADAPTER=postgres
PORT=3000
# gitignore ignores files.yaml in this folder
# however a config file for tests in CI is required
# so this example file in fact is the CI test file
connection:
database: 'blendb_fixture'
user: 'runner'
password: ''
host: 'postgres'
port: 5432
max: 10
idleTimeoutMillis: 30000
struct:
create: true
insert: true
schema:
views:
-
alias: "View 0"
data: "test/postgres/fixtures/view0.json"
origin: true
dimensions:
- "dim:0"
- "dim:7"
metrics:
- "met:0"
- "met:1"
- "met:2"
- "met:10"
-
alias: "View 1"
data: "test/postgres/fixtures/view1.json"
origin: true
dimensions:
- "dim:1"
- "dim:8"
metrics:
- "met:3"
- "met:4"
-
alias: "View 2"
data: "test/postgres/fixtures/view2.json"
origin: true
dimensions:
- "dim:2"
metrics:
- "met:5"
- "met:6"
- "met:11"
-
alias: "View 3"
data: "test/postgres/fixtures/view3.json"
origin: true
dimensions:
- "dim:2"
- "dim:3"
- "dim:4"
-
alias: "View 4"
data: "test/postgres/fixtures/view4.json"
origin: true
dimensions:
- "dim:2"
- "dim:7"
- "dim:8"
-
alias: "View 5"
data: "test/postgres/fixtures/view5.json"
origin: true
dimensions:
- "dim:3"
metrics:
- "met:7"
-
alias: "View 6"
data: "test/postgres/fixtures/view6.json"
origin: true
dimensions:
- "dim:4"
metrics:
- "met:8"
-
alias: "View 7"
data: "test/postgres/fixtures/view7.json"
origin: true
dimensions:
- "dim:4"
- "dim:5"
- "dim:6"
-
alias: "View 8"
data: "test/postgres/fixtures/view8.json"
origin: true
dimensions:
- "dim:5"
- "dim:6"
metrics:
- "met:9"
-
alias: "view 9"
data: "test/postgres/fixtures/view9.json"
origin: false
dimensions:
- "dim:2"
metrics:
- "met:5"
- "met:6"
- "met:7"
clauses:
- "dim:2!=1"
metrics:
-
name: "met:0"
dataType: "integer"
aggregation: "sum"
description: "No meaning, just used for test"
-
name: "met:1"
dataType: "integer"
aggregation: "avg"
description: "No meaning, just used for test"
-
name: "met:2"
dataType: "integer"
aggregation: "avg"
description: "No meaning, just used for test"
-
name: "met:3"
dataType: "integer"
aggregation: "sum"
description: "No meaning, just used for test"
-
name: "met:4"
dataType: "integer"
aggregation: "sum"
description: "No meaning, just used for test"
-
name: "met:5"
dataType: "integer"
aggregation: "avg"
description: "No meaning, just used for test"
-
name: "met:6"
dataType: "integer"
aggregation: "count"
description: "No meaning, just used for test"
-
name: "met:7"
dataType: "integer"
aggregation: "count"
description: "No meaning, just used for test"
-
name: "met:8"
dataType: "integer"
aggregation: "sum"
description: "No meaning, just used for test"
-
name: "met:9"
dataType: "integer"
aggregation: "count"
description: "No meaning, just used for test"
-
name: "met:10"
dataType: "integer"
aggregation: "max"
description: "No meaning, just used for test"
-
name: "met:11"
dataType: "integer"
aggregation: "min"
description: "No meaning, just used for test"
dimensions:
-
name: "dim:0"
dataType: "date"
description: "A dimension of Blendb. Has 5 possible values."
-
name: "dim:1"
dataType: "date"
description: "A dimension of Blendb. Has 5 possible values."
-
name: "dim:2"
dataType: "integer"
description: "A dimension of Blendb. Has 5 possible values."
-
name: "dim:3"
dataType: "string"
description: "A dimension of Blendb. Has 5 possible values."
-
name: "dim:4"
dataType: "string"
description: "A dimension of Blendb. Has 5 possible values."
-
name: "dim:5"
dataType: "boolean"
description: "A dimension of Blendb. Has 2 possible values."
-
name: "dim:6"
dataType: "integer"
description: "A dimension of Blendb. Has 5 possible values."
-
name: "dim:7"
dataType: "integer"
description: "A dimension of Blendb. Has 5 possible values."
-
name: "dim:8"
dataType: "integer"
description: "A dimension of Blendb. Has 5 possible values."
-
name: "dim:9"
dataType: "integer"
parent: "dim:0"
relation: "day"
description: "A dimension of Blendb. Has 30 possible values."
-
name: "dim:10"
dataType: "integer"
parent: "dim:0"
relation: "month"
description: "A dimension of Blendb. Has 12 possible values."
-
name: "dim:11"
dataType: "integer"
parent: "dim:0"
relation: "year"
description: "A dimension of Blendb. Has 1 possible value."
-
name: "dim:12"
dataType: "integer"
parent: "dim:0"
relation: "dayofweek"
description: "A dimension of Blendb. Has 7 possible values."
views:
-
alias: "View 0"
data: "test/postgres/fixtures/view0.json"
origin: true
dimensions:
- "dim:0"
- "dim:7"
metrics:
- "met:0"
- "met:1"
- "met:2"
- "met:10"
-
alias: "View 1"
data: "test/postgres/fixtures/view1.json"
origin: true
dimensions:
- "dim:1"
- "dim:8"
metrics:
- "met:3"
- "met:4"
-
alias: "View 2"
data: "test/postgres/fixtures/view2.json"
origin: true
dimensions:
- "dim:2"
metrics:
- "met:5"
- "met:6"
- "met:11"
-
alias: "View 3"
data: "test/postgres/fixtures/view3.json"
origin: true
dimensions:
- "dim:2"
- "dim:3"
- "dim:4"
-
alias: "View 4"
data: "test/postgres/fixtures/view4.json"
origin: true
dimensions:
- "dim:2"
- "dim:7"
- "dim:8"
-
alias: "View 5"
data: "test/postgres/fixtures/view5.json"
origin: true
dimensions:
- "dim:3"
metrics:
- "met:7"
-
alias: "View 6"
data: "test/postgres/fixtures/view6.json"
origin: true
dimensions:
- "dim:4"
metrics:
- "met:8"
-
alias: "View 7"
data: "test/postgres/fixtures/view7.json"
origin: true
dimensions:
- "dim:4"
- "dim:5"
- "dim:6"
-
alias: "View 8"
data: "test/postgres/fixtures/view8.json"
origin: true
dimensions:
- "dim:5"
- "dim:6"
metrics:
- "met:9"
-
alias: "view 9"
data: "test/postgres/fixtures/view9.json"
origin: false
dimensions:
- "dim:2"
metrics:
- "met:5"
- "met:6"
- "met:7"
clauses:
- "dim:2!=1"
metrics:
-
name: "met:0"
dataType: "integer"
aggregation: "sum"
description: "No meaning, just used for test"
-
name: "met:1"
dataType: "integer"
aggregation: "avg"
description: "No meaning, just used for test"
-
name: "met:2"
dataType: "integer"
aggregation: "avg"
description: "No meaning, just used for test"
-
name: "met:3"
dataType: "integer"
aggregation: "sum"
description: "No meaning, just used for test"
-
name: "met:4"
dataType: "integer"
aggregation: "sum"
description: "No meaning, just used for test"
-
name: "met:5"
dataType: "integer"
aggregation: "avg"
description: "No meaning, just used for test"
-
name: "met:6"
dataType: "integer"
aggregation: "count"
description: "No meaning, just used for test"
-
name: "met:7"
dataType: "integer"
aggregation: "count"
description: "No meaning, just used for test"
-
name: "met:8"
dataType: "integer"
aggregation: "sum"
description: "No meaning, just used for test"
-
name: "met:9"
dataType: "integer"
aggregation: "count"
description: "No meaning, just used for test"
-
name: "met:10"
dataType: "integer"
aggregation: "max"
description: "No meaning, just used for test"
-
name: "met:11"
dataType: "integer"
aggregation: "min"
description: "No meaning, just used for test"
dimensions:
-
name: "dim:0"
dataType: "date"
description: "A dimension of Blendb. Has 5 possible values."
-
name: "dim:1"
dataType: "date"
description: "A dimension of Blendb. Has 5 possible values."
-
name: "dim:2"
dataType: "integer"
description: "A dimension of Blendb. Has 5 possible values."
-
name: "dim:3"
dataType: "string"
description: "A dimension of Blendb. Has 5 possible values."
-
name: "dim:4"
dataType: "string"
description: "A dimension of Blendb. Has 5 possible values."
-
name: "dim:5"
dataType: "boolean"
description: "A dimension of Blendb. Has 2 possible values."
-
name: "dim:6"
dataType: "integer"
description: "A dimension of Blendb. Has 5 possible values."
-
name: "dim:7"
dataType: "integer"
description: "A dimension of Blendb. Has 5 possible values."
-
name: "dim:8"
dataType: "integer"
description: "A dimension of Blendb. Has 5 possible values."
-
name: "dim:9"
dataType: "integer"
parent: "dim:0"
relation: "day"
description: "A dimension of Blendb. Has 30 possible values."
-
name: "dim:10"
dataType: "integer"
parent: "dim:0"
relation: "month"
description: "A dimension of Blendb. Has 12 possible values."
-
name: "dim:11"
dataType: "integer"
parent: "dim:0"
relation: "year"
description: "A dimension of Blendb. Has 1 possible value."
-
name: "dim:12"
dataType: "integer"
parent: "dim:0"
relation: "dayofweek"
description: "A dimension of Blendb. Has 7 possible values."
BLENDB_DB_USER=blendb
BLENDB_DB_NAME=blendb-test
BLENDB_DB_PASSWORD=secret
BLENDB_DB_HOST=localhost
BLENDB_DB_PORT=5432
BLENDB_ST_CREATE=false
BLENDB_ST_INSERT=false
BLENDB_ADAPTER=postgres
PORT=3000
......@@ -4,9 +4,9 @@
"description": "BlenDB",
"main": "index.js",
"scripts": {
"start": "node index",
"start": "env $(cat config/config.env) node index",
"lint": "tslint -s node_modules/tslint-stylish -t stylish src/**/*.ts test/**/*.ts",
"test": "ts-node node_modules/istanbul/lib/cli.js cover -x \"**/*.spec.ts\" -e .ts _mocha",
"test": "env $(cat config/test.env) ts-node node_modules/istanbul/lib/cli.js cover -x \"**/*.spec.ts\" -e .ts _mocha",
"show-coverage": "xdg-open coverage/lcov-report/index.html",
"doc-api": "raml2html -i specs/blendb-api-v1.raml -o doc/api-v1-reference.html",
"database": "ts-node database/config.ts database",
......
......@@ -22,9 +22,19 @@ import { Middleware } from "../types";
import { Adapter } from "../../core/adapter";
import { PostgresAdapter } from "../../adapter/postgres";
import { PoolConfig } from "pg";
import { Connection } from "../../util/configParser";
export function PostgresMw(config: PoolConfig): Middleware {
let adapter: Adapter = new PostgresAdapter(config);
export function PostgresMw(config: Connection): Middleware {
let parsedConfig: PoolConfig = {
user: config.user,
database: config.database,
password: config.password,
host: config.host,
port: config.port,
max: 10,
idleTimeoutMillis: 3000
};
let adapter: Adapter = new PostgresAdapter(parsedConfig);
return function postgresMiddleware(req, res, next) {
req.adapter = adapter;
next();
......
......@@ -42,7 +42,14 @@ import { PostgresMw } from "./api/middlewares/adapter";
import { ErrorMw } from "./api/middlewares/error";
app.use(EngineMw(config));
app.use(PostgresMw(config.connection));
if (config.adapter === "postgres") {
app.use(PostgresMw(config.connection));
}
else {
console.error("Invalid adapter. Options available are: postgres");
process.exit(1);
}
// parse the RAML spec and load osprey middleware
ramlParser.loadFile("specs/blendb-api-v1.raml")
......
......@@ -24,7 +24,6 @@ import { View, ViewOptions, LoadView } from "../core/view";
import { RelationType } from "../common/types";
import { Filter } from "../core/filter";
import { Clause } from "../core/clause";
import { PoolConfig } from "pg";
import * as fs from "fs";
import * as yaml from "js-yaml";
......@@ -45,12 +44,6 @@ interface ConfigSchema {
dimensions: DimensionStrOptions[];
}
interface ConfigFile {
connection: PoolConfig;
struct: LoadStruct;
schema: ConfigSchema;
}
interface BuildView {
view: View;
file: string;
......@@ -63,7 +56,8 @@ export interface LoadStruct{
}
export interface ParsedConfig {
connection: PoolConfig;
adapter: string;
connection: Connection;
views: View[];
metrics: Metric[];
dimensions: Dimension[];
......@@ -72,6 +66,14 @@ export interface ParsedConfig {
buildViews: BuildView[];
}
export interface Connection {
user: string;
database: string;
password: string;
host: string;
port: number;
}
interface DimensionMap {
[key: string]: Dimension;
}
......@@ -82,19 +84,33 @@ interface MetricMap {
export class ConfigParser {
public static parse(configPath: string): ParsedConfig {
let config: ConfigFile = yaml.safeLoad(fs.readFileSync(configPath, {
let config: ConfigSchema = yaml.safeLoad(fs.readFileSync(configPath, {
encoding: "utf-8"
}));
let metricsOpts = config.schema.metrics;
let viewsOpts = config.schema.views;
let dimensionsOpts = config.schema.dimensions;
let connection: Connection = {
user: process.env.BLENDB_DB_USER,
database: process.env.BLENDB_DB_NAME,
password: process.env.BLENDB_DB_PASSWORD,
host: process.env.BLENDB_DB_HOST,
port: parseInt(process.env.BLENDB_DB_PORT, 10)
};
let struct: LoadStruct = {
create: process.env.BLENDB_ST_CREATE.toLowerCase() === "true",
insert: process.env.BLENDB_ST_INSERT.toLowerCase() === "true"
};
let metricsOpts = config.metrics;
let viewsOpts = config.views;
let dimensionsOpts = config.dimensions;
let parsed: ParsedConfig = {
connection: config.connection,
adapter: process.env.BLENDB_ADAPTER || "postgres",
connection: connection,
views: [],
metrics: [],
dimensions: [],
struct: config.struct,
struct: struct,
loadViews: [],
buildViews: []
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment