Add initial project structure with Docker setup and frontend/backend files
This commit is contained in:
101
frontend/app.py
Executable file
101
frontend/app.py
Executable file
@@ -0,0 +1,101 @@
|
||||
|
||||
import os
|
||||
from flask import Flask, render_template, redirect, url_for, request, session, jsonify
|
||||
import requests
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'super-secret')
|
||||
|
||||
API_URL = os.environ.get('API_URL', 'http://localhost:5000')
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
if 'access_token' in session:
|
||||
return redirect(url_for('dashboard'))
|
||||
return render_template('index.html')
|
||||
|
||||
@app.route('/register', methods=['GET', 'POST'])
|
||||
def register():
|
||||
if request.method == 'POST':
|
||||
data = {
|
||||
'name': request.form['name'],
|
||||
'email': request.form['email'],
|
||||
'age': request.form['age'],
|
||||
'location': request.form['location'],
|
||||
'gender': request.form['gender'],
|
||||
'password': request.form['password']
|
||||
}
|
||||
response = requests.post(f"{API_URL}/users", json=data)
|
||||
if response.status_code == 201:
|
||||
return redirect(url_for('login'))
|
||||
else:
|
||||
return render_template('register.html', error=response.json().get('message', 'Error during registration'))
|
||||
return render_template('register.html')
|
||||
|
||||
@app.route('/login', methods=['GET', 'POST'])
|
||||
def login():
|
||||
if request.method == 'POST':
|
||||
email = request.form['email']
|
||||
password = request.form['password']
|
||||
response = requests.post(f"{API_URL}/auth", json={'email': email, 'password': password})
|
||||
if response.status_code == 200:
|
||||
session['access_token'] = response.json()['access_token']
|
||||
# Save the user ID from the access token or fetch it from the API
|
||||
session['user_id'] = email # Or any unique identifier like user ID
|
||||
return redirect(url_for('dashboard'))
|
||||
else:
|
||||
return render_template('login.html', error=response.json().get('message', 'Invalid credentials'))
|
||||
return render_template('login.html')
|
||||
|
||||
@app.route('/dashboard')
|
||||
def dashboard():
|
||||
if 'access_token' not in session:
|
||||
return redirect(url_for('login'))
|
||||
|
||||
# Fetch the current user's details
|
||||
headers = {'Authorization': f'Bearer {session["access_token"]}'}
|
||||
user_response = requests.get(f"{API_URL}/users/me", headers=headers) # Adjust the API URL as necessary
|
||||
user = user_response.json() if user_response.status_code == 200 else None
|
||||
|
||||
# Handle search filters
|
||||
location = request.args.get('location', '')
|
||||
age = request.args.get('age', '')
|
||||
gender = request.args.get('gender', 'Any')
|
||||
|
||||
filters = {}
|
||||
if location:
|
||||
filters['location'] = location
|
||||
if age:
|
||||
filters['age'] = age
|
||||
if gender and gender != "Any":
|
||||
filters['gender'] = gender
|
||||
|
||||
# Fetch users based on filters
|
||||
response = requests.get(f"{API_URL}/users", params=filters, headers=headers)
|
||||
users = response.json() if response.status_code == 200 else []
|
||||
|
||||
# Fetch messages for the current user
|
||||
messages_response = requests.get(f"{API_URL}/messages", headers=headers)
|
||||
messages = messages_response.json() if messages_response.status_code == 200 else []
|
||||
|
||||
return render_template('dashboard.html', user=user, users=users, messages=messages)
|
||||
|
||||
@app.route('/messages', methods=['POST'])
|
||||
def send_message():
|
||||
if 'access_token' not in session:
|
||||
return redirect(url_for('login'))
|
||||
|
||||
data = {
|
||||
'receiver_id': request.form['receiver_id'],
|
||||
'content': request.form['message_content']
|
||||
}
|
||||
headers = {'Authorization': f'Bearer {session["access_token"]}', 'Content-Type': 'application/json'}
|
||||
response = requests.post(f"{API_URL}/messages", headers=headers, json=data)
|
||||
|
||||
if response.status_code == 201:
|
||||
return redirect(url_for('dashboard'))
|
||||
else:
|
||||
return jsonify({'message': 'Error sending message'}), 500
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=8000, debug=True)
|
||||
Reference in New Issue
Block a user