-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
71 lines (61 loc) · 2.12 KB
/
app.py
File metadata and controls
71 lines (61 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS
import os
app = Flask(__name__)
CORS(app)
# Route to serve your HTML
@app.route("/")
def index():
return render_template("review.html") # review.html must be in a 'templates' folder
# Analyze route
@app.route("/analyze", methods=["POST"])
def analyze():
data = request.get_json()
code = data.get("code", "")
return jsonify(analyze_code(code))
def analyze_code(code):
issues = []
# Syntax check
try:
compile(code, "<string>", "exec")
except SyntaxError as e:
issues.append({
"type": "fatal-error",
"line": e.lineno or 0,
"message": f"Syntax error: {e.msg}",
"tip": "Check indentation, missing colons, or unmatched parentheses."
})
return {"issues": issues, "score": 0, "errors": 1, "warnings": 0}
# Security checks
security_patterns = [
("os.system", "Avoid using os.system() with untrusted input."),
("eval(", "Use of eval() is unsafe; prefer safer alternatives."),
("exec(", "Use of exec() can lead to code injection."),
]
for pattern, tip in security_patterns:
if pattern in code:
issues.append({
"type": "security",
"line": 0,
"message": f"Use of insecure function detected: {pattern}",
"tip": tip
})
# Style warning
if "print(" in code:
issues.append({
"type": "warning",
"line": 0,
"message": "Print statement found — avoid print() in production code.",
"tip": "Use logging module for maintainability."
})
return {
"issues": issues,
"score": max(0, 100 - len(issues)*10),
"errors": sum(1 for i in issues if i["type"] in ["error","fatal-error"]),
"warnings": sum(1 for i in issues if i["type"]=="warning")
}
if __name__ == "__main__":
app.run(debug=True)
import os
port = int(os.environ.get("PORT", 5000)) # Render provides PORT automatically
app.run(host="0.0.0.0", port=port, debug=True)