Skip to content

Commit

Permalink
添加“软件架构”说明,和“开发约定”;其他小细节调整
Browse files Browse the repository at this point in the history
  • Loading branch information
leeyisoft committed Sep 8, 2019
1 parent 0db8e86 commit 30e34b8
Show file tree
Hide file tree
Showing 7 changed files with 372 additions and 207 deletions.
318 changes: 171 additions & 147 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,99 @@
依赖 Tornado SQLAlchemy pycryptodome pytz 等

#### 软件架构
软件架构说明

```
tree -I '*svn|*node_module*|*git|py3|*.pyc|__pycache__|statics'
.
├── LICENSE
├── Pipfile
├── README.md
├── applications
│   ├── common
│   │   ├── const.py
│   │   ├── models
│   │   │   └── *.py
│   │   ├── filters
│   │   │   ├── filters/requests
│   │   │   │ └── *.py
│   │   │   └── filters/responses
│   │   │   └── *.py
│   │   ├── services
│   │   │   └── *.py
│   │   └── utils.py
│   ├── app1
│   │   ├── handlers
│   │   │   └── *.py
│   │   ├── models
│   │   │   └── *.py
│   │   ├── modules.py
│   │   ├── services
│   │   │   └── *.py
│   │   ├── filters
│   │   │   ├── filters/requests
│   │   │   │ └── *.py
│   │   │   └── filters/responses
│   │   │   └── *.py
│   │   ├── templates
│   │   │   └── */*.html
│   │   └── utils.py
│   └── app2
│   └── app3
├── configs
│   ├── dev.ini
│   └── local.ini
├── 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],一个变量 TREST_ENV
* configs 应用配置文件
* configs/local.ini 本地开发环境相关配置
* configs/dev.ini 开发环境相关配置
* configs/test.ini 测试环境相关配置
* configs/product.ini 生产环境相关配置
* applications 应用rest api相关代码
* applications/common/models 公共应用数据模型层
* applications/common/services 公共应用服务层
* applications/common/filters/requests 公共应用请求过滤器层
* applications/common/filters/responses 公共应用响应过滤器层
* applications/common/const.py 公共应用常量
* applications/common/utils.py 公共应用助手函数
* applications/app1 独立应用
* applications/app1/handlers app1用控制器层
* applications/app1/models app1用数据模型层
* applications/app1/services app1应用服务层
* applications/app1/templates app1应用视图层
* applications/app1/filters/requests app1应用请求过滤器层
* applications/app1/filters/responses app1应用响应过滤器层
* datas 数据
* datas/locales 多语言数据
* datas/json JSON数据文件
* datas/sql SQL数据文件
* \*\.* 其他数据文件
* logs 日志文件
* statics Web静态资源
* tests 测试脚本
* server.py 项目入口文件
* README.md 项目说明文件
* Pipfile pipenv配置文件
* LICENSE 开源许可证
* .gitignore Git忽略文件

#### 安装教程

Expand All @@ -26,167 +114,103 @@
> pip install git+https://gitee.com/leeyi/trest.git
#### 使用说明
参考 下面Demo项目
参考 下面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 文件](https://gitee.com/leeyi/trest/blob/master/demo_dot.env)
```
# TREST_ENV is not one of the local, dev, test, or product
[sys]
TREST_ENV = dev
```

run
```
pipenv install --skip-lock
pipenv shell
export ENV=local
python server.py --port=8080
python server.py --port=5080
```

f'{ROOT_PATH}/configs/{env}.ini' demo
```
# f'{ROOT_PATH}/configs/{env}.ini'
# 注意配置解析出来都是字符串,请不要带单引号或者双引号
# 例如 '0.0.0.0' "0.0.0.0" 都会报错

