Browse your Git repositories
from flask import Flask, render_template, request, redirect, url_for, jsonify
import json
import os
from datetime import datetime
app = Flask(__name__)
# Store notes in a JSON file
NOTES_FILE = os.environ.get('NOTES_FILE', '/data/notes.json')
def load_notes():
"""Load notes from JSON file"""
if os.path.exists(NOTES_FILE):
try:
with open(NOTES_FILE, 'r') as f:
return json.load(f)
except:
return []
return []
def save_notes(notes):
"""Save notes to JSON file"""
os.makedirs(os.path.dirname(NOTES_FILE), exist_ok=True)
with open(NOTES_FILE, 'w') as f:
json.dump(notes, f, indent=2)
@app.route('/')
def index():
"""Display all notes"""
notes = load_notes()
return render_template('index.html', notes=notes)
@app.route('/add', methods=['POST'])
def add_note():
"""Add a new note"""
title = request.form.get('title', '').strip()
content = request.form.get('content', '').strip()
if title and content:
notes = load_notes()
note = {
'id': len(notes) + 1,
'title': title,
'content': content,
'created_at': datetime.now().isoformat()
}
notes.append(note)
save_notes(notes)
return redirect(url_for('index'))
@app.route('/delete/<int:note_id>', methods=['POST'])
def delete_note(note_id):
"""Delete a note by ID"""
notes = load_notes()
notes = [n for n in notes if n['id'] != note_id]
save_notes(notes)
return redirect(url_for('index'))
@app.route('/api/notes', methods=['GET'])
def api_get_notes():
"""API endpoint to get all notes"""
notes = load_notes()
return jsonify(notes)
@app.route('/api/notes', methods=['POST'])
def api_add_note():
"""API endpoint to add a note"""
data = request.get_json()
title = data.get('title', '').strip()
content = data.get('content', '').strip()
if not title or not content:
return jsonify({'error': 'Title and content are required'}), 400
notes = load_notes()
note = {
'id': len(notes) + 1,
'title': title,
'content': content,
'created_at': datetime.now().isoformat()
}
notes.append(note)
save_notes(notes)
return jsonify(note), 201
@app.route('/health')
def health():
"""Health check endpoint"""
return jsonify({'status': 'healthy'}), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=False)