Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
prajjwalyd committed Jul 13, 2024
1 parent 2bf6e51 commit 821b1a1
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 57 deletions.
4 changes: 3 additions & 1 deletion analytics/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
from datetime import datetime
import logging

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = Flask(__name__)
metrics = PrometheusMetrics(app, group_by='endpoint')
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://user:password@my-postgresql:5432/analytics-db'

# if using docker-compose then,
# app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://user:password@analytics-db:5432/analytics-db"

try:
db = SQLAlchemy(app)
logger.info("Successfully connected to the database")
Expand Down
11 changes: 2 additions & 9 deletions api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,27 @@
app = Flask(__name__)
metrics = PrometheusMetrics(app, group_by='endpoint')

# Set up logging
logging.basicConfig(level=logging.DEBUG)

try:
client = MongoClient('mongodb://root:example@my-mongo-mongodb:27017/url_db')
# if using docker-compose then,
# client = MongoClient('mongodb://root:example@mongo-db:27017/url_db')
db = client.url_db
logging.info("Connected to MongoDB successfully.")
except errors.ServerSelectionTimeoutError as err:
logging.error("Failed to connect to MongoDB: %s", err)



# client = MongoClient(host='testmongodb', port=27017, username='root', password='example', authSource="admin")
# db = client.url_db

@app.route('/create', methods=['POST'])
def create_entry():
data = request.get_json()
long_url = data['long_url']
custom_url = data.get('custom_url')
generate_qr = data.get('generate_qr', False)

# Check if custom URL already exists
if custom_url and db.entries.find_one({'short_url': custom_url}):
return jsonify({'error': 'Custom URL already exists'}), 400

# Generate short URL
response = requests.post('http://url-shortener:5001/shorten', json={'long_url': long_url, 'custom_url': custom_url})
short_url = response.json()['short_url']

Expand All @@ -44,7 +38,6 @@ def create_entry():
qr_response = requests.get(f'http://qr-code-generator:5002/generate_qr', params={'url': long_url})
qr_code = qr_response.content

# Save to MongoDB
db.entries.insert_one({'long_url': long_url, 'short_url': short_url, 'qr_code': qr_code})

return jsonify({'short_url': short_url, 'long_url': long_url})
Expand Down
31 changes: 0 additions & 31 deletions api/tests/test_api.py

This file was deleted.

20 changes: 8 additions & 12 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
ports:
- "5432:5432"
volumes:
# - analytics_data:/var/lib/postgresql/data
- analytics_data:/var/lib/postgresql/data
- ./analytics/init.sql:/docker-entrypoint-initdb.d/init.sql
healthcheck:
test: ["CMD-SHELL", "pg_isready -U user -d analytics-db"]
Expand All @@ -25,13 +25,13 @@ services:
MONGO_INITDB_ROOT_PASSWORD: example
ports:
- "27017:27017"
# volumes:
# # - mongo_data:/data/db
# - ./api/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js

volumes:
- mongo_data:/data/db

url-shortener:
build: ./url-shortener
depends_on:
- mongo-db
ports:
- "5001:5001"

Expand All @@ -49,8 +49,6 @@ services:
- analytics-db
ports:
- "5003:5003"
# environment:
# - SQLALCHEMY_DATABASE_URI=postgresql://analytics_user:your_password@analytics-db:5432/analytics-db

api:
build: ./api
Expand All @@ -61,9 +59,7 @@ services:
- mongo-db
ports:
- "5000:5000"
# environment:
# - MONGO_URI=mongodb://root:example@mongo-db:27017/url_db

# volumes:
# analytics_data:
# mongo_data:
volumes:
analytics_data:
mongo_data:
1 change: 0 additions & 1 deletion url-shortener/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
app = Flask(__name__)
metrics = PrometheusMetrics(app, group_by='endpoint')

# Function to generate a random short URL
def generate_short_url():
return ''.join(random.choices(string.ascii_letters + string.digits, k=6))

Expand Down
3 changes: 0 additions & 3 deletions url-shortener/test_url_shortener.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def client():
yield client

def test_shorten_url(client):
# Test shortening a URL without a custom URL
response = client.post('/shorten', data=json.dumps({'long_url': 'https://example.com'}), content_type='application/json')
data = json.loads(response.data)
assert response.status_code == 200
Expand All @@ -18,14 +17,12 @@ def test_shorten_url(client):
assert len(data['short_url']) == 6

def test_shorten_url_with_custom(client):
# Test shortening a URL with a custom URL
response = client.post('/shorten', data=json.dumps({'long_url': 'https://example.com', 'custom_url': 'custom123'}), content_type='application/json')
data = json.loads(response.data)
assert response.status_code == 200
assert data['short_url'] == 'custom123'
assert data['long_url'] == 'https://example.com'

def test_invalid_request(client):
# Test handling of invalid requests
response = client.post('/shorten', data=json.dumps({'invalid_key': 'https://example.com'}), content_type='application/json')
assert response.status_code == 400

0 comments on commit 821b1a1

Please sign in to comment.