[sys]
arbitrary_ip = 0.0.0.0
port = 5080
local_ip = 127.0.0.1
translation = true
time_zone = Asia/Shanghai
language_code = zh-hans
login_pwd_rsa_encrypt = True
default_aes_secret = 883d65f06fd447f3a1e69a36e73f58e0
admin_session_key = de0b3fb0c2f44563944a8cccca7f225a
front_session_key = 171630947de24c969c28b2d178c4e0fe
valid_code_key = ab1195c6f0084b4f8b007d3aa7628a38
token_key = f30a2331813f46d0adc2bcf26fcbbbf4
rabbitmq_config =
sentry_url =
config_cache_prefix = conf:
user_cache_prefix = user:
admin_cache_prefix = admin:
# 超级管理员角色ID
super_role_id = 1
default_role_id = 2
[tornado]
debug = true
xsrf_cookies = true
xheaders = true
cookie_secret = e921bfcd-ace4-4124-8657-c57a162365f6
[session]
cache_alias = default_redis
name = nkzpg9NKBpKS2iaK
cookie_domain =
cookie_path = /
expires = 86400
secret = fLjUfxqXtfNoIldA0A0J
version = v0.1.0
[sqlalchemy]
# (s秒)
ping_db = 300
# 每次取出ping多少个连接
ping_conn_count = 5
connect_timeout = 3
echo = true
echo_pool = true
max_overflow = 10
pool_timeout = 5
encoding = utf8
pool_size = 5
pool_recycle = 3600
poolclass = QueuePool
[default_redis]
location = 127.0.0.1:6379
db = 0
password = abc123456
ping_interval = 120
parser_class = redis.connection.DefaultParser
socket_timeout = 2
socket_connect_timeout = 2
[redis]
host = 127.0.0.1
port = 6379
password = abc123456
charset = utf8
db = 3
[db_master]
driver = mysql+mysqldb
user = root
password = 123456
host = 127.0.0.1
port = 3306
database = db_py_admin
charset = utf8mb4
[db_slave]
driver = mysql+mysqldb
user = root
password = 123456
host = 127.0.0.1
port = 3306
database = db_py_admin
charset = utf8mb4
like this [./tests/app_demo/configs/dev.ini](https://gitee.com/leeyi/trest/blob/master/tests/app_demo/configs/dev.ini)

```
# [开发约定](https://gitee.com/leeyi/trest/blob/master/promise.md)

* 开发约定 [https://gitee.com/leeyi/trest/blob/master/promise.md](https://gitee.com/leeyi/trest/blob/master/promise.md)
* 其他约定遵从[Python风格规范](http://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_language_rules/)[Python 编码规范](http://liyangliang.me/posts/2015/08/simple-python-style-guide/)

#### Demo
* [https://gitee.com/leeyi/py_admin](https://gitee.com/leeyi/py_admin/tree/dev/)
```
from trest.exception import JsonError
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
[description]
"""
from trest.router import put
from trest.router import get
from trest.router import delete
from trest.router import post
from trest.router import put
from trest.router import delete
from trest.handler import Handler
from trest.exception import JsonError
"""
在 applications/admin/handlers/abcd.py
# 文件名称不以 _ 开头,明确API方法名称要唯一
url 像这样,
/admin/welcome
/admin/welcome2
/welcome3
/admin/welcome4
"""
class WelcomeHandler(CommonHandler):
@get('welcome')
@required_permissions()
@tornado.web.authenticated
def welcome_get(self, *args, **kwargs):
"""后台首页
"""
# menu = AdminMenu.main_menu()
# print('menu ', menu)
# self.show('abc')
params = {
}
self.render('dashboard/welcome.html', **params)
@get('welcome2')
@required_permissions()
@tornado.web.authenticated
def welcome_get2(self, *args, **kwargs):
"""后台首页
"""
self.success(data=['welcome2'])
@get('/welcome3')
@required_permissions()
@tornado.web.authenticated
def welcome_get(self, *args, **kwargs):
"""后台首页
"""
self.success(data=['welcome3'])
@post('welcome4')
@required_permissions()
@tornado.web.authenticated
def welcome_post(self, *args, **kwargs):
"""后台首页
"""
self.success(data=['welcome3'])
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响应
Expand Down
Loading

0 comments on commit 30e34b8

Please sign in to comment.