Skip to main content

Pancake Web Plugin - aiohttp 驱动的 Web 服务器插件

Project description

pancake-web

Spring MVC 风格的 Web 服务器插件,基于 aiohttp。

安装

pip install pancake-web

依赖自动安装 aiohttp

快速开始

1. 配置 YAML

src/resource/yaml/web.yaml 中配置(所有配置项都有缺省值,可省略):

web:
  host: 127.0.0.1
  port: 8080
  debug: false
  static: src/static
  templates: src/templates
  cors:
    allow_origins: "*"
    allow_methods: "GET,POST,PUT,DELETE,OPTIONS"
    allow_headers: "*"
    max_age: 3600
  session:
    secret_key: my-secret-key
    max_age: 86400
    secure: false
  request:
    timeout: 30
    max_body_size: 1048576
  server:
    max_connections: 100
    backlog: 128

2. 创建控制器

src/controller/user_controller.py

from dataclasses import dataclass

@controller("/api/users")
class UserController:
    user_service: UserService = inject()

    @get("/")
    async def list_users(self, request):
        return self.user_service.find_all()

    @get("/{id}")
    async def get_user(self, request, id: int = path_variable()):
        return self.user_service.find_by_id(id)

    @post("/")
    async def create_user(self, request, body: CreateUserForm = request_body()):
        return self.user_service.create(body), 201

    @delete("/{id}")
    async def delete_user(self, request, id: int = path_variable()):
        self.user_service.delete(id)

@dataclass
class CreateUserForm:
    name: str
    age: int = 0

3. 启动

python main.py

访问 http://127.0.0.1:8080/api/users

YAML 配置项

配置键 类型 缺省值 说明
web.host str 127.0.0.1 服务器地址
web.port int 8080 服务器端口
web.debug bool false 调试模式
web.static str src/static 静态文件目录
web.templates str src/templates 模板目录
web.cors.allow_origins str * CORS 允许来源
web.cors.allow_methods str GET,POST,PUT,DELETE,OPTIONS CORS 允许方法
web.cors.allow_headers str * CORS 允许头
web.cors.max_age int 3600 预检缓存秒数
web.session.secret_key str null Session 密钥
web.session.max_age int 86400 Session 有效期
web.session.secure bool false 仅 HTTPS
web.request.timeout int 30 请求超时秒数
web.request.max_body_size int 1048576 最大请求体字节
web.server.max_connections int 100 最大连接数
web.server.backlog int 128 TCP backlog

装饰器

路由

装饰器 用途
@controller(prefix) 标记类为控制器(自动注册为 Dough,支持 IoC)
@get(path) GET 路由
@post(path) POST 路由
@put(path) PUT 路由
@delete(path) DELETE 路由

参数绑定

标记 用途
path_variable() 路径变量 /users/{id}
request_param(name, default) 查询参数 ?page=1
request_body() JSON 请求体(支持 dataclass/Struct)

中间件与异常

装饰器 用途
@middleware(order) 注册中间件(order 越小越先执行)
@exception_handler(exc_class) 注册全局异常处理器

返回值自动转换

返回类型 转换结果
dict / list JSON 响应
str HTML 响应
dataclass / Struct JSON 响应(自动 asdict)
(data, status) 指定状态码的 JSON
(data, status, headers) 指定状态码和响应头
None 204 No Content
web.Response 原样返回

示例:中间件

@middleware(order=0)
class LoggingMiddleware:
    async def process(self, request, handler):
        logger.info(f"{request.method} {request.path}")
        response = await handler(request)
        logger.info(f"响应: {response.status}")
        return response

示例:异常处理

@exception_handler(ValueError)
async def handle_value_error(request, exc):
    return JsonResponse({"error": str(exc)}, status=400)

@exception_handler(PermissionError)
async def handle_permission(request, exc):
    return JsonResponse({"error": "无权限"}, status=403)

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

pancake_web-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.

pancake_web-0.1.1-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pancake_web-0.1.1.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pancake_web-0.1.1.tar.gz
Algorithm Hash digest
SHA256 676eab87f49ddcff99535a2f4e3f037de2bb99d9e715726f55f8180a7fa233d5
MD5 a402f309e22d60085a7b93299937da10
BLAKE2b-256 b744ceefc0c3037a61a1a9c70608f523ef147254808c954c85388bd677609859

See more details on using hashes here.

Provenance

The following attestation bundles were made for pancake_web-0.1.1.tar.gz:

Publisher: publish.yml on PancakeFramework/Pancake-Web

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: pancake_web-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pancake_web-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a825d000561edb216965cf1bcfab1896e55f565217e8e4acb8ffa637f62db80b
MD5 31fc9ab55fdaccb72d292eafa3cc498a
BLAKE2b-256 d307847c93b1d15f1a9089f2c60f6de0f7579445482555ba06309201bfd4be50

See more details on using hashes here.

Provenance

The following attestation bundles were made for pancake_web-0.1.1-py3-none-any.whl:

Publisher: publish.yml on PancakeFramework/Pancake-Web

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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