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.2.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.2-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ctlib_webstack-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 a8cbdef0fd6bf300b843ff33a69197b33b377896883bec6132a635846747b3ff
MD5 e1eaf282d1d7c360a0ce323fe1ca3005
BLAKE2b-256 aaa8faae382567ae07f13e62281e512f32f8b1520d31ccc23e117383b6dc3adf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ctlib_webstack-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f7ce2ec9c0f2fcbffac071d11dd5038283b6cb1ee312b8c5414efc2f48e99fe4
MD5 b0c0dd065f96d0cee85cae33b79d6e60
BLAKE2b-256 9e8607932d2cc42bbef9fdd65ce2c7f7cc84f4dad0fbeaa32cceff16c65171fe

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