9
0
mirror of https://github.com/donlon/cloudflare-error-page.git synced 2026-01-06 15:41:45 +00:00

editor/server: reorganize files

This commit is contained in:
Anthony Donlon
2025-12-28 22:31:42 +08:00
parent f2f4a8223d
commit c2cad60cce
10 changed files with 37 additions and 20 deletions

View File

@@ -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

View File

@@ -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('/<path:path>')
def index(path: str):
return send_from_directory(res_folder, path)
return send_from_directory(default_res_folder, path)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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/**/*"]

View File

@@ -1,3 +0,0 @@
Flask
Flask-Limiter
Flask-SqlAlchemy