Files
aditya 9fe3196827 test: re-run all 7 simulations with improved pipeline
All sims re-run after pipeline fixes (better prompts, real syntax
validation, fenced-block extraction fix). Results: 7/7 PASS,
20 files total, all Python files pass compile() syntax check.

Includes run_all_sims.py runner script and rag-basic action config.
2026-03-13 11:56:24 +00:00

121 lines
3.6 KiB
Python

from flask import Flask, request, jsonify, render_template, redirect
import validators
import string
app = Flask(__name__)
# In-memory storage
url_mapping = {} # counter -> original_url
counter = 1
# Base62 characters for encoding
BASE62_CHARS = string.ascii_lowercase + string.ascii_uppercase + string.digits
def encode_base62(num):
"""Convert integer to base62 string"""
if num == 0:
return BASE62_CHARS[0]
result = []
while num > 0:
result.append(BASE62_CHARS[num % 62])
num //= 62
return ''.join(reversed(result))
def decode_base62(encoded):
"""Convert base62 string back to integer"""
result = 0
for char in encoded:
if char in BASE62_CHARS:
result = result * 62 + BASE62_CHARS.index(char)
else:
return None
return result
@app.route('/')
def index():
"""Serve the landing page"""
return render_template('index.html')
@app.route('/shorten', methods=['POST'])
def shorten_url():
"""Shorten a URL and return the short code"""
global counter
# Get URL from JSON request or form data
if request.is_json:
data = request.get_json()
if not data or 'url' not in data:
return jsonify({'error': 'URL is required'}), 400
url = data['url']
else:
url = request.form.get('url')
if not url:
return jsonify({'error': 'URL is required'}), 400
# Validate URL
if not validators.url(url):
return jsonify({'error': 'Invalid URL format'}), 400
# Check if URL already exists
for existing_counter, existing_url in url_mapping.items():
if existing_url == url:
short_code = encode_base62(existing_counter)
if request.is_json:
return jsonify({
'short_code': short_code,
'short_url': request.host_url + short_code,
'original_url': url
})
else:
return render_template('index.html',
short_code=short_code,
short_url=request.host_url + short_code,
original_url=url)
# Store new URL
url_mapping[counter] = url
short_code = encode_base62(counter)
counter += 1
if request.is_json:
return jsonify({
'short_code': short_code,
'short_url': request.host_url + short_code,
'original_url': url
})
else:
return render_template('index.html',
short_code=short_code,
short_url=request.host_url + short_code,
original_url=url)
@app.route('/<short_code>')
def redirect_url(short_code):
"""Redirect to original URL using short code"""
# Decode the short code
counter_id = decode_base62(short_code)
if counter_id is None:
return jsonify({'error': 'Invalid short code format'}), 404
# Look up the original URL
if counter_id not in url_mapping:
return jsonify({'error': 'Short code not found'}), 404
original_url = url_mapping[counter_id]
return redirect(original_url, code=302)
@app.errorhandler(404)
def not_found(error):
"""Handle 404 errors"""
return jsonify({'error': 'Endpoint not found'}), 404
@app.errorhandler(500)
def internal_error(error):
"""Handle 500 errors"""
return jsonify({'error': 'Internal server error'}), 500
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)