From c2cad60cce104d307901412a3a3031bb4f8e319b Mon Sep 17 00:00:00 2001 From: Anthony Donlon Date: Sun, 28 Dec 2025 22:31:42 +0800 Subject: [PATCH] editor/server: reorganize files --- editor/server/{ => app}/__init__.py | 9 +++----- editor/server/{ => app/data}/.gitignore | 0 .../{ => app/data}/cf-colos.bundled.json | 0 editor/server/{ => app}/editor.py | 6 ++--- editor/server/{ => app}/examples.py | 7 +++--- editor/server/{ => app}/models.py | 0 editor/server/{ => app}/share.py | 0 editor/server/{ => app}/utils.py | 9 ++++---- editor/server/pyproject.toml | 23 +++++++++++++++++++ editor/server/requirements.txt | 3 --- 10 files changed, 37 insertions(+), 20 deletions(-) rename editor/server/{ => app}/__init__.py (91%) rename editor/server/{ => app/data}/.gitignore (100%) rename editor/server/{ => app/data}/cf-colos.bundled.json (100%) rename editor/server/{ => app}/editor.py (58%) rename editor/server/{ => app}/examples.py (93%) rename editor/server/{ => app}/models.py (100%) rename editor/server/{ => app}/share.py (100%) rename editor/server/{ => app}/utils.py (95%) create mode 100644 editor/server/pyproject.toml delete mode 100644 editor/server/requirements.txt diff --git a/editor/server/__init__.py b/editor/server/app/__init__.py similarity index 91% rename from editor/server/__init__.py rename to editor/server/app/__init__.py index f5365e0..024a044 100644 --- a/editor/server/__init__.py +++ b/editor/server/app/__init__.py @@ -1,21 +1,20 @@ # SPDX-License-Identifier: MIT -import json import os import secrets import string import sys import tomllib +from pathlib import Path -from flask import Flask, redirect, request, url_for +from flask import Flask, redirect, url_for from flask_limiter import Limiter from flask_limiter.util import get_remote_address from flask_sqlalchemy import SQLAlchemy from sqlalchemy.orm import DeclarativeBase from werkzeug.middleware.proxy_fix import ProxyFix -root_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../') -sys.path.append(root_dir) +root_dir = Path(__file__).parent.parent.parent.parent class Base(DeclarativeBase): pass @@ -39,8 +38,6 @@ def _initialize_app_config(app: Flask): app.wsgi_app = ProxyFix( app.wsgi_app, x_for=1, x_proto=1 ) - app.json.ensure_ascii = False - app.json.mimetype = "application/json; charset=utf-8" secret_key = app.config.get('SECRET_KEY', '') if secret_key: app.secret_key = secret_key diff --git a/editor/server/.gitignore b/editor/server/app/data/.gitignore similarity index 100% rename from editor/server/.gitignore rename to editor/server/app/data/.gitignore diff --git a/editor/server/cf-colos.bundled.json b/editor/server/app/data/cf-colos.bundled.json similarity index 100% rename from editor/server/cf-colos.bundled.json rename to editor/server/app/data/cf-colos.bundled.json diff --git a/editor/server/editor.py b/editor/server/app/editor.py similarity index 58% rename from editor/server/editor.py rename to editor/server/app/editor.py index e478e72..e7d6933 100644 --- a/editor/server/editor.py +++ b/editor/server/app/editor.py @@ -7,8 +7,8 @@ from flask import ( send_from_directory, ) -root_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../') -res_folder = os.path.join(root_dir, 'editor/web/dist') +from . import root_dir +default_res_folder = os.path.join(root_dir, 'editor/web/dist') bp = Blueprint('editor', __name__, url_prefix='/') @@ -16,4 +16,4 @@ bp = Blueprint('editor', __name__, url_prefix='/') @bp.route('/', defaults={'path': 'index.html'}) @bp.route('/') def index(path: str): - return send_from_directory(res_folder, path) + return send_from_directory(default_res_folder, path) diff --git a/editor/server/examples.py b/editor/server/app/examples.py similarity index 93% rename from editor/server/examples.py rename to editor/server/app/examples.py index cbb2c63..d0a128f 100644 --- a/editor/server/examples.py +++ b/editor/server/app/examples.py @@ -16,14 +16,15 @@ from .utils import ( render_extended_template, ) -root_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../') +from . import root_dir examples_dir = os.path.join(root_dir, 'examples') +# TODO: copy to current folder for packaging + bp = Blueprint('examples', __name__, url_prefix='/') - - param_cache: dict[str, dict] = {} + def get_page_params(name: str) -> ErrorPageParams: name = re.sub(r'[^\w]', '', name) params = param_cache.get(name) diff --git a/editor/server/models.py b/editor/server/app/models.py similarity index 100% rename from editor/server/models.py rename to editor/server/app/models.py diff --git a/editor/server/share.py b/editor/server/app/share.py similarity index 100% rename from editor/server/share.py rename to editor/server/app/share.py diff --git a/editor/server/utils.py b/editor/server/app/utils.py similarity index 95% rename from editor/server/utils.py rename to editor/server/app/utils.py index def8f85..30f5562 100644 --- a/editor/server/utils.py +++ b/editor/server/app/utils.py @@ -2,6 +2,7 @@ import json import os import re from typing import Any +from pathlib import Path from cloudflare_error_page import ( ErrorPageParams, @@ -11,8 +12,6 @@ from cloudflare_error_page import ( from flask import current_app, request from jinja2 import Environment, select_autoescape -from . import root_dir - env = Environment( autoescape=select_autoescape(), trim_blocks=True, @@ -53,7 +52,7 @@ loc_data: dict = None def read_loc_file(path: str): try: - with open(os.path.join(root_dir, path)) as f: + with open(os.path.join(Path(__file__).parent / path)) as f: return json.load(f) except: return @@ -63,10 +62,10 @@ def get_cf_location(loc: str): global loc_data loc = loc.upper() if loc_data is None: - loc_data = read_loc_file('editor/server/cf-colos.json') + loc_data = read_loc_file('data/cf-colos.json') if loc_data is None: # From https://github.com/Netrvin/cloudflare-colo-list/blob/main/DC-Colos.json - loc_data = read_loc_file('editor/server/cf-colos.bundled.json') + loc_data = read_loc_file('data/cf-colos.bundled.json') if loc_data is None: return data: dict = loc_data.get(loc) diff --git a/editor/server/pyproject.toml b/editor/server/pyproject.toml new file mode 100644 index 0000000..f65e3f3 --- /dev/null +++ b/editor/server/pyproject.toml @@ -0,0 +1,23 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "cloudflare-error-page-editor-server" +description = "Backend of the Cloudflare error page editor" +requires-python = ">=3.13" +license = "MIT" +authors = [{ name = "Anthony Donlon" }] +dependencies = [ + "Flask", + "Flask-Limiter", + "Flask-SqlAlchemy", + "cloudflare-error-page @ {root:uri}/../../", +] +version = "0.0.0" + +[tool.hatch.metadata] +allow-direct-references = true + +[tool.hatch.build] +include = ["app/**/*"] diff --git a/editor/server/requirements.txt b/editor/server/requirements.txt deleted file mode 100644 index ed2e52c..0000000 --- a/editor/server/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -Flask -Flask-Limiter -Flask-SqlAlchemy \ No newline at end of file