from flask import Flask, render_template, request, jsonify from flask_cors import CORS import os import datetime app = Flask(__name__) CORS(app) # Enable CORS for all routes # Ensure templates directory exists os.makedirs('templates', exist_ok=True) @app.route('/') def index(): """Render the home page.""" current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") return render_template('index.html', current_time=current_time) @app.route('/api/time') def get_time(): """API endpoint that returns the current time.""" current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") return jsonify({"time": current_time}) @app.route('/api/echo', methods=['POST']) def echo(): """API endpoint that echoes back the JSON data sent to it.""" data = request.json return jsonify({"status": "success", "echo": data}) if __name__ == '__main__': # Create templates directory if it doesn't exist if not os.path.exists('templates'): os.makedirs('templates') # Create a simple HTML template if it doesn't exist if not os.path.exists('templates/index.html'): with open('templates/index.html', 'w') as f: f.write('''
The current server time is: {{ current_time }}
Results will appear here...