Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom routing converter #46

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion easyun/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from config import env_config
from flask_cors import CORS
from flask_migrate import Migrate

# from .common.result import BaseResponseSchema
from .celery import FlaskCelery
from config import Config
Expand Down Expand Up @@ -54,6 +55,11 @@ def create_app(run_env=None):
# defaults to "data"
app.config['BASE_RESPONSE_DATA_KEY'] = 'detail'

# 注册Demo内容
# 将自定义Converter添加到url_map中
from easyun.modules.demo.converter import RegConverter
app.url_map.converters["re"] = RegConverter

# 初始化扩展
register_extensions(app=app)

Expand All @@ -70,21 +76,24 @@ def create_app(run_env=None):
# 初始化AWS云环境账号基础信息
register_aws_account(app)



return app


# 注册 Flask blueprints
def register_blueprints(app: APIFlask):
"""Register Flask blueprints."""
from .common import auth
from .modules import mserver, mstorage, datacenter, account, dashboard
from .modules import mserver, mstorage, datacenter, account, dashboard, demo

app.register_blueprint(auth.bp)
app.register_blueprint(mserver.bp)
app.register_blueprint(datacenter.bp)
app.register_blueprint(account.bp)
app.register_blueprint(mstorage.bp)
app.register_blueprint(dashboard.bp)
app.register_blueprint(demo.bp)
return None


Expand Down
14 changes: 14 additions & 0 deletions easyun/modules/demo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
'''
:file: __init__.py
:author: -Farmer
:url: https://blog.farmer233.top
:date: 2022/01/20 01:32:21
'''
from apiflask import APIBlueprint

ver = '/api/v1'

bp = APIBlueprint('Demo', __name__, url_prefix=ver + '/demo')

from . import view
20 changes: 20 additions & 0 deletions easyun/modules/demo/converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
'''
:file: view.py
:author: -Farmer
:url: https://blog.farmer233.top
:date: 2022/01/20 01:33:42
'''


import typing as t

from werkzeug.routing import BaseConverter

class PhoneConverter(BaseConverter):
"""匹配中国手机号码"""

def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
super().__init__(*args, **kwargs)
self.regex = "13[0-9]{9}"

14 changes: 14 additions & 0 deletions easyun/modules/demo/view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
'''
:file: view.py
:author: -Farmer
:url: https://blog.farmer233.top
:date: 2022/01/20 01:43:11
'''

from . import bp

@bp.get("/phone/<phone:phone>")
def get_info(phone):
print(f"user phone: {phone}")
return f"用户手机号: {phone}"