Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,46 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import os
|
| 3 |
from datetime import datetime
|
| 4 |
-
import json
|
| 5 |
import traceback
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
BASE_DIR = "markdown_files"
|
| 9 |
-
LOG_FILE = "error.log"
|
| 10 |
os.makedirs(BASE_DIR, exist_ok=True)
|
| 11 |
|
| 12 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
f.write(f"Args: {dict(req.args)}\n")
|
| 21 |
-
f.write(f"Form: {dict(req.form)}\n")
|
| 22 |
-
f.write(f"Raw Data: {req.data.decode('utf-8', errors='ignore')}\n")
|
| 23 |
-
f.write(f"Error: {error_msg}\n")
|
| 24 |
-
f.write(f"Traceback:\n{traceback.format_exc()}\n")
|
| 25 |
-
f.write("------------------\n")
|
| 26 |
-
except Exception as e:
|
| 27 |
-
print("Logging failed:", e)
|
| 28 |
|
| 29 |
-
def extract_data(
|
| 30 |
try:
|
| 31 |
-
return
|
| 32 |
except:
|
| 33 |
pass
|
| 34 |
|
| 35 |
-
if "title" in
|
| 36 |
return {
|
| 37 |
-
"title":
|
| 38 |
-
"content":
|
| 39 |
-
"tag":
|
| 40 |
}
|
| 41 |
|
| 42 |
-
if "X-Title" in
|
| 43 |
return {
|
| 44 |
-
"title":
|
| 45 |
-
"content":
|
| 46 |
-
"tag":
|
| 47 |
}
|
| 48 |
|
| 49 |
return None
|
|
@@ -76,7 +73,7 @@ def create_markdown():
|
|
| 76 |
}), 201
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
-
|
| 80 |
return jsonify({"error": str(e)}), 400
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import os
|
| 3 |
from datetime import datetime
|
|
|
|
| 4 |
import traceback
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
BASE_DIR = "markdown_files"
|
|
|
|
| 8 |
os.makedirs(BASE_DIR, exist_ok=True)
|
| 9 |
|
| 10 |
+
def log_error_console(req, error_msg):
|
| 11 |
+
print("\n--- Failed Request ---")
|
| 12 |
+
print(f"Time: {datetime.utcnow().isoformat()} UTC")
|
| 13 |
+
print(f"Remote Addr: {req.remote_addr}")
|
| 14 |
+
print(f"Path: {req.path}")
|
| 15 |
+
print(f"Headers: {dict(req.headers)}")
|
| 16 |
+
print(f"Args: {dict(req.args)}")
|
| 17 |
+
print(f"Form: {dict(req.form)}")
|
| 18 |
try:
|
| 19 |
+
print(f"Raw Data: {req.data.decode('utf-8', errors='ignore')}")
|
| 20 |
+
except:
|
| 21 |
+
print("Raw Data: <unreadable>")
|
| 22 |
+
print(f"Error: {error_msg}")
|
| 23 |
+
print(f"Traceback:\n{traceback.format_exc()}")
|
| 24 |
+
print("----------------------\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
def extract_data(req):
|
| 27 |
try:
|
| 28 |
+
return req.get_json(force=True)
|
| 29 |
except:
|
| 30 |
pass
|
| 31 |
|
| 32 |
+
if "title" in req.args and "content" in req.args:
|
| 33 |
return {
|
| 34 |
+
"title": req.args.get("title"),
|
| 35 |
+
"content": req.args.get("content"),
|
| 36 |
+
"tag": req.args.get("tag", "untagged")
|
| 37 |
}
|
| 38 |
|
| 39 |
+
if "X-Title" in req.headers and "X-Content" in req.headers:
|
| 40 |
return {
|
| 41 |
+
"title": req.headers.get("X-Title"),
|
| 42 |
+
"content": req.headers.get("X-Content"),
|
| 43 |
+
"tag": req.headers.get("X-Tag", "untagged")
|
| 44 |
}
|
| 45 |
|
| 46 |
return None
|
|
|
|
| 73 |
}), 201
|
| 74 |
|
| 75 |
except Exception as e:
|
| 76 |
+
log_error_console(request, str(e))
|
| 77 |
return jsonify({"error": str(e)}), 400
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|