Permalink
Browse files

Update the apps to support external services via ENV

Adds ENV var processing for 3 of the core applications (worker, result, vote) to connect to external services instead of hard coded connection strings which assume docker networking.
  • Loading branch information...
chuckbutler committed Jul 29, 2016
1 parent 036c119 commit 68727c9c2044d74e46e22af424f67bb090f8dfad
Showing with 38 additions and 6 deletions.
  1. +11 −1 result/server.js
  2. +2 −1 vote/app.py
  3. +25 −4 worker/src/Worker/Program.cs
View
@@ -11,6 +11,16 @@ var express = require('express'),
io.set('transports', ['polling']);
var port = process.env.PORT || 4000;
+var pg_host = process.env.PG_HOST || 'db';
+var pg_user = process.env.PG_USER || 'postgres';
+var pg_pass = process.env.PG_PASS || '';
+var pg_db = process.env.PG_DB || 'postgres';
+
+if(pg_host != null) {
+ pg_connection_string = 'postgres://' + pg_user + ':' + pg_pass + '@' + pg_host + '/' + pg_db;
+} else {
+ pg_connection_string = 'postgres://postgres@db/postgres';
+}
io.sockets.on('connection', function (socket) {
@@ -24,7 +34,7 @@ io.sockets.on('connection', function (socket) {
async.retry(
{times: 1000, interval: 1000},
function(callback) {
- pg.connect('postgres://postgres@db/postgres', function(err, client, done) {
+ pg.connect(pg_connection_string, function(err, client, done) {
if (err) {
console.error("Waiting for db");
}
View
@@ -7,13 +7,14 @@
option_a = os.getenv('OPTION_A', "Cats")
option_b = os.getenv('OPTION_B', "Dogs")
+redis_host = os.getenv('REDIS_HOST', "redis")
hostname = socket.gethostname()
app = Flask(__name__)
def get_redis():
if not hasattr(g, 'redis'):
- g.redis = Redis(host="redis", db=0, socket_timeout=5)
+ g.redis = Redis(host=redis_host, db=0, socket_timeout=5)
return g.redis
@app.route("/", methods=['POST','GET'])
@@ -16,8 +16,29 @@ public static int Main(string[] args)
{
try
{
- var pgsql = OpenDbConnection("Server=db;Username=postgres;");
- var redis = OpenRedisConnection("redis").GetDatabase();
+ var redis_host = Environment.GetEnvironmentVariable("REDIS_HOST");
+
+ if(redis_host == null) {
+ redis_host = "redis";
+ }
+
+ var postgres_host = Environment.GetEnvironmentVariable("PG_HOST");
+ var postgres_user = Environment.GetEnvironmentVariable("PG_USER");
+ var postgres_pass = Environment.GetEnvironmentVariable("PG_PASS");
+ var postgres_db = Environment.GetEnvironmentVariable("PG_DB");
+
+ string pg_connection = "";
+
+ if(postgres_host == null) {
+ pg_connection="Server=db;Username=postgres;";
+ } else {
+ pg_connection="Host=" + postgres_host;
+ pg_connection = pg_connection + ";User Id=" + postgres_user;
+ pg_connection = pg_connection + ";Password=" + postgres_pass;
+ pg_connection = pg_connection + ";Database=" + postgres_db + ";";
+ }
+ var pgsql = OpenDbConnection(pg_connection);
+ var redis = OpenRedisConnection(redis_host).GetDatabase();
var definition = new { vote = "", voter_id = "" };
while (true)
@@ -66,7 +87,7 @@ private static NpgsqlConnection OpenDbConnection(string connectionString)
var command = connection.CreateCommand();
command.CommandText = @"CREATE TABLE IF NOT EXISTS votes (
- id VARCHAR(255) NOT NULL UNIQUE,
+ id VARCHAR(255) NOT NULL UNIQUE,
vote VARCHAR(255) NOT NULL
)";
command.ExecuteNonQuery();
@@ -123,4 +144,4 @@ private static void UpdateVote(NpgsqlConnection connection, string voterId, stri
}
}
}
-}
+}

0 comments on commit 68727c9

Please sign in to comment.