SalexAI commited on
Commit
e6f981b
·
verified ·
1 Parent(s): 247c7db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -28
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 log_error(req, error_msg):
 
 
 
 
 
 
 
13
  try:
14
- with open(LOG_FILE, "a", encoding="utf-8") as f:
15
- f.write("\n--- Error Log ---\n")
16
- f.write(f"Time: {datetime.utcnow().isoformat()} UTC\n")
17
- f.write(f"Remote Addr: {request.remote_addr}\n")
18
- f.write(f"Path: {request.path}\n")
19
- f.write(f"Headers: {dict(req.headers)}\n")
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(request):
30
  try:
31
- return request.get_json(force=True)
32
  except:
33
  pass
34
 
35
- if "title" in request.args and "content" in request.args:
36
  return {
37
- "title": request.args.get("title"),
38
- "content": request.args.get("content"),
39
- "tag": request.args.get("tag", "untagged")
40
  }
41
 
42
- if "X-Title" in request.headers and "X-Content" in request.headers:
43
  return {
44
- "title": request.headers.get("X-Title"),
45
- "content": request.headers.get("X-Content"),
46
- "tag": request.headers.get("X-Tag", "untagged")
47
  }
48
 
49
  return None
@@ -76,7 +73,7 @@ def create_markdown():
76
  }), 201
77
 
78
  except Exception as e:
79
- log_error(request, str(e))
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__":