-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
46 lines (38 loc) · 1.6 KB
/
app.py
File metadata and controls
46 lines (38 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from flask import Flask, request, send_from_directory, jsonify
from markupsafe import escape
import os
# Disable Flask's default static file serving
app = Flask(__name__, static_folder=None)
@app.route('/api/userinfo')
def get_user_info():
"""API endpoint to return user information"""
user_agent = escape(request.headers.get('User-Agent', 'Unknown'))
# Get the real IP address (considering proxy headers)
# Note: In Cloud Run, X-Forwarded-For is set by the platform and can be trusted
forwarded_for = request.headers.get('X-Forwarded-For')
if forwarded_for:
ip_address = escape(forwarded_for.split(',')[0].strip())
else:
ip_address = escape(request.remote_addr or 'Unknown')
return jsonify({
'userAgent': str(user_agent),
'ipAddress': str(ip_address)
})
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def serve_react_app(path):
"""Serve the React application and its static assets"""
build_folder = os.path.join(os.path.dirname(__file__), 'frontend', 'build')
# If path is empty or root, serve index.html
if path == '':
return send_from_directory(build_folder, 'index.html')
# send_from_directory has built-in protection against path traversal
# Try to serve the requested file
try:
return send_from_directory(build_folder, path)
except:
# For client-side routing, return index.html for unknown routes
return send_from_directory(build_folder, 'index.html')
if __name__ == '__main__':
port = int(os.environ.get('PORT', 8080))
app.run(host='0.0.0.0', port=port)