Skip to main content

A collection of common web middlewares for security and request processing.

Project description

ctlib-webstack

面向 FastAPI/Starlette 的 Web 组件集合,目前包含:

  • IP 访问地理过滤中间件(IPFilterMiddleware
  • 统一错误处理器与中间件包装(ErrorHandler / ErrorHandlerWrapper

特性

  • 按国家/地区/城市或地理编码的灵活过滤规则
  • 统一异常分派:按 HTTP 状态码或异常类型注册处理器,未命中回退 500
  • 支持异常处理前/后钩子,方便埋点、告警、统一响应格式化
  • 中间件级一键包裹,兜住下游所有异常,避免“内层中间件异常击穿”

目录结构

  • src/ctlib_webstack/fastapi/ip_filter/:IP 地理过滤中间件
  • src/ctlib_webstack/fastapi/error_handler/:统一错误处理

快速上手(FastAPI)

from fastapi import FastAPI
from ctlib_webstack.fastapi import IPFilterMiddleware, ErrorHandler

app = FastAPI()

error_handler = ErrorHandler(app)

app.add_middleware(
    IPFilterMiddleware,
    ip_info_url="https://your-geoip-endpoint",
    filter_regions=[
        "Geo:Shenzhen",
        "GeoCode:HK-HCW---",
        "US-Texas",
    ],
    error_handler=error_handler,  # 可选:启用统一异常处理
)

过滤规则说明(示例)

  • GeoCode:<code>:地理编码,如 GeoCode:HK-HCW---
  • GeoID:<int>:地理 ID,如 GeoID:12345
  • Geo:<name>:名称匹配,如 Geo:Shenzhen
  • country-region-city:分段(可截断),如 US-TexasCNHK-Yau Tsim Mong

数据来源参考 ctlib_geoip 项目。

开发提示

  • 本仓库使用 src/ 布局;在本地直接运行示例时,可将项目根目录加入 PYTHONPATH 或以可编辑模式安装本项目。

相关文档

  • src/ctlib_webstack/fastapi/ip_filter/README.md
  • src/ctlib_webstack/fastapi/error_handler/README.md

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ctlib_webstack-0.1.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ctlib_webstack-0.1.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file ctlib_webstack-0.1.0.tar.gz.

File metadata

  • Download URL: ctlib_webstack-0.1.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.6

File hashes

Hashes for ctlib_webstack-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2125a7b4d18e8b57d4bc50a726a46f6bc543209f5780243d50f5f4bf2b9f5384
MD5 f7b660ec97655729380c8e863c6a842a
BLAKE2b-256 f545c839e8c729944c24271d8bf722febe19966a65bc05fe19588db08a1ff16d

See more details on using hashes here.

File details

Details for the file ctlib_webstack-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ctlib_webstack-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.6

File hashes

Hashes for ctlib_webstack-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 51b6abf4da33dacd6ea12a072fbfcfc9056e57e5d0b5e60e0e58ac39a07c8951
MD5 28595cedf0b37e89338bc0429aaff903
BLAKE2b-256 651ef6d02f9bedea223d192f48b7a4149ec3d880e7d997808eb339cc2e1bcf7d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page