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

Uploaded Python 3

File details

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

File metadata

  • Download URL: ctlib_webstack-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 f341249516ce7190fb6ca7e94f1b92b762a66878acaca16bb67eedf4d97f559c
MD5 edf192092442743b53be53f5daa07f51
BLAKE2b-256 cfc975a81e1ec4e316bf93bdc421ce9683e402832b55ea5f7de5545568f63a30

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ctlib_webstack-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 41b38a7028a607336c0cf0f1fefecddf0f6cd4a3b6d8cd12e72a1aedd17bb9ad
MD5 c9c5f5b6405b4619ea717023ece0d202
BLAKE2b-256 b04a529e45739e112095bc2958067a60218c4fc19fbb451d176ff185ef8480a7

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