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
This diff is collapsed.
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