|
20 | 20 |
|
21 | 21 | import requests
|
22 | 22 | from flask import Flask, jsonify, request
|
| 23 | +from flask_wtf.csrf import CSRFProtect |
23 | 24 |
|
24 | 25 | # The MONAI Deploy application to be wrapped.
|
25 | 26 | # This can be changed to any other application in the repository.
|
|
29 | 30 | APP_CLASS_NAME = "AISpleenSegApp"
|
30 | 31 |
|
31 | 32 | # Flask application setup
|
32 |
| -restful_app = Flask(__name__) |
| 33 | +app = Flask(__name__) |
| 34 | +# It is recommended to use a securely generated random string for the secret key, |
| 35 | +# and store it in an environment variable or a secure configuration file. |
| 36 | +app.config["SECRET_KEY"] = os.environ.get("FLASK_SECRET_KEY", "a-secure-default-secret-key-for-dev") |
| 37 | +csrf = CSRFProtect(app) |
| 38 | + |
| 39 | + |
33 | 40 | logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
34 | 41 |
|
35 | 42 | # Global state to track processing status. A lock is used for thread safety.
|
@@ -64,8 +71,16 @@ def app_status_callback(summary: str):
|
64 | 71 | logging.info(f"Sending final status callback to {callback_url}")
|
65 | 72 | # Here you could map the summary to the expected format of the callback.
|
66 | 73 | # For now, we'll just forward the summary.
|
67 |
| - requests.post(callback_url, data=summary, timeout=5) |
| 74 | + response = requests.post(callback_url, data=summary, timeout=5) |
| 75 | + response.raise_for_status() # for bad status codes (4xx or 5xx) |
68 | 76 | logging.info("Sent final status callback.")
|
| 77 | + |
| 78 | + except requests.exceptions.Timeout: |
| 79 | + logging.error("The request timed out.") |
| 80 | + except requests.exceptions.ConnectionError: |
| 81 | + logging.error("A connection error occurred.") |
| 82 | + except requests.exceptions.RequestException as e: |
| 83 | + logging.error(f"An unexpected error occurred: {e}") |
69 | 84 | except Exception as e:
|
70 | 85 | logging.error(f"Failed to send callback to {callback_url}: {e}")
|
71 | 86 |
|
@@ -106,13 +121,14 @@ def app_status_callback(summary: str):
|
106 | 121 | logging.info("Processor is now IDLE.")
|
107 | 122 |
|
108 | 123 |
|
109 |
| -@restful_app.route("/status", methods=["GET"]) |
| 124 | +@app.route("/status", methods=["GET"]) |
110 | 125 | def status():
|
111 | 126 | """Endpoint to check the current processing status."""
|
112 | 127 | return jsonify({"status": get_processing_status()})
|
113 | 128 |
|
114 | 129 |
|
115 |
| -@restful_app.route("/process", methods=["POST"]) |
| 130 | +@app.route("/process", methods=["POST"]) |
| 131 | +@csrf.exempt |
116 | 132 | def process():
|
117 | 133 | """Endpoint to start a new processing job."""
|
118 | 134 | if get_processing_status() == "BUSY":
|
@@ -151,4 +167,4 @@ def process():
|
151 | 167 | args = parser.parse_args()
|
152 | 168 | host = args.host or os.environ.get("FLASK_HOST", "0.0.0.0")
|
153 | 169 | port = args.port or int(os.environ.get("FLASK_PORT", 5000))
|
154 |
| - restful_app.run(host=host, port=port) |
| 170 | + app.run(host=host, port=port) |
0 commit comments