From 41b377721551d237965747450f2f65b684077312 Mon Sep 17 00:00:00 2001 From: Anthony Donlon Date: Thu, 20 Nov 2025 00:17:26 +0800 Subject: [PATCH] examples/flask_server: support loading parameters from file --- examples/catastrophic.json | 28 ++++++++++++++ examples/default.json | 28 ++++++++++++++ examples/flask_server.py | 79 +++++++++++++++++++++----------------- examples/working.json | 23 +++++++++++ 4 files changed, 122 insertions(+), 36 deletions(-) create mode 100644 examples/catastrophic.json create mode 100644 examples/default.json create mode 100644 examples/working.json diff --git a/examples/catastrophic.json b/examples/catastrophic.json new file mode 100644 index 0000000..6c5d33c --- /dev/null +++ b/examples/catastrophic.json @@ -0,0 +1,28 @@ +{ + "title": "Catastrophic infrastructure failure", + "more_information": { + "text": "cloudflare.com", + "link": "https://youtube.com/watch?v=dQw4w9WgXcQ" + }, + "browser_status": { + "status": "error", + "status_text": "Out of Memory" + }, + "cloudflare_status": { + "status": "error", + "location": "Everywhere", + "status_text": "Not Working" + }, + "host_status": { + "status": "error", + "location": "example.com", + "status_text": "On Fire" + }, + "error_source": "cloudflare", + "what_happened": "

There is a catastrophic failure.

", + "what_can_i_do": "

Please try again in a few years.

", + "perf_sec_by": { + "text": "Cloudflare", + "link": "https://youtube.com/watch?v=dQw4w9WgXcQ" + } +} \ No newline at end of file diff --git a/examples/default.json b/examples/default.json new file mode 100644 index 0000000..8bb8cd5 --- /dev/null +++ b/examples/default.json @@ -0,0 +1,28 @@ +{ + "html_title": "500: Internal server error", + "title": "Internal server error", + "error_code": 500, + "more_information": { + "hidden": false, + "text": "cloudflare.com", + "link": "https://youtube.com/watch?v=dQw4w9WgXcQ" + }, + "browser_status": { + "status": "ok" + }, + "cloudflare_status": { + "status": "error", + "status_text": "Not Working" + }, + "host_status": { + "status": "ok", + "location": "example.com" + }, + "error_source": "cloudflare", + "what_happened": "

There is an internal server error on Cloudflare\"s network.

", + "what_can_i_do": "

Please try again in a few minutes.

", + "perf_sec_by": { + "text": "Cloudflare", + "link": "https://youtube.com/watch?v=dQw4w9WgXcQ" + } +} \ No newline at end of file diff --git a/examples/flask_server.py b/examples/flask_server.py index 9152014..b90c13c 100644 --- a/examples/flask_server.py +++ b/examples/flask_server.py @@ -1,8 +1,17 @@ import os +import re import sys -from flask import Flask, request, send_from_directory -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from flask import ( + Flask, + json, + request, + abort, + send_from_directory +) + +examples_dir = os.path.dirname(os.path.abspath(__file__)) +sys.path.append(os.path.dirname(examples_dir)) from cloudflare_error_page import get_resources_folder, render as render_cf_error_page app = Flask(__name__) @@ -20,47 +29,45 @@ if use_cdn: return send_from_directory(res_folder, path) -@app.route("/") -def index(): +param_cache: dict[str, dict] = {} + +def get_page_params(name: str) -> dict: + name = re.sub(r'[/\\]', '', name) + params = param_cache.get(name) + if params is not None: + return params + try: + with open(os.path.join(examples_dir, f'{name}.json')) as f: + params = json.load(f) + param_cache[name] = params + return params + except Exception as _: + return None + + +@app.route("/", defaults={'name': 'default'}) +@app.route("/") +def index(name: str): + params = get_page_params(name) + if params is None: + abort(404) + # Get real Ray ID from Cloudflare header - ray_id = request.headers.get('Http-Cf-Ray', '') + ray_id = request.headers.get('Cf-Ray') # Get real client ip from Cloudflare header or request.remote_addr - client_ip = request.headers.get('Http-X-Forwarded-For') + client_ip = request.headers.get('X-Forwarded-For') if not client_ip: client_ip = request.remote_addr + params = { + **params, + "ray_id": ray_id, + "client_ip": client_ip, + } + # Render the error page - return render_cf_error_page({ - 'html_title': '500: Internal server error', - 'title': 'Internal server error', - 'error_code': 500, - 'more_information': { - "hidden": False, - "text": "cloudflare.com", - "link": "https://youtube.com/watch?v=dQw4w9WgXcQ", - }, - 'browser_status': { - "status": 'ok', - }, - 'cloudflare_status': { - "status": 'error', - "status_text": 'Not Working', - }, - 'host_status': { - "status": 'ok', - "location": 'example.com', - }, - 'error_source': 'cloudflare', # 'browser', 'cloudflare', or 'host' - 'what_happened': '

There is an internal server error on Cloudflare\'s network.

', - 'what_can_i_do': '

Please try again in a few minutes.

', - 'ray_id': ray_id, - 'client_ip': client_ip, - 'perf_sec_by': { - "text": "Cloudflare", - "link": "https://youtube.com/watch?v=dQw4w9WgXcQ", - }, - }, use_cdn=use_cdn), 500 + return render_cf_error_page(params, use_cdn=use_cdn), 500 if __name__ == "__main__": diff --git a/examples/working.json b/examples/working.json new file mode 100644 index 0000000..0b36866 --- /dev/null +++ b/examples/working.json @@ -0,0 +1,23 @@ + { + "title": "Web server is working", + "error_code": 200, + "more_information": { + "hidden": true + }, + "browser_status": { + "status": "ok", + "status_text": "Seems Working" + }, + "cloudflare_status": { + "status": "ok", + "status_text": "Often Working" + }, + "host_status": { + "status": "ok", + "location": "example.com", + "status_text": "Just Working" + }, + "error_source": "host", + "what_happened": "

This site is still working. And it looks great.

", + "what_can_i_do": "

Visit the site before it crashes someday.

" +} \ No newline at end of file