Skip to main content

yet a sanic route extension.

Project description

sanic_route

扩展了 Sanic 的路由装饰器,使得通过一次注册就加载整个模块包括其子模块内全部被 @http_* 装饰的处理器。

使用和示例

# 通过 pip 安装
pip install sanic_route

示例

示例结构如下,通过执行 python demo.py 来启动示例。

demo.py
example
  |- __init__.py
  |- controller
        |- __init__.py
        |- bar.py
        |- foo.py
# demo.py

from sanic import Sanic
from sanic.response import json
from sanic_route import attach
from example import controller

app = Sanic("sanic_route_debug_app")

# 注册整个模块内被 @http_* 装饰器标记的路由
attach(app, controller)

@app.route('/')
async def test(request):
    '''
    原本的 sanic 路由
    '''
    
    return json({'hello': 'world'})

if __name__ == '__main__':
    app.run()
# example/controller/__init__.py
# 注册了 /example.html 的路由。
from sanic.response import json
from sanic_route import http_get

@http_get('/example.html')
async def index(request):

    return json({'hello': 'world'})
# example/controller/bar.py
# 注册了 /bar/index.html 和 /bar/info/<bid:int>' 的路由。
from sanic.response import json
from sanic_route import http_get

class BarController:
    '''
    '''
    
    @http_get('/bar/index.html')
    async def index(self, request):
        '''
        
        '''
        page = request.args.get('p', [1])[0]
        return json({'bar': 'index', 'page': page })

    @http_get('/bar/info/<bid:int>')
    async def info(self, request, bid):
        return json({'bar': bid})
# example/controller/foo.py
from sanic.response import json
from sanic_route import http_any, http_delete, http_post, http_put

class FooController:
    '''
    
    '''

    @http_post('/foo/post')
    async def index(self, request):
        '''
        
        '''

        return json({'foo': 'post' })

    @http_any('/foo/any')
    async def request_any(self, request):
        '''
        
        '''
        
        return json({'foo': 'any' })

    @http_delete('/foo/delete/<fid:int>')
    async def delete_one(self, request, fid):
        '''
        
        '''

        return json({'foo': 'delete', 'fid': fid })

    @http_put('/foo/put')
    async def put_one(self, request):
        '''
        
        '''

        return json({ 'foo': 'put' })

开发与发布

注:如果要扩展本库,可以 fork 后添加自己的修改。

# 安装 setup.py 依赖的库。
pip install ran

# 安装本地的本库进环境 用于调试。
pip install -e . -i https://pypi.python.org/pypi

# 安装发布打包的工具。
pip install twine wheel

# 打包
python setup.py sdist bdist_wheel

# 上传
twine upload dist/*

# 发布后可以指定源更新,这样可以测试发布后的效果。
pip install --upgrade suoran -i https://pypi.python.org/pypi

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

sanic_route-0.0.1.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

sanic_route-0.0.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file sanic_route-0.0.1.tar.gz.

File metadata

  • Download URL: sanic_route-0.0.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for sanic_route-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a2e98b9ee193e8475b9c462b72c823c7e35ec0cea5de2d4d330b11e053804a09
MD5 3242a3a29636133efe3c123017aed2dd
BLAKE2b-256 a6f05acbf1ecd723966ccdd24c0aa93dedde68962d1579f60d6c69b1efb67200

See more details on using hashes here.

File details

Details for the file sanic_route-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: sanic_route-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for sanic_route-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ae548a74ace88b2c1b5f16f2df5abd65b36138164fe35707f552af78112c97db
MD5 01d8d4f36c9504dc9d6addcce50504bd
BLAKE2b-256 b0c2a5f5f2cc0083d14b6dcc0bb2dcde17b7eb3d6a10842ae16f9939d2ac6756

See more details on using hashes here.

Supported by

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