Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add REDIS_HOST and POSTGRES_HOST Environment Variables to Example Voting App Configuration (Fixes #370) #386

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ bin/
obj/
.vs/
node_modules/
.aider*
2 changes: 1 addition & 1 deletion result/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ io.on('connection', function (socket) {
});

var pool = new Pool({
connectionString: 'postgres://postgres:postgres@db/postgres'
connectionString: `postgres://postgres:postgres@${process.env.POSTGRES_HOST || 'db'}/postgres`
});

async.retry(
Expand Down
31 changes: 31 additions & 0 deletions result/test_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const assert = require('assert');

describe('Database Configuration', () => {
let originalEnv;

beforeEach(() => {
originalEnv = process.env.POSTGRES_HOST;
});

afterEach(() => {
process.env.POSTGRES_HOST = originalEnv;
});

it('should use POSTGRES_HOST environment variable when set', () => {
process.env.POSTGRES_HOST = 'custom-host';
const { Pool } = require('pg');
const pool = new Pool({
connectionString: `postgres://postgres:postgres@${process.env.POSTGRES_HOST || 'db'}/postgres`
});
assert.ok(pool.options.connectionString.includes('custom-host'));
});

it('should default to "db" when POSTGRES_HOST is not set', () => {
delete process.env.POSTGRES_HOST;
const { Pool } = require('pg');
const pool = new Pool({
connectionString: `postgres://postgres:postgres@${process.env.POSTGRES_HOST || 'db'}/postgres`
});
assert.ok(pool.options.connectionString.includes('@db/'));
});
});
2 changes: 1 addition & 1 deletion vote/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

def get_redis():
if not hasattr(g, 'redis'):
g.redis = Redis(host="redis", db=0, socket_timeout=5)
g.redis = Redis(host=os.getenv('REDIS_HOST', 'redis'), db=0, socket_timeout=5)
return g.redis

@app.route("/", methods=['POST','GET'])
Expand Down
25 changes: 25 additions & 0 deletions vote/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
import unittest
from app import get_redis
from flask import g

class TestConfig(unittest.TestCase):
def test_redis_host_config(self):
# Clear any existing redis connection
if hasattr(g, 'redis'):
delattr(g, 'redis')

# Test default value
os.environ.pop('REDIS_HOST', None)
redis_conn = get_redis()
self.assertEqual(redis_conn.connection_pool.connection_kwargs['host'], 'redis')

# Test custom value
os.environ['REDIS_HOST'] = 'custom-redis'
if hasattr(g, 'redis'):
delattr(g, 'redis')
redis_conn = get_redis()
self.assertEqual(redis_conn.connection_pool.connection_kwargs['host'], 'custom-redis')

if __name__ == '__main__':
unittest.main()