163 lines
4.4 KiB
Python
163 lines
4.4 KiB
Python
from flask import Flask, request, jsonify
|
|
from flask_cors import CORS
|
|
|
|
# Initialize CORS
|
|
app = Flask(__name__)
|
|
CORS(app)
|
|
import json
|
|
import os
|
|
from datetime import datetime
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Define the data directory
|
|
DATA_DIR = 'data'
|
|
os.makedirs(DATA_DIR, exist_ok=True)
|
|
|
|
# Define the data file path
|
|
DATA_FILE = os.path.join(DATA_DIR, 'journeys.json')
|
|
|
|
# Initialize journeys list
|
|
journeys = []
|
|
|
|
def load_journeys():
|
|
"""Load journeys from the data file."""
|
|
global journeys
|
|
try:
|
|
if os.path.exists(DATA_FILE):
|
|
with open(DATA_FILE, 'r') as f:
|
|
journeys = json.load(f)
|
|
else:
|
|
journeys = []
|
|
except Exception as e:
|
|
print(f"Error loading journeys: {e}")
|
|
journeys = []
|
|
|
|
def save_journeys():
|
|
"""Save journeys to the data file."""
|
|
try:
|
|
with open(DATA_FILE, 'w') as f:
|
|
json.dump(journeys, f, indent=2)
|
|
except Exception as e:
|
|
print(f"Error saving journeys: {e}")
|
|
|
|
# Load journeys when the app starts
|
|
load_journeys()
|
|
|
|
@app.route('/api/journeys', methods=['POST'])
|
|
def create_journey():
|
|
"""Create a new journey."""
|
|
data = request.get_json()
|
|
|
|
if not data or 'name' not in data:
|
|
return jsonify({'error': 'Journey name is required'}), 400
|
|
|
|
# Create new journey
|
|
new_journey = {
|
|
'id': len(journeys) + 1,
|
|
'name': data['name'],
|
|
'description': data.get('description', ''),
|
|
'markers': data.get('markers', []),
|
|
'createdAt': datetime.now().isoformat()
|
|
}
|
|
|
|
journeys.append(new_journey)
|
|
save_journeys()
|
|
|
|
return jsonify(new_journey), 201
|
|
|
|
@app.route('/api/journeys', methods=['GET'])
|
|
def get_journeys():
|
|
"""Get all journeys."""
|
|
return jsonify(journeys)
|
|
|
|
@app.route('/api/journeys/<string:journey_id>', methods=['GET'])
|
|
def get_journey(journey_id):
|
|
"""Get a specific journey by ID."""
|
|
journey = next((j for j in journeys if j['id'] == journey_id), None)
|
|
if journey:
|
|
return jsonify(journey)
|
|
return jsonify({'error': 'Journey not found'}), 404
|
|
|
|
@app.route('/api/journeys', methods=['POST'])
|
|
def create_journey():
|
|
"""Create a new journey."""
|
|
data = request.get_json()
|
|
|
|
# Validate required fields
|
|
if not data or 'name' not in data:
|
|
return jsonify({'error': 'Journey name is required'}), 400
|
|
|
|
# Create new journey
|
|
new_journey = {
|
|
'id': data.get('id', str(len(journeys) + 1)),
|
|
'name': data['name'],
|
|
'description': data.get('description', ''),
|
|
'markers': data.get('markers', []),
|
|
'createdAt': datetime.now().isoformat()
|
|
}
|
|
|
|
journeys.append(new_journey)
|
|
save_journeys()
|
|
|
|
return jsonify(new_journey), 201
|
|
|
|
@app.route('/api/journeys/<string:journey_id>', methods=['PUT'])
|
|
def update_journey(journey_id):
|
|
"""Update an existing journey."""
|
|
journey = next((j for j in journeys if j['id'] == journey_id), None)
|
|
if not journey:
|
|
return jsonify({'error': 'Journey not found'}), 404
|
|
|
|
data = request.get_json()
|
|
|
|
# Update journey fields
|
|
if 'name' in data:
|
|
journey['name'] = data['name']
|
|
if 'description' in data:
|
|
journey['description'] = data['description']
|
|
if 'markers' in data:
|
|
journey['markers'] = data['markers']
|
|
|
|
save_journeys()
|
|
return jsonify(journey)
|
|
|
|
@app.route('/api/journeys/<string:journey_id>', methods=['DELETE'])
|
|
def delete_journey(journey_id):
|
|
"""Delete a journey."""
|
|
global journeys
|
|
journey = next((j for j in journeys if j['id'] == journey_id), None)
|
|
if not journey:
|
|
return jsonify({'error': 'Journey not found'}), 404
|
|
|
|
journeys = [j for j in journeys if j['id'] != journey_id]
|
|
save_journeys()
|
|
|
|
return jsonify({'message': 'Journey deleted successfully', 'journey': journey})
|
|
|
|
@app.route('/api/journeys/health', methods=['GET'])
|
|
def health_check():
|
|
"""Health check endpoint."""
|
|
return jsonify({'status': 'healthy', 'timestamp': datetime.now().isoformat()})
|
|
|
|
@app.route('/')
|
|
def index():
|
|
"""Serve the main HTML page."""
|
|
return '''
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Journey Mapper</title>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
</head>
|
|
<body>
|
|
<h1>Journey Mapper</h1>
|
|
<p>Backend is running. Access API at <a href="/api/journeys">/api/journeys</a></p>
|
|
</body>
|
|
</html>
|
|
'''
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True, port=5000)
|