Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from flask_cors import CORS
|
3 |
+
from datetime import datetime, timedelta
|
4 |
+
import uuid
|
5 |
+
import threading
|
6 |
+
import time
|
7 |
+
|
8 |
+
app = Flask(__name__)
|
9 |
+
CORS(app)
|
10 |
+
|
11 |
+
storage = {}
|
12 |
+
|
13 |
+
@app.route('/api/store', methods=['POST'])
|
14 |
+
def store():
|
15 |
+
req = request.get_json()
|
16 |
+
data = req.get('data')
|
17 |
+
ttl = int(req.get('ttl', 3600)) # default 1 hour
|
18 |
+
view_once = bool(req.get('view_once', False))
|
19 |
+
|
20 |
+
secret_id = str(uuid.uuid4())[:8]
|
21 |
+
expire_at = datetime.utcnow() + timedelta(seconds=ttl)
|
22 |
+
|
23 |
+
storage[secret_id] = {
|
24 |
+
'data': data,
|
25 |
+
'expire_at': expire_at,
|
26 |
+
'view_once': view_once,
|
27 |
+
}
|
28 |
+
|
29 |
+
return jsonify({'id': secret_id})
|
30 |
+
|
31 |
+
@app.route('/api/fetch/<secret_id>', methods=['GET'])
|
32 |
+
def fetch(secret_id):
|
33 |
+
secret = storage.get(secret_id)
|
34 |
+
if not secret:
|
35 |
+
return jsonify({'error': 'Not found'}), 404
|
36 |
+
|
37 |
+
if datetime.utcnow() > secret['expire_at']:
|
38 |
+
del storage[secret_id]
|
39 |
+
return jsonify({'error': 'Expired'}), 410
|
40 |
+
|
41 |
+
data = secret['data']
|
42 |
+
if secret['view_once']:
|
43 |
+
del storage[secret_id]
|
44 |
+
return jsonify({'data': data})
|
45 |
+
|
46 |
+
def cleanup():
|
47 |
+
while True:
|
48 |
+
now = datetime.utcnow()
|
49 |
+
expired_keys = [k for k, v in storage.items() if now > v['expire_at']]
|
50 |
+
for k in expired_keys:
|
51 |
+
del storage[k]
|
52 |
+
time.sleep(60)
|
53 |
+
|
54 |
+
threading.Thread(target=cleanup, daemon=True).start()
|
55 |
+
|
56 |
+
if __name__ == "__main__":
|
57 |
+
app.run(host="0.0.0.0", port=7860)
|