From 0d771bf0cb76b5c018d8e0676ccde98387407b08 Mon Sep 17 00:00:00 2001 From: chenzuoqing Date: Mon, 29 Nov 2021 11:09:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=EF=BC=8C=E6=95=B4=E7=90=86=E5=9F=BA=E7=A1=80=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 12 +++++ app.py | 21 ++++++++ settings/__init__.py | 0 settings/dev.py | 5 ++ settings/prod.py | 0 src/common/__init__.py | 0 src/common/views.py | 113 +++++++++++++++++++++++++++++++++++++++ src/database/__init__.py | 0 src/database/mongodb.py | 7 +++ src/game/__init__.py | 0 src/game/models.py | 2 + src/game/routes.py | 11 ++++ src/game/views.py | 12 +++++ src/routes.py | 9 ++++ 14 files changed, 192 insertions(+) create mode 100644 .gitignore create mode 100644 app.py create mode 100644 settings/__init__.py create mode 100644 settings/dev.py create mode 100644 settings/prod.py create mode 100644 src/common/__init__.py create mode 100644 src/common/views.py create mode 100644 src/database/__init__.py create mode 100644 src/database/mongodb.py create mode 100644 src/game/__init__.py create mode 100644 src/game/models.py create mode 100644 src/game/routes.py create mode 100644 src/game/views.py create mode 100644 src/routes.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..143ba0e --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +.idea +.vscode + +venv +__pycache__ +*.pyc +*.pyi + +*.log +logs/ + +*.swap diff --git a/app.py b/app.py new file mode 100644 index 0000000..5509a70 --- /dev/null +++ b/app.py @@ -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) + diff --git a/settings/__init__.py b/settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/settings/dev.py b/settings/dev.py new file mode 100644 index 0000000..83d1098 --- /dev/null +++ b/settings/dev.py @@ -0,0 +1,5 @@ + +# mongodb 地址 +MONGODB_SETTINGS = { + 'host': 'mongodb://admin:111111@10.2.2.10:27017/ops_api?authSource=admin', +} diff --git a/settings/prod.py b/settings/prod.py new file mode 100644 index 0000000..e69de29 diff --git a/src/common/__init__.py b/src/common/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/common/views.py b/src/common/views.py new file mode 100644 index 0000000..03d2269 --- /dev/null +++ b/src/common/views.py @@ -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` 参数的视图集合""" diff --git a/src/database/__init__.py b/src/database/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/database/mongodb.py b/src/database/mongodb.py new file mode 100644 index 0000000..ce7a432 --- /dev/null +++ b/src/database/mongodb.py @@ -0,0 +1,7 @@ +from flask_mongoengine import MongoEngine + +db = MongoEngine() + + +def initialize_db(app): + db.init_app(app) diff --git a/src/game/__init__.py b/src/game/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/game/models.py b/src/game/models.py new file mode 100644 index 0000000..139597f --- /dev/null +++ b/src/game/models.py @@ -0,0 +1,2 @@ + + diff --git a/src/game/routes.py b/src/game/routes.py new file mode 100644 index 0000000..bf66d4c --- /dev/null +++ b/src/game/routes.py @@ -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") + diff --git a/src/game/views.py b/src/game/views.py new file mode 100644 index 0000000..6558ca3 --- /dev/null +++ b/src/game/views.py @@ -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"} + + diff --git a/src/routes.py b/src/routes.py new file mode 100644 index 0000000..5bcdacf --- /dev/null +++ b/src/routes.py @@ -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) +