5 Star 16 Fork 8

leeyi / trest

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.en.md 7.61 KB
一键复制 编辑 原始数据 按行查看 历史
leeyi 提交于 2020-07-17 09:37 . 避免 master/slave 等术语

T-Rest

  • T = Tornado
  • Rest = Restful

介绍

基于Tornado结合asyncio的web mvc框架

依赖 Tornado SQLAlchemy pycryptodome pytz 等

软件架构

tree -I '*svn|*node_module*|*git|py3|*.pyc|__pycache__|statics'
.
├── LICENSE
├── Pipfile
├── README.md
├── applications
│   ├── common
│   │   ├── const.py
│   │   ├── models
│   │   │   └── *.py
│   │   ├── assemblers
│   │   │     └── *.py
│   │   ├── services
│   │   │   └── *.py
│   │   └── utils.py
│   ├── app1
│   │   ├── handlers
│   │   │   └── *.py
│   │   ├── models
│   │   │   └── *.py
│   │   ├── modules.py
│   │   ├── services
│   │   │   └── *.py
│   │   ├── assemblers
│   │   │     └── *.py
│   │   ├── templates
│   │   │   └── */*.html
│   │   └── utils.py
│   └── app2
│   └── app3
├── configs
│   ├── dev.yaml
│   └── local.yaml
├── datas
│   ├── locales
│   │   ├── en_US.csv
│   │   └── zh_CN.csv
│   ├── menu
│   │   └── menu0.json
│   ├── mysql
│   │   └── *.sql
│   ├── nginx_vhost.conf
│   ├── production_deploy.md
│   ├── supervisor_tornado.conf
│   └── supervisord.conf
├── logs
│   └── *.log
├── server.py
└── tests
    └── *_test.py

软件架构说明

  • .env 环境配置文件,只有一个section [sys],一个变量 RUNTIME_ENV
  • configs 应用配置文件
    • configs/local.yaml 本地开发环境相关配置
    • configs/dev.yaml 开发环境相关配置
    • configs/test.yaml 测试环境相关配置
    • configs/product.yaml 生产环境相关配置
  • applications 应用rest api相关代码
    • applications/common/models 公共应用数据模型层
    • applications/common/services 公共应用服务层
    • applications/common/assemblers 公共组装器层
    • applications/common/const.py 公共应用常量
    • applications/common/utils.py 公共应用助手函数
    • applications/app1 独立应用
    • applications/app1/handlers app1用控制器层,有路由器调用,负责接收并且校验参数
    • applications/app1/services app1应用服务层,在控制器里面调用,负责一个业务逻辑
    • applications/app1/models app1用数据模型层,在服务层调用,负责对一个数据库表CURD操作
    • applications/app1/assemblers app1组装器层,在控制器里面调用,负责把服务层数据响应给API
    • applications/app1/templates app1应用视图层,渲染service数据
  • datas 数据
    • datas/locales 多语言数据
    • datas/json JSON数据文件
    • datas/sql SQL数据文件
    • *.* 其他数据文件
  • logs 日志文件
  • statics Web静态资源
  • tests 测试脚本
  • server.py 项目入口文件
  • README.md 项目说明文件
  • Pipfile pipenv配置文件
  • LICENSE 开源许可证
  • .gitignore Git忽略文件

安装教程

把下面一行代码放入Pipfile文件 [packages]下面

trest = {editable = true,git = "https://gitee.com/leeyi/trest.git",ref = "main"}

或者直接

pipenv install -e git+https://gitee.com/leeyi/trest.git@main#egg=trest

或者

pip install git+https://gitee.com/leeyi/trest.git

使用说明

参考 下面Demo项目,

在项目根目录( ROOT_PATH )下面创建 server.py 文件

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import resource
resource.setrlimit(resource.RLIMIT_NOFILE, (10240, 9223372036854775807))

from tornado.options import define

abs_file = os.path.abspath(sys.argv[0])
ROOT_PATH = abs_file[:abs_file.rfind('/')]
define('ROOT_PATH', ROOT_PATH)

# 把当前目录添加到 sys.path 开头
sys.path.insert(0, ROOT_PATH)

from trest.webserver import run


if __name__ == "__main__":
    try:
       server = run()
    except KeyboardInterrupt:
        sys.exit(0)

在 项目根目录( ROOT_PATH ) 下面创建 .env 文件

# RUNTIME_ENV is not one of the local, dev, test, or product
# the colon must have Spaces around it
RUNTIME_ENV : local

run

pipenv install --skip-lock
pipenv shell
python server.py --port=5080
python tests/app_demo/server.py --port=5081

f'{ROOT_PATH}/configs/{env}.yaml' demo

like this ./tests/app_demo/configs/dev.yaml

开发约定

Demo

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
[description]
"""
from trest.router import put
from trest.router import get
from trest.router import post
from trest.router import delete
from trest.handler import Handler
from trest.exception import JsonError


class DemoHandler(Handler):
    @post('demo0')
    def add(self):
        return self.success(data = ['post', 'demo0'])

class Demo1Handler(Handler):
    @post('demo1')
    def add(self):
        return self.success(data = ['post', 'demo1'])

class Demo2Handler(Handler):
    @get('demo2')
    def get_demo2(self):
        return self.success(data = ['get', 'demo2', ])

    @get('demo2')
    def get_demo2(self):
        return self.success(data = ['get', 'demo23', ])

    @delete('demo3/(?P<id>\d*)')
    def del_demo3(self, id):
        return self.success(data = ['delete', 'demo3', id])

    @delete('demo2/(?P<id>\d*)')
    def del_demo2(self, id):
        return self.success(data = ['delete', 'demo2', id])
API响应

在任意的地方使用 raise JsonError

from trest.exception import JsonError

raise JsonError('msg')
raise JsonError('msg', 0)
raise JsonError('msg', 1, [])
raise JsonError('msg', 1, [1,2,3])

避免 master/slave 等术语

Old New 说明
master main 主要的
slave subordinate 从属的
blacklist denylist 拒绝名单
whitelist allowlist 允许名单

参与贡献

  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request

码云特技

  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. 码云官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解码云上的优秀开源项目
  4. GVP 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目
  5. 码云官方提供的使用手册 https://gitee.com/help
  6. 码云封面人物是一档用来展示码云会员风采的栏目 https://gitee.com/gitee-stars/
Python
1
https://gitee.com/leeyi/trest.git
git@gitee.com:leeyi/trest.git
leeyi
trest
trest
main

搜索帮助