初始化项目,整理基础目录结构

This commit is contained in:
chenzuoqing 2021-11-29 11:09:21 +08:00
commit 0d771bf0cb
14 changed files with 192 additions and 0 deletions

12
.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
.idea
.vscode
venv
__pycache__
*.pyc
*.pyi
*.log
logs/
*.swap

21
app.py Normal file
View File

@ -0,0 +1,21 @@
import sys
import os.path
from flask import Flask
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
from database.mongodb import initialize_db
from routes import blueprint_api
app = Flask(__name__)
app.config.from_pyfile("settings/dev.py")
initialize_db(app)
# 注册蓝图
app.register_blueprint(blueprint_api)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001, debug=True)

0
settings/__init__.py Normal file
View File

5
settings/dev.py Normal file
View File

@ -0,0 +1,5 @@
# mongodb 地址
MONGODB_SETTINGS = {
'host': 'mongodb://admin:111111@10.2.2.10:27017/ops_api?authSource=admin',
}

0
settings/prod.py Normal file
View File

0
src/common/__init__.py Normal file
View File

113
src/common/views.py Normal file
View File

@ -0,0 +1,113 @@
from flask_restful import abort, Resource, marshal, fields
class ModelViewBase(Resource):
model = None
fields = {}
request_parse = None
def get_object(self, pk):
try:
return self.model.objects.get_or_404(id=pk)
except:
abort(404, msg=f"resource '{pk}' not found")
def get_queryset(self):
return self.model.objects
class ListMixin(ModelViewBase):
def get(self):
"""获取列表数据"""
# 过滤后的数据,
# TODO 匹配过滤参数
queryset = self.get_queryset()
# TODO 还要匹配分页,分页参数
res = queryset.all()
ret_format = {
"count": fields.Integer,
"data": fields.List(fields.Nested(self.fields))
}
return marshal({"data": res, "count": queryset.count()}, ret_format)
class CreateMixin(ModelViewBase):
def pre_create(self, args):
"""创建前钩子,接收参数,可以对参数进行处理,最后保存此方法返回的数据"""
return args
def post(self):
"""创建对象"""
# 解析参数
args = self.request_parse.parse_args()
# 创建前钩子,
validated_data = self.pre_create(args)
# 保存对象
obj = self.model(**validated_data)
obj.save()
# 返回创建信息
return marshal(obj, self.fields)
class RetrieveMixin(ModelViewBase):
def get(self, pk):
obj = self.get_object(pk)
return marshal(obj, self.fields)
class UpdateMixin(ModelViewBase):
def pre_update(self, obj, args):
"""更新前钩子"""
data = {}
for k, v in args.items():
# 为 None 的不设置
if v is not None:
data[k] = v
return data
def put(self, pk):
# 获取对象
obj = self.get_object(pk)
# 解析参数
args = self.request_parse.parse_args()
validated_data = self.pre_update(obj, args)
# 更新对象、保存
obj.update(**validated_data)
obj.save()
# 重新读取数据
obj.reload()
return marshal(obj, self.fields)
class DestroyMixin(ModelViewBase):
def pre_destroy(self, obj):
"""删除对象前的方法,可以在这拦截做些操作"""
pass
def delete(self, pk):
"""删除对象方法"""
obj = self.get_object(pk)
# 删除前钩子
self.pre_destroy(obj)
obj.delete()
return {"id": pk}
class ListCreateViewSet(ListMixin, CreateMixin):
"""不带 `pk` 参数的视图集合"""
class DetailViewSet(RetrieveMixin, UpdateMixin, DestroyMixin):
"""带 `pk` 参数的视图集合"""

0
src/database/__init__.py Normal file
View File

7
src/database/mongodb.py Normal file
View File

@ -0,0 +1,7 @@
from flask_mongoengine import MongoEngine
db = MongoEngine()
def initialize_db(app):
db.init_app(app)

0
src/game/__init__.py Normal file
View File

2
src/game/models.py Normal file
View File

@ -0,0 +1,2 @@

11
src/game/routes.py Normal file
View File

@ -0,0 +1,11 @@
from game.views import Server
from flask import Blueprint
from flask_restful import Api
# 当前app的蓝图以app名为前缀
game = Blueprint('game', __name__, url_prefix="/game")
# 增加路由
api = Api(game)
api.add_resource(Server, '/server/', endpoint="server")

12
src/game/views.py Normal file
View File

@ -0,0 +1,12 @@
from flask_restful import Resource
class Server(Resource):
""""""
def get(self):
return {"msg": "ok", "method": "get"}
def post(self):
return {"msg": "ok", "method": "post"}

9
src/routes.py Normal file
View File

@ -0,0 +1,9 @@
from flask import Blueprint
from game.routes import game
blueprint_api = Blueprint('api-main', __name__, url_prefix='/api')
# 注册子蓝图,嵌套
blueprint_api.register_blueprint(game)