from flask import Flask, request, jsonify
import json
import os

app = Flask(__name__)
DATA_FILE = "/data/tasks.json"

def read_tasks():
    if os.path.exists(DATA_FILE):
        with open(DATA_FILE, "r") as f:
            return json.load(f)
    return []

def write_tasks(tasks):
    with open(DATA_FILE, "w") as f:
        json.dump(tasks, f)

@app.route("/tasks", methods=["GET"])
def get_tasks():
    return jsonify(read_tasks())

@app.route("/tasks", methods=["POST"])
def add_task():
    tasks = read_tasks()
    new_task = request.json
    tasks.append(new_task)
    write_tasks(tasks)
    return jsonify(new_task), 201

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

