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 / ErrorHandlerMiddleware / 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
from ctlib_webstack.fastapi.error_handler import ErrorHandler, ErrorHandlerWrapper

app = FastAPI()

# ErrorHandler 支持无 app 初始化,按需再挂入 FastAPI
error_handler = ErrorHandler()

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

# 如需在最外层统一兜住异常,可自定义一个全局中间件:
class GlobalErrorMiddleware:
    def __init__(self, app):
        self.app = ErrorHandlerWrapper(app, error_handler)

    async def __call__(self, scope, receive, send):
        await self.app(scope, receive, send)

app.add_middleware(GlobalErrorMiddleware)

过滤规则说明(示例)

  • 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.3.tar.gz (10.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.3-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ctlib_webstack-0.1.3.tar.gz
  • Upload date:
  • Size: 10.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.3.tar.gz
Algorithm Hash digest
SHA256 4d1a9b090c389f72f0cb83842a763ec7ce89972f58c9d8009421a0a158b4e56b
MD5 7dbb5ef47bf9892c3f2644b542f6642a
BLAKE2b-256 9953c6a1c0691fa0666075fb063239b0d3e4754027018ec4e18cb4d4beb95f80

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ctlib_webstack-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 12.0 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a980fe5e222816393adfa9daf5ded6485182f0ec86443b4b0019bd63c100a888
MD5 f6a7d7da428b3e82f98558eb2024e42e
BLAKE2b-256 1ff553f20544a69dba33ef78298d019b665f74279b3cdcf29e1aa51cc24d7d39

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