-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
278 lines (220 loc) · 8.88 KB
/
Copy pathapp.py
File metadata and controls
278 lines (220 loc) · 8.88 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import os
from flask import Flask, request, jsonify
from flask_cors import CORS
import mysql.connector
from mysql.connector import Error
import logging
from datetime import datetime
import hashlib
app = Flask(__name__)
CORS(app)
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Database configuration - UPDATE WITH YOUR GODADDY DETAILS
DB_CONFIG = {
'host': 'localhost', # Usually localhost on GoDaddy
'database': 'your_db_name', # From GoDaddy cPanel
'user': 'your_db_username', # From GoDaddy cPanel
'password': 'your_db_password', # From GoDaddy cPanel
'port': 3306,
'charset': 'utf8mb4',
'collation': 'utf8mb4_unicode_ci'
}
def get_db_connection():
"""Create and return a database connection"""
try:
connection = mysql.connector.connect(**DB_CONFIG)
return connection
except Error as e:
logger.error(f"Database connection error: {e}")
return None
def validate_email(email):
"""Basic email validation"""
import re
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
return re.match(pattern, email) is not None
@app.route('/api/waitlist', methods=['POST', 'OPTIONS'])
def add_to_waitlist():
"""Add prospect to waitlist"""
# Handle CORS preflight
if request.method == 'OPTIONS':
return jsonify({'status': 'ok'}), 200
try:
data = request.get_json()
# Validate input
if not data or not data.get('name') or not data.get('email'):
return jsonify({'error': 'Name and email are required'}), 400
name = data['name'].strip()
email = data['email'].strip().lower()
language = data.get('language', 'es')
# Validate email format
if not validate_email(email):
return jsonify({'error': 'Invalid email address'}), 400
# Connect to database
connection = get_db_connection()
if not connection:
return jsonify({'error': 'Database connection failed'}), 500
cursor = connection.cursor()
# Check if email already exists
check_query = "SELECT id FROM nx8020_waitlist WHERE email = %s"
cursor.execute(check_query, (email,))
if cursor.fetchone():
cursor.close()
connection.close()
return jsonify({'error': 'Email already exists'}), 409
# Insert new prospect
insert_query = """
INSERT INTO nx8020_waitlist (name, email, language, created_at)
VALUES (%s, %s, %s, %s)
"""
cursor.execute(insert_query, (name, email, language, datetime.now()))
connection.commit()
prospect_id = cursor.lastrowid
cursor.close()
connection.close()
# Log successful signup
logger.info(f"New waitlist signup: {name} ({email}) - Language: {language}")
return jsonify({
'success': True,
'message': 'Successfully added to waitlist',
'id': prospect_id
}), 201
except Exception as e:
logger.error(f"Waitlist error: {e}")
return jsonify({'error': 'Internal server error'}), 500
@app.route('/api/demo-interaction', methods=['POST', 'OPTIONS'])
def log_demo_interaction():
"""Log demo interaction for analytics"""
# Handle CORS preflight
if request.method == 'OPTIONS':
return jsonify({'status': 'ok'}), 200
try:
data = request.get_json()
session_id = data.get('session_id', '')
input_text = data.get('input', '')
response_type = data.get('response_type', 'unknown')
language = data.get('language', 'es')
# Connect to database
connection = get_db_connection()
if not connection:
return jsonify({'error': 'Database connection failed'}), 500
cursor = connection.cursor()
# Insert demo interaction
insert_query = """
INSERT INTO nx8020_demo_interactions
(session_id, input_text, response_type, language, created_at)
VALUES (%s, %s, %s, %s, %s)
"""
cursor.execute(insert_query, (session_id, input_text, response_type, language, datetime.now()))
connection.commit()
cursor.close()
connection.close()
return jsonify({'success': True}), 200
except Exception as e:
logger.error(f"Demo interaction error: {e}")
return jsonify({'error': 'Failed to log interaction'}), 500
@app.route('/admin/prospects')
def view_prospects():
"""Admin endpoint to view prospects (password protected)"""
# Simple password protection
auth_header = request.headers.get('Authorization')
if not auth_header or not verify_admin_password(auth_header):
return jsonify({'error': 'Unauthorized'}), 401
try:
connection = get_db_connection()
if not connection:
return jsonify({'error': 'Database connection failed'}), 500
cursor = connection.cursor(dictionary=True)
# Get prospects
cursor.execute("SELECT * FROM nx8020_waitlist ORDER BY created_at DESC")
prospects = cursor.fetchall()
# Get demo interactions count
cursor.execute("SELECT COUNT(*) as total FROM nx8020_demo_interactions")
demo_count = cursor.fetchone()['total']
# Get interactions by type
cursor.execute("""
SELECT response_type, COUNT(*) as count
FROM nx8020_demo_interactions
GROUP BY response_type
""")
interaction_types = cursor.fetchall()
cursor.close()
connection.close()
return jsonify({
'prospects': prospects,
'stats': {
'total_prospects': len(prospects),
'total_interactions': demo_count,
'interaction_breakdown': interaction_types,
'conversion_rate': round((len(prospects) / demo_count * 100), 2) if demo_count > 0 else 0
}
})
except Exception as e:
logger.error(f"Admin prospects error: {e}")
return jsonify({'error': 'Internal server error'}), 500
@app.route('/admin/export')
def export_prospects():
"""Export prospects as CSV"""
# Simple password protection
auth_header = request.headers.get('Authorization')
if not auth_header or not verify_admin_password(auth_header):
return jsonify({'error': 'Unauthorized'}), 401
try:
connection = get_db_connection()
if not connection:
return jsonify({'error': 'Database connection failed'}), 500
cursor = connection.cursor(dictionary=True)
cursor.execute("SELECT name, email, language, created_at FROM nx8020_waitlist ORDER BY created_at DESC")
prospects = cursor.fetchall()
cursor.close()
connection.close()
# Convert to CSV format
import csv
import io
output = io.StringIO()
writer = csv.writer(output)
# Write headers
writer.writerow(['Name', 'Email', 'Language', 'Date'])
# Write data
for prospect in prospects:
writer.writerow([
prospect['name'],
prospect['email'],
prospect['language'],
prospect['created_at'].strftime('%Y-%m-%d %H:%M:%S')
])
csv_content = output.getvalue()
output.close()
return csv_content, 200, {
'Content-Type': 'text/csv',
'Content-Disposition': f'attachment; filename=nx8020-prospects-{datetime.now().strftime("%Y%m%d")}.csv'
}
except Exception as e:
logger.error(f"Export error: {e}")
return jsonify({'error': 'Export failed'}), 500
def verify_admin_password(auth_header):
"""Verify admin password from Authorization header"""
try:
# Expected format: "Bearer your_password"
if not auth_header.startswith('Bearer '):
return False
password = auth_header.split(' ')[1]
expected_password = "your_secure_admin_password" # CHANGE THIS!
return password == expected_password
except:
return False
@app.route('/health')
def health_check():
"""Health check endpoint"""
try:
connection = get_db_connection()
if connection:
connection.close()
return jsonify({'status': 'healthy', 'database': 'connected'}), 200
else:
return jsonify({'status': 'unhealthy', 'database': 'disconnected'}), 500
except Exception as e:
return jsonify({'status': 'unhealthy', 'error': str(e)}), 500
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port=5000